Jump to content

Recommended Posts

Hello,

 

I have the following code in my PHP (form submission) application:

 

	<p><b>Field:  </b>
	<select name="field" value="Physics">
		<option selected value "<?php echo $field?>"><?php echo $field?></option>
		<!--<option disabled value="">----------</option>-->
		<option value ="Biology">Biology</option>
		<option value ="Chemistry">Chemistry</option>
		<option value ="Engineering">Engineering</option>
		<option value ="Et cetera">Et cetera</option>
		<option value ="Materials Science">Materials Science</option>
		<option value ="Physics">Physics</option>
	</select>
<b>Discipline:  </b>
	<select name="discipline">
		<option selected value = "<?php echo $discipline?>"><?php echo $discipline?></option>
		<optgroup label="Biology">
			<option value ="Cell">Cell</option>
			<option value ="Micro">Micro</option>
			<option value ="Molecular">Molecular</option>
			<option value ="Toxicology">Toxicology</option>
		<optgroup label="Chemistry">
			<option value ="Analytical">Analytical</option>
			<option value ="Biological">Biological</option>
			<option value ="Inorganic">Inorganic</option>
			<option value ="Materials">Materials</option>
			<option value ="Nuclear">Nuclear</option>
			<option value ="Organic">Organic</option>
			<option value ="Physical">Physical</option>
		</optgroup>
		<optgroup label="Engineering">
			<option value ="Chemical">Chemical</option>
			<option value ="Mechanical">Mechanical</option>
		</optgroup>
		<optgroup label="Et cetera">

		</optgroup>
		<optgroup label="Materials Science">
			<option value ="Materials">Materials</option>
		</optgroup>
		<optgroup label="Physics">
			<option value ="Astronomy">Astronomy</option>
			<option value ="Atomic">Atomic</option>
			<option value ="Materials">Materials</option>
			<option value ="Nuclear">Nuclear</option>
			<option value ="Particle">Particle</option>
			<option value ="Weapons">Weapons</option>
		</optgroup>
	</select>
(required)</p><br />

 

After a user submits the form (of which the code above is just a part) the data goes to a database and is retrieved automatically to fill out the form each time the user visits the page in order for them to know what info they had uploaded. 

 

In the section of code above, I want the user to select from drop down menus, their field and discipline and as I mentioned, have the drop down menus select the items relevant to each user each time the user re-visits the page. 

 

I used the following code to do so (as shown in the larger chunk above):

 

<option selected value "<?php echo $field?>"><?php echo $field?></option>

 

This works fine in IE but when users in Firefox go to the page, there is a problem....

 

In both browsers the proper and relevant items (selected by the users in the past) are listed in the top of the drop down menu (per the above code) and are therefore visible; however, in Firefox, if a user tries to resubmit their data (for an edit, etc) the drop down menus return an error as if none of the items of the drop down boxes were selected.  That is, even though the drop down menu lists the correct data and shows that it is selected, in Firefox this appears to be untrue as far as the PHP code is concerned.  If the user goes and re-selects the data in the drop down menu before re-submitting their data, there is no problem.

 

Is there anyone that can help me determine what changes I can make to get the program to work in both IE and Firefox?

Link to comment
https://forums.phpfreaks.com/topic/54347-problem-with-html-form/
Share on other sites

I prefer to use a function

 

<?php
function fieldSelect($field)
{
    $fields = array('Biology','Chemistry','Engineering','Et cetera','Materials Science','Physics');
    $str = "<select name='field'>\n";
    foreach ($fields as $f)
    {
        $sel = $f==$field ? 'selected' : '';
        $str .= "<option value='$f' $sel> $f</option>\n";
    }
    $str .= "</select>\n";
    return $str;
}

/**
* call the function passing the current value
*/
$currentfield = 'Physics';
echo fieldSelect($currentfield);  
?>

Agree with Barand - duplicating the value at the top of the select list is not standard behavior and could cause confusion.

 

However, since you have several select lists I would recommend making the function more generic so you can pass the select list values and the selected value:

 

<?php

function fieldSelect($fieldList, $selected) {

    foreach ($fieldList as $option)
    {
        $str .= "<option value='$f'". (($selected==$option)?' selected':'') . "> $f</option>\n";
    }
    return $str;
}
?>

 


Barand and MJ?

 

How can I make a second function to cover the next drop down list?  A 2D array?  I would like to, if possible and I assume it is (I am new at PHP but learning that it can do anything), make the second drop down dependent on the first.  That is, if a person selects chemistry, then the second drop down will only show the chemistry options.

 

Thank you for all of your help!@  I already learned a lot from this post.

Jason

@Barand, yep I missed that one.

 

But, I still stand by making functions as generic as possible to maximize portability and reusability.

 

The code below contains a working page with a revised function that can support both types of lists.

<?php

function fieldSelect($fieldList, $selected) {

    foreach ($fieldList as $optionKey => $option)
    {
        if (is_array($option)) {
            $str .= "<optgroup label=\"$optionKey\">\n";
            foreach ($option as $optionValue) {
                $str .= "<option value=\"$optionValue\"". (($selected==$optionValue)?' selected':'') . ">$optionValue</option>\n";
            }
            $str .= "</optgroup>\n";

        } else {
            $str .= "<option value=\"$option\"". (($selected==$option)?' selected':'') . ">$option</option>\n";
        }
    }
    return $str;
}


$oneDimensionSelected = "Engineering"; //Get from database or form submission
$twoDimensionSelected = "Nuclear"; //Get from database or form submission

?>


<html>
<head>

<body>

One Dimensional Select:
<select name="onedimension">
<?php
$optionArray = array('Biology','Chemistry','Engineering','Et cetera','Materials Science','Physics');
echo fieldSelect($optionArray, $oneDimensionSelected);

?>
</select>
<br><br>

Two Dimensional Select:
<select name="twdimension">
<?php
$optionArray = array(
    "Biology" => array ("Cell", "Micro", "Molecular", "Toxicology"),
    "Chemistry" => array ("Analytical", "Biological", "Inorganic", "Materials", "Nuclear", "Organic", "Physical")
);
echo fieldSelect($optionArray, $twoDimensionSelected);

?>
</select>

</body>
</html>

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.