Jump to content

Dropdown select submit and placing variables also syntax help


Bill Withers

Recommended Posts

I looked at the page and it is very odd indeed, but I see what is happening and it is exactly as I described above. The reason it looks to be behaving differently is because the random VALUES are changing on reloading the page. Again, this is a behavior implemented in FF. I assume so a user doesn't lose their input on a form if they happen to hit refresh by accident.

 

When the page first loads a select list might look like this:

<select name="select">
    <option value="2">Random</option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
</select>

 

When the user refreshes the page FF will reset the value that is currently selected. In this case since no option was selected the first option, Random with a value of 2, is the one considered selected. However, when the user refreshes the page, the code to build the page will set a new value for the "Random" option. FF will look for the first option with the previously selected value (2). If the new value for Random was not 2 then FF will select "Option 2" because it's value is 2.

 

An easy way to not have this behavior should be to also set the random options to be selected by default

<select name="select">
    <option value="2" selected="selected">Random</option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
</select>

 

Link to comment
Share on other sites

After looking closer at the code I think I can see why upon looking at the source I can deduce what one or more of the random options will say

when the submit button is pushed:

 

<option value='{$randomID}'>Random</option>

hehe

 

I changed the line to read

<option value='{$randomID}' selected=' "selected" '>Random</option>

 

that didn't work so I tried:

 

$selected = 'selected="selected"';

$optionsHTML = "<option value='{$randomID}' '{$selected}'>Random</option>\n" . $optionsHTML;

 

still no go

 

the quotes are killing me......

time to go read up

 

Link to comment
Share on other sites

Thanks,

I have that now in the code and FF still is doing it.

I am not sure but being as the string for the random ID has already been evaluated when the page first loads,

(you can see it when looking at the source when opening the page for the first time)

FF is doing its "special Behavior" as you pointed out earlier.

 

Not a big deal I guess for what I am doing but I can see how that could drive a real coder crazy..

 

A big thank you for all that you have done. I think the old farts that use it will like it the way it is.

 

 

Link to comment
Share on other sites

Yeah, not a bug deal by any means. A user hitting refresh on that page should be an edge case scenario. But, I found a fairly simple way to work around the issue if you want.

 

1. When creating the random value append a ".0" to then end of the value. So, a "2" because "2.0". That way, when the page refreshes, if the selected option was random with a value of "2.0" FF will only find a match on the Random option and not the true option with a value of 2.

 

2. Then in your processing code you should already be be doing some sanitization of the vaue to ensure they are ints. If not, you need to be doing this. $value = intval($_POST['post_value']). Since all your post values are in an array, you could easily use array_map() to do the whole lot in one line of code

$selections = array_map('intval', $_POST['selections']);

Link to comment
Share on other sites

Have you tried capturing the headers Fx sends when refreshing? Sounds like it might be sending a POST request, or perhaps triggering some JavaScript that modifies the dropdown menus that relies upon the first.

 

It is not sending a POST. FF simply has some built in functionality to retain/repopulate current form input when a refresh is performed. This happens when clicking the refresh button or F5. But, if you perform a "hard" refresh using Ctrl-F5 then it completely reloads the page as would be expected for a normal refresh. This makes sense as a feature. I have had instances where I was entering data into a form and accidentally refreshed the page losing my data. In fact this occurs for any input fields even if there is no form - so no POST could even occur.

 

However, I did some testing and found something really odd. When trying to reset/reselect a select list the functionality is starting from the last option and working backwards looking for a match. Take this sample select list:

<select name="sel">
<option value="">-- Choose One --</option>
<option value="A">Apple</option>
<option value="B">Banana</option>
<option value="A">Apricot</option>
<option value="P">Pear</option>
<option value="A">Avacado</option>
</option>

 

As you can see, the three options beginning with "A" all have the same value. But, all the other options have a unique value. If you select one of the options with a unique value and click refresh or F5 (in FF) the same option will be reselected. However, if you select any of the options with the value "A" and perform a refresh (again in FF) the "Avacado" option will be selected - even if the previous option was Apple or Apricot. Not sure why the logic works from the last option to the first. You can create a flat HTML page with just that code above and test it yourself.

Link to comment
Share on other sites

I guess there's no standard that says options must have unique values, huh?

 

No and there shouldn't be. For example, let's say you need to find out what NAICS industry code a person should be classified as based upon their job. Would a software developer go in "31 Manufacturing" or "51 Information" or "54 Professional, Scientific, and Technical Services"? Most likely the respondent won't know. So you can provide a list of jobs that would make sense to the respondent with the appropriate code as the value. however, some of them will have the same classification.

Link to comment
Share on other sites

Yeah, not a bug deal by any means. A user hitting refresh on that page should be an edge case scenario. But, I found a fairly simple way to work around the issue if you want.

 

1. When creating the random value append a ".0" to then end of the value. So, a "2" because "2.0". That way, when the page refreshes, if the selected option was random with a value of "2.0" FF will only find a match on the Random option and not the true option with a value of 2.

 

2. Then in your processing code you should already be be doing some sanitization of the vaue to ensure they are ints. If not, you need to be doing this. $value = intval($_POST['post_value']). Since all your post values are in an array, you could easily use array_map() to do the whole lot in one line of code

$selections = array_map('intval', $_POST['selections']);

 

Wow, FF really holds onto its need to do what it does.

I placed the .0 in for the value like so:

$optionsHTML = "<option value='{$randomID}.0' >Random</option>\n" . $optionsHTML;

 

The results are shown from the page source:

<form action='' method='post'>COURSE NAME <br><select name="selections[dd_courses]"><option value='21.0' >Random</option>

 

FF shows the actual selection that references ID 21 in the dropdown. IE and chrome show the random option and behaves as expected.. weird.

 

As for the other part, with the intval, I tried putting it in various places I thought it should go, But I havent had any luck. I am reading up and gonna try some more..

Thanks for all your help on this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.