Reputation: 9
I have a form with one drop down box(X) and one text box(Y). After selecting a value in X drop down box (For example: x2) and entering a value in the Y text box, when I click on the SUBMIT button, the value in the drop down box is getting reset to the First value(x1).I notice this same behavior, when I click on the refresh button. But this is not the case with text box Y. can anyone help me with a sample piece of code. Or please tell me where I am going wrong and what I need to change.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>history plugin demo</title>
</head>
<body>
<?php
$x = ( !empty( $_REQUEST['X'] ) ? $_REQUEST['X'] : null );
$y = ( !empty( $_REQUEST['Y'] ) ? $_REQUEST['Y'] : null );
?>
Ajax load<BR>
<form id="myForm" action='fields.php' method='GET' rel="history">
X <BR>
<select name="X" value="<?=$x;?>">
<option value="x1">x1</option>
<option value="x2">x2</option>
</select><BR> <BR>
Y <BR>
<input type="text" name="Y" value="<?=$y;?>"> <BR> <BR>
<input id="sub" type="submit" value="Search" align="centre"/>
</form>
<!--other part of code!-->
</body>
</html>
Upvotes: 0
Views: 2993
Reputation: 24645
The select
html element doesn't have a value attribute, instead you set the selected attribute on the option elements:
<select name="X">
<option value="x1"<?= $_REQUEST["X"]=="x1"?" selected='selected'":"" ?>>x1</option>
<option value="x2"<?= $_REQUEST["X"]=="x2"?" selected='selected'":"" ?>>x2</option>
Upvotes: 0
Reputation: 988
You need to also add a selected
attribute to the option
that should be selected
<option value="x1" <?= ($x === 'x1' ? 'selected="selected"' : '')?> >x1</option>
<option value="x1" <?= ($x === 'x2' ? 'selected="selected"' : '')?> >x1</option>
Upvotes: 2