PHP form values after POST in dropdown - php

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.

Related

PHP keep dropdown value after submit

I have the following code for a simple calculator. The code works fine, but I want the value in the dropdown list to stay there after submitted. How would I do this?
Essentially, I want the operator to stay selected once the calculation has been done. At the moment, it just shows a '+', even if the sum is 25 / 5.
<?php
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$operation = $_POST['operator'];
Switch ($operation) {
case 'add': $answer = $number1 + $number2;
break;
case 'minus': $answer = $number1 - $number2;
break;
case 'divide': $answer = $number1 / $number2;
break;
case 'multiply': $answer = $number1 * $number2;
break;
}
?>
<form name='calculator' method='post' action=''>
<table>
<tr>
<td>
<input name="number1" type="text" value="<?php i if(isset($_POST['number1'])) { echo htmlentities($_POST['number1']);}?>" />
</td>
<td>
<select name="operator">
<option value="add">+</option>
<option value="minus">-</option>
<option value="divide">/</option>
<option value="multiply">x</option>
</select>
</td>
<td>
<input name="number2" type="text" value="<?php if(isset($_POST['number2'])) { echo htmlentities($_POST['number2']);}?>" />
</td>
<td>
<input name="submit" type="submit" value="=" />
</td>
<td>
<input name="" type="text" value="<?php echo $answer ?>" />
</td>
</tr>
</table>
You have to set the selected attribute for the option that was submitted. So you have to check the submitted value for each option. In my solution I am using the ternary operator to echo the selected attribute only for the correct operator.
<select name="operator">
<option value="add" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'add') ? 'selected' : ''; ?>>+</option>
<option value="minus" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'minus') ? 'selected' : ''; ?>>-</option>
<option value="divide" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'divide') ? 'selected' : ''; ?>>/</option>
<option value="multiply" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'multiply') ? 'selected' : ''; ?>>x</option>
</select>
The code above is somewhat repetitive. It keeps repeating a lot of code and html. It would be great if we could factor out the repetitive stuff. Luckily we can do that by creating an array that stores the options and loop through them using a foreach, like this:
<?php
$options = [
'add' => '+',
'minus' => '-',
'divide' => '/',
'multiply' => 'x'
];
?>
<select name="operator">
<?php foreach ($options as $key => $label) { ?>
<option value="<?= $key ?>" <?= (isset($_POST['operator']) && $_POST['operator'] == $key) ? 'selected' : '' ?>><?= $label ?></option>
<?php } ?>
</select>

Dropdown menu does not stay selected via php after refreshing the page

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>

Php/Html form with different option selection

my php and html coding is:
$damt2 = isset($_POST['damt2']) ? $_POST['damt2'] : 200;
$dateinfo2 = json_decode(file_get_contents($_SESSION['base_path'] . 'dl.dict'), true);
arsort($dateinfo2); //arsort — Sort an array in reverse order and maintain index association
$dateinfo2 = array_slice($dateinfo2, 0, $damt2); //array_slice — Extract a slice of the array
Table
<input type="hidden" name="userId" value="<?= $_SESSION['userId']; ?>">
<select name="damt2" onchange="this.form.submit()">
<option value="50" <?= (isset($damt2) & $damt2 == 50) ? 'selected' : ''; ?>>50</option>
<option value="100" <?= (isset($damt2) & $damt2 == 100) ? 'selected' : ''; ?>>100</option>
<option value="200" <?= (isset($damt2) & $damt2 == 200) ? 'selected' : ''; ? >>200</option>
<input name="radio" type="radio" value="cow">Cow
<input name="radio" type="radio" value="chicken">Chicken
</select> <input type="submit" name="submit" value="Show List & Save">
<?php
foreach ($dateinfo2 as $key => $time){
$uInfo = $_SESSION['data']->table('units')->byName($key)->data();
if ($uInfo["keyword"] != 'chicken') continue;
if ($uInfo["keyword"] != 'cow') continue;
if (isset($uInfo['type']) && !empty($uInfo['type'])) {
echo '<center><font color="olive">TypE: '.$uInfo['type'].'</font><br>';
}else{
}
}
?>
Why in results all blank ?
I guess where i'm doing mistake :
In foreach loop by both values are continued.
In category selection with radio button.
I want form with two options ( one with search number 50,100,200 ) and second with category like " cow " and " chicken " .
My question is solved now and i did it by reading several posts from all around the internet and at the end i tried :
IN HTML section:
<input name="cow" type="radio" >Cow
<input name="chicken" type="radio" >chicken
PHP coding :
<?php
foreach ($dateinfo2 as $key => $time)
{
$uInfo = $_SESSION['data']->table('units')->byName($key)->data();//item get data by name
if(isset($_POST['cow'])){
if ($uInfo["keyword"] != 'cow') continue;
}
if(isset($_POST['chicken'])){
if ($uInfo["keyword"] != 'chicken') continue;
}
if (isset($uInfo['type']) && !empty($uInfo['type'])) {
echo '<center><font color="olive">TypE: '.$uInfo['type'].'</font><br>';
}else{
}
}
In the results, i have two radio buttons and pulldown menu to select length(50,100,200..etc) of result with desire category (Chicken/Cow).
thank you all for trying to support me with your suggestions. Have good day to all ^_^

Issue with setting the default value and remembering the values in select box in PHP

<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>

Post back the data if validation fails for "Select"

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"

Categories