The following script would let the user to chose between on and off. The problem is that the selected value does not stay selected after refreshing the page. Any idea how to fix this?
<form action="Conf.php" name="Calculation" method="post">
<?php
if (isset($_POST['Submit1']))
{
$Wave = $_POST['WaveHMap'];
}
?>
<?php
$attr = 'selected="selected"';
?>
Wave Height Maps:
<select selected="selected" name="WaveHMap" VALUE="<?PHP print $Wave ; ?>">
<?php $attr= 'selected="selected"'; ?>
<option VALUE="On"<?php echo $Wave == 'On' ? $attr : ''; ?>>On</option>
<option VALUE="Off"<?php echo $Wave== 'Off' ? $attr : ''; ?>>off</option>
</select>
<Input Type="Submit" Name="Submit1" Value="Save Parameters">
</form>
Try this
<select name="WaveHMap" >
<option VALUE="On" <?php echo $Wave == 'On' ? 'selected' : ''; ?>>On</option>
<option VALUE="Off" <?php echo $Wave== 'Off' ? 'selected': ''; ?>>off</option>
</select>
Related
I have a form which a user fills up and hits "Save & Next" which will take them to another page where the user can upload images and hit "Final Submit". They can also go back to the previous page to edit the data.
At that time, all the data he had previously filled up should be displayed on the text box. I have used session variable to store the data and display it.
I am stuck in the drop down box.
<select name="District">
<option value="East">East</option>
<option value="West">West</option>
<option value="North">North</option>
<option value="South">South</option>
</select>
When the user submits, I am storing the selected value in Session $_SESSION['District'] = $_POST['District']; and when the user clicks back to go the previous page, I need to auto select that option value in the drop down.
How can I achieve this?
Using array would make it easier.
<?php
$options = array(
'East', 'West', 'North', 'South',
);
?>
<select name="District">
<?php foreach($options as $option) { ?>
<option value="<?php echo $option; ?>" <?php echo (isset($_SESSION['District']) && $_SESSION['District'] == $option) ? "selected" : "" ?>><?php echo $option; ?></option>
<?php } ?>
</select>
Check value in session and if matches set the attibute selected
<select name="District">
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'East'){ echo 'selected'; }?> value="East">East</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'West'){ echo 'selected'; }?> value="West">West</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'North'){ echo 'selected'; }?> value="North">North</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'South'){ echo 'selected'; }?> value="South">South</option>
</select>
Just check the selected value against the one stored in session.
Your option will look like this:
<option value="East" <?php echo ($_SESSION['District']=="East" ? "selected" : ""; ?>>East</option>
And the right one will be selected
<select name="District">
<option value="East" <?php if($_SESSION['District'] == "East"):?>selected="selected"<?php endif; ?>>East</option>
... Repeat with all options ...
Use below code:
<select name="District">
<option value="East" <?php echo ($_SESSION['District'] == "East") ? "selected" : "" ?>>East</option>
<option value="West" <?php echo ($_SESSION['District'] == "West") ? "selected" : "" ?>>West</option>
<option value="North" <?php echo ($_SESSION['District'] == "North") ? "selected" : "" ?>>North</option>
<option value="South" <?php echo ($_SESSION['District'] == "South") ? "selected" : "" ?>>South</option>
</select>
<option value="East" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'East' ? 'selected="selected"' :'' ;?> >East</option>
<option value="West" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'West' ? 'selected="selected"' :'' ;?>>West</option>
<option value="North" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'North' ? 'selected="selected"' :'' ;?>>North</option>
<option value="South" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'South' ? 'selected="selected"' :'' ;?>>South</option>
I have got the following form:
<form action="" method="get" target="_self">
<select name="Category" class="dropmenu" id="Category"onchange="this.form.submit()">
<option value="">Any</option>
<option value="Keyboard"<?php if ($_GET['Category']=="Keyboard") {echo "selected='selected'"; } ?>>Keyboard</option>
<option value="Piano"<?php if ($_GET['Category']=="Piano") {echo "selected='selected'"; } ?>>Piano</option>
</select>
<select name='Manufacturer' class="dropmenu" onChange="this.form.submit()" >
<?php
echo '<option value="">Any</option>';
while ($row = mysql_fetch_array($RS_Search1)) {
$selected = $_GET['Manufacturer'] == $row['Manufacturer'] ? 'selected' : '';
echo '<option '.$selected.'>' . $row['Manufacturer'] . '</option>';
} ?> </select>
<select name="Color" class="dropmenu" onChange="this.form.submit()">
<?php
echo '<option value="">Any</option>';
while ($Color = mysql_fetch_array($RS_Search2)) {
$selected2 = $_GET['Color'] == $Color['Color'] ? 'selected' : '';
echo '<option '.$selected2.'>' . $Color['Color'] . '</option>';
} ?> </form>
Second form to submit
<form action="search2.php" method="get"> </select><input name="Category" type="hidden" value="<?php echo $_GET['Category']; ?>">
<input name="Manufacturer" type="hidden" value="<?php echo $_GET['Manufacturer']; ?>">
<input name="Color" type="hidden" value="<?php echo $_GET['Color']; ?>">
<select name="price" class="dropmenu">
<?php
echo '<option value="">Any</option>';
while ($Price = mysql_fetch_array($RS_Price)) {
$selected3 = $_GET['price_range'] == $Price['price_range'] ? 'selected' : '';
echo '<option '.$selected3.'>' . $Price['price_range']. '</option>';
} ?>
</select><input name="Search2" type="submit" id="Search2" value="Submit">
</form>
As you can see the option values are pulled from a db, after the first value is chosen. What the form does at the minute is reload the whole page. Is there a way to just reload the form in stead of reloading the whole page. I need to keep the values that are submitted on change of a option value to be able to fill in the next drop down menu.
Any help welcome.
<form name="search" method="post" >
Seach for: <input type="text" name="find" value="<?php echo (isset($_POST['find']) ? $_POST['find'] : ''); ?>" /> in
<Select NAME="field">
<Option VALUE="category1" <?php echo (isset($_POST['field']) && $_POST['field'] === 'category1') ? 'selected="selected"': ''; ?>>category1</option>
<Option VALUE="category2" <?php echo (isset($_POST['field']) && $_POST['field'] === 'category2') ? 'selected="selected"': ''; ?>>category2</option>
</Select>
<input type="submit" name="search" value="Search" />
</form>
<?php
if(!empty($_POST)){
$options = array('category1'=> array('1' => 'dog', '2' => 'cat'), 'category2' =>array('1'=>'flower', '2'=>'grass'));
$input = trim($_POST['find']);
$category = $_POST['field'];
$output = $options[$category][$input];
echo $output;
}
?>
Question:
How could I make category2 to be the default value for the select box instead of category1? I tried this:
<Option VALUE="category2" <?php
echo (isset($_POST['field']) && $_POST['field'] === 'category2') ? 'selected="selected"' : '';
?> selected = "selected">category2</option>
but it breaks my function. When I input 1 and select category1, after I clicked search, the select box changed to category2. That is not what I want. I want this function to perform like this:
Remember the values that user make for input field and select box.
Set the default value category2 for the select box.
How could I achieve this?
try default will be cat2 and after selected will be result
<form method="post">
<Select name="field">
<Option <?php if(isset($_POST['field']) && $_POST['field'] == "category1") echo 'selected="selected"'; ?> VALUE="category1" >category1</option>
<Option <?php if ((isset($_POST['field']) && $_POST['field'] == "category2") OR empty($_POST['field'])) echo 'selected="selected'; ?> VALUE="category2" >category2</option>
</Select>
<input type="submit" />
</form>
u may use like this
<form method="post">
<Select name="field">
<Option VALUE="category2" >category2</option>
<Option <?php if($_POST['field'] == "category1") echo 'selected="selected"'; ?> VALUE="category1" >category1</option>
<Option <?php if($_POST['field'] == "category3") echo 'selected="selected"'; ?> VALUE="category3" >category3</option>
</Select>
<input type="submit" />
</form>
Once submitted selected option, the data is not stored.
just want to know how to post back the data if validation fails
The following line doesnt really work for me.
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
if someone could give me a hand?
Many thanks, here is my code
<?php
if(isset($_POST['numbers']) &&($_POST['fruits']) && $_POST['numbers'] != "null" && $_POST['fruits'] !== "null")
{
echo "Thank you!";
} elseif (isset($_POST['numbers']) && $_POST['numbers'] = "null") {
echo "you forgot to choose a number";
}
elseif(isset($_POST['fruits']) && $_POST['fruits'] = "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" value="<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : " "; ?>"/>
<option value="null" selected="selected">-</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<select id="fruits" name="fruits" value="<?php echo (isset($_POST['fruits']))? $_POST['fruits'] : ''; ?>"/>
<option value="null" selected="selected">-</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Pear">Pear</option>
</select>
<input type="submit" value="Send" />
</form>
Solved it, Maybe not a best way, but at least got it sorted:
<?php
$item = null; #
$itemyear = null;
if(isset($_POST['numbers'])){
$item = $_POST['numbers'];
}
if(isset($_POST['fruits'])){
$itemyear = $_POST['fruits'];
}
if(isset($item) && isset($itemyear) && $item != "null" && $itemyear !== "null")
{
echo "Thank you!";
} elseif ($item == "null") {
echo "you forgot to choose a number";
}
elseif($itemyear == "null")
{
echo "you forgot to choose fruit name";
}
?>
<form id="form" name="form" method="post" action="">
<label for="expiry">Select</label>
<select id="numbers" name="numbers" />
<option value="null" selected="selected">-</option>
<option value="01" <?php if($item == '01'): echo "selected='selected'"; endif; ?>>01</option>
<option value="02" <?php if($item == '02'): echo "selected='selected'"; endif; ?>>02</option>
<option value="03" <?php if($item == '03'): echo "selected='selected'"; endif; ?>>03</option>
</select>
<select id="fruits" name="fruits" />
<option value="null" selected="selected">-</option>
<option value="Apple"<?php if($itemyear == 'Apple'): echo "selected='selected'"; endif; ?> >Apple</option>
<option value="Banana"<?php if($itemyear == 'Banana'): echo "selected='selected'"; endif; ?>>Banana</option>
<option value="Pear"<?php if($itemyear == 'Pear'): echo "selected='selected'"; endif; ?>>Pear</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
echo $item ."-". $itemyear;
?>
the PHP isset() function returns either true or false (depending on whether the input is.. well... set.
You would want to use this:
value='<?php echo (isset($_POST['numbers'])) ? $_POST['numbers'] : ""; ?>
You can't set a value attribute on a <select>. You have to find the correct <option> and set its selected attribute. Personally, I put a data-default attribute on the <select> and then use JavaScript to loop through the options and find the right one.
Oh now I see.
I don't know what the usual thing to do is but one way is to put all the data as a query string when you redirect the user back. The reason why it doesn't stay in the $_POST global is because it's only kept on the page you post to then it's gone.
So when you redirect the user back
if(validationFailed)
{
header("Location: page.php?data=example");
}
The data can then be retrieved in page.php by using
$data = $_GET['data']; // contains "example"
I have a form with 'selected' values pulled from the database.
Now I want the user to edit the values.
When the data is send I want to show the new values.
When I submit my form I always get the 'green' value?
What am I doing wrong here?
<?php
// pulled from db
$color = "blue";
// update
if (isset($_POST['Submit'])) {
echo "write to db: " . $_POST['name'] . " + " . $_POST['color'];
}
?>
<html>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="name">Name:</label>
<input type="text" name="name" size="30" value="<?php echo (isset($_POST['name'])) ? $_POST['name'] : ""; ?>">
<br />
<label for="color">Color:</label>
<select name="color">
<option <?php echo (isset($_POST['color']) || $color == "red") ? 'selected="selected"' : ''; ?> value="red">red</option>
<option <?php echo (isset($_POST['color']) || $color == "blue") ? 'selected="selected"' : ''; ?> value="blue">blue</option>
<option <?php echo (isset($_POST['color']) || $color == "green") ? 'selected="selected"' : ''; ?> value="green">green</option>
</select>
<br />
<input type="submit" name="Submit" value="Update">
</form>
</html>
Your conditionals all use ||. They all evaluate to TRUE when the post is set. If you look at the HTML output, every option will say selected='selected'.
Just compare $_POST['color'] to your specified string.
<option <?php echo (isset($_POST['color']) || $color == "red") ? 'selected="selected"' : ''; ?> value="red">red</option>
|| is the "or" operator. If $_POST['color'] is set (i.e., the form was submitted), that will always evaluate to true. You should probably just do
$_POST['color'] == 'red'
Instead. Forget the isset check.