I have started with PHP and HTML and would like to execute simple code with three drop-down lists (select tag). But when I make selection from one, the other two selected values disappear. Should I save values when submitting them and then echo. If yes, how can I do it. Sample code will be appreciated.
<?php // formtest_2.php
$name_vid = $name_type = $name_model = "";
if (isset($_POST['vid']))$name_vid=($_POST['vid']);
else $name_vid="no";
if (isset($_POST['type']))$name_type=sanitizeString($_POST['type']);
else $name_type="no";
if (isset($_POST['model']))$name_model=sanitizeString($_POST['model']);
else $name_model="no";
echo <<<_END
<html>
<head>
<title>Form Test Two</title>
</head>
<body>
Make your selection:
<form action="formtest_2.php" method="post">
<select name="vid" size="1">
<option value="" selected>Product</option>
<option value = "apple">apple</option>
<option value = "cherry">cherry</option>
<option value = "orange">orange</option>
</select>
<input type="submit"/><br />
<select name="type" size="1">
<option value="" selected>Color</option>
<option value = "green">green</option>
<option value = "red">red</option>
<option value = "orange">orange</option>
</select>
<input type="submit"/><br />
<select name="model" size="1">
<option value="" selected>Model</option>
<option value = "1-a">1-a</option>
<option value = "2-b">2-b</option>
<option value = "3-c">3-c</option>
</select>
<input type="submit"/><br />
</form>
</body>
</html>
_END;
echo "Your selection is: $name_vid and $name_type and $name_model";
function sanitizeString($var)
{
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
?>
I think you need to re-select the values of the drop downs after submission. The following code would be a fix.
<?php // formtest_2.php
$name_vid = $name_type = $name_model = "(nothing)";
if (isset($_POST['vid']) && $_POST['vid']) $name_vid=($_POST['vid']);
if (isset($_POST['type']) && $_POST['type']) $name_type=sanitizeString($_POST['type']);
if (isset($_POST['model']) && $_POST['model']) $name_model=sanitizeString($_POST['model']);
?>
<html>
<head>
<title>Form Test Two</title>
</head>
<body>
Make your selection:
<form action="formtest_2.php" method="post">
<select name="vid" size="1">
<option value="">Product</option>
<option value = "apple" <?php if($name_vid == 'apple') echo 'selected="selected"'; ?>>apple</option>
<option value = "cherry" <?php if($name_vid == 'cherry') echo 'selected="selected"'; ?>>cherry</option>
<option value = "orange" <?php if($name_vid == 'orange') echo 'selected="selected"'; ?>>orange</option>
</select>
<br />
<select name="type" size="1">
<option value="">Color</option>
<option value = "green" <?php if($name_type == 'green') echo 'selected="selected"'; ?>>green</option>
<option value = "red" <?php if($name_type == 'red') echo 'selected="selected"'; ?>>red</option>
<option value = "orange" <?php if($name_type == 'orange') echo 'selected="selected"'; ?>>orange</option>
</select>
<br />
<select name="model" size="1">
<option value="">Model</option>
<option value = "1-a" <?php if($name_model == '1-a') echo 'selected="selected"'; ?>>1-a</option>
<option value = "2-b" <?php if($name_model == '2-b') echo 'selected="selected"'; ?>>2-b</option>
<option value = "3-c" <?php if($name_model == '3-c') echo 'selected="selected"'; ?>>3-c</option>
</select>
<input type="submit"/><br />
</form>
</body>
</html>
<?php
echo "Your selection is: $name_vid and $name_type and $name_model";
function sanitizeString($var)
{
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
?>
I would not use heredoc syntax ("<<<") to print HTML.
Try this out, i made one submit button, and re selected the values after submit :
<?php
// formtest_2.php
$name_vid = $name_type = $name_model = "";
if (isset($_POST['vid']))
$name_vid = ($_POST['vid']);
else
$name_vid = "no";
if (isset($_POST['type']))
$name_type = sanitizeString($_POST['type']);
else
$name_type = "no";
if (isset($_POST['model']))
$name_model = sanitizeString($_POST['model']);
else
$name_model = "no";
///////////////////
if($name_vid == 'apple')
{
$v1 = 'selected';
}
elseif($name_vid == 'cherry')
{
$v2 = 'selected';
}
elseif($name_vid == 'orange')
{
$v3='selected';
}
////////////////////////
if($name_type == 'green')
{
$t1 = 'selected';
}
elseif($name_type == 'red')
{
$t2 = 'selected';
}
elseif($name_type == 'orange')
{
$t3='selected';
}
///////////////////////
if($name_model == '1-a')
{
$m1 = 'selected';
}
elseif($name_model == '2-b')
{
$m2 = 'selected';
}
elseif($name_model == '3-c')
{
$m3='selected';
}
?>
<html>
<head>
<title>Form Test Two</title>
</head>
<body>
Make your selection:
<form name="fruite_form" action="formtest_2.php" method="post">
<select name="vid" size="1">
<option value="" selected>Product</option>
<option <?=$v1?> value = "apple">apple</option>
<option <?=$v2?> value = "cherry">cherry</option>
<option <?=$v3?> value = "orange">orange</option>
</select>
<select name="type" size="1">
<option value="" selected>Color</option>
<option <?=$t1?> value = "green">green</option>
<option <?=$t2?>value = "red">red</option>
<option <?=$t3?>value = "orange">orange</option>
</select>
<select name="model" size="1">
<option value="" selected>Model</option>
<option <?=$m1?> value = "1-a">1-a</option>
<option <?=$m2?>value = "2-b">2-b</option>
<option <?=$m3?>value = "3-c">3-c</option>
</select>
<input name="submitButtom" id="submitButton" type="submit"/><br />
</form>
</body>
</html>
<?
echo "Your selection is: $name_vid and $name_type and $name_model";
function sanitizeString($var)
{
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
?>
Feel free top add back your submit buttons.
But give each a unique name and also don't forget to name your form, and as a good practice, give then an id too which will be the same as name attribute.
Update: Just to make sure you are really getting a post, since this is your initial question, print this at the end of your code
echo $_POST['vid'] . ' ' . $_POST['type'] . ' ' . $_POST['model'] . '<br/><br/>';
and see if post really holds the value after submission, if you get a value from post, but not from your assigned variables, then check your conditions used at the top to assign your values.
// you need to check if post isset and has an actual value, only than, the varibale should be assigned. Just like what Sithu said.
Related
So I am making a programme and I am submitting a form to itself. This programme is a very basic calculator as I am new to HTML and PHP. I am trying to make it so that when you submit the form, the Select dropdown will remain on the most recently used operator.
For example, if I make the calculator do '5 + 5', then I want the submitted form to keep the operator dropdown on '+'.
Here is my code:
<?php
// grab the form values from $_HTTP_GET_VARS hash extract($_GET);
// first compute the output, but only if data has been input if(isset($calc) && $operator == "multiply") { // $calc exists as a variable
$prod = $x * $y; } elseif (isset($calc) && $operator == "plus") {
$operator = $plus;
$prod = $x + $y; } elseif (isset($calc) && $operator == "minus") {
$operator = "minus";
$prod = $x - $y; } elseif (isset($calc) && $operator == "divide") {
$sign = "/";
$prod = $x / $y; } else { // set defaults
$x=0;
$y=0;
$prod=0; } ?>
<html> <head>
<title>PHP Calculator Example</title> </head>
<body>
<h3>PHP Calculator (Version 1)</h3>
<p>Multiply two numbers and output the result</p>
<form method="get" action="<?php print $_SERVER['PHP_SELF']; ?>">
<label for="x">x = </label>
<input type="text" name="x" id="x" size="5" value="<?php print $x; ?>"/>
<select name="operator" id="operator">
<option value="plus">+</option>
<option value="minus">-</option>
<option value="divide">/</option>
<option value="multiply">*</option>
</select>
<label for="y">y = </label>
<input type="text" name="y" id="y" size="5" value="<?php print $y; ?>"/>
<input type="submit" name="calc" value="Calculate"/>
</form>
<!-- print the result -->
<?php if(isset($calc)) {
print "<p>x $sign y = $prod</p>";
} ?>
</body> </html>
In order to remain on option selected in the select element
The option has to have an attribute "selected"
<select name="operator" id="operator">
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "plus"){echo "selected";} ?> value="plus">+</option>
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "minus"){echo "selected";} ?> value="minus">-</option>
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "divide"){echo "selected";} ?> value="divide">/</option>
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "multiply"){echo "selected";} ?> value="multiply">*</option>
</select>
Hope that it will help you.
A possible solution employing javascript would be this
<?php
if(isset($_GET['operator'])) {
?>
<script>document.getElementById("operator").value = '<?=$_GET['operator']?>';</script>
<?php
}
?>
Html Form:
<form>
<select name="country[]" id="country" multiple>
<option value="any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
</form>
PHP Code
<?php
$country = $_REQUEST['country'];
if($country=="")
$countrysql = "";
else
{
if($country == "Any") $countrysql = "";
else
{
$country = str_replace(",","','",$country);
$countrysql = " and Country in ('$country')";
}
}
$queryString = "SELECT * FROM register where $countrysql";
?>
I have created a form in PHP and I want to search multiple options. I already created table Register and a column Country. I am getting the result If I give single value. If I give multiple I am not getting the result. Please help.
You evaluate in if($country == "Any") the word Any is not equal to any in option <option value="any">any</option>
But I suggest this php code:
<?php
$country="";
$countryError="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["country"])){
$countryError = "Country is required";
}else{
$country = $_POST["country"];
}
if($country == "Any") {
$queryString = "SELECT * FROM register";
}else{
$queryString = "SELECT * FROM register where Country in ('$country')";
}
// Print the SQL string:
echo $queryString;
}
?>
The html tags:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="country" id="country" multiple>
<option value="Any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<span class="error"><?php echo $countryError;?></span>
i want to change tag option based on values coming from database.if value is "1" i want to show different options and different for other values.
<<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
Because
No closing ; on here <option value='4'>Maths</option></select>"
and here too <option value='8'>Maths</option></select>"
First if() condition missing end }.
So final code is
<!DOCTYPE html>
<html>
<head>
<title>select</title>
</head>
<?php
if(isset($_POST['submit']))
{
$number = $_POST['number'];
//echo $number;
if ( $number == "1" )
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
<body>
<form method="post" action="select.php">
<input type="text" name="number" value="">
<input type="submit" name="submit" value="submit">
</form>
</select>
</body>
</html>
You are missing the ; off of the end of both echos:
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>";
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>";
}
}
?>
Note: Sorry about poor layout, quick posting of answer!
if(isset($_POST['submit']))
{
$number=$_POST['number'];
//echo $number;
if ($number=="1")
{
echo "<select name='subject'>
<option value='1'>chemistry</option>
<option value='2'>Physics</option>
<option value='3'>Biology</option>
<option value='4'>Maths</option></select>"; //missing ; here
}
else
{
echo "<select name='subject'>
<option value='5'>english</option>
<option value='6'>computer</option>
<option value='7'>Biology</option>
<option value='8'>Maths</option></select>"; //missing ; here
}
} // missing } here
I want to store the onchange value of option and store to a php variable
<select class="form-control" name="users" >
<option value="#">Please Select Your Employee</option>
<?php while($rows = mysql_fetch_row($res)){ ?>
<option value="<?php echo $rows[0]; ?>">
<?php echo "\r\n $rows[0]";}?>
</option>
</select>
Try This:
<html>
<head>
<script type="text/javascript">
function myfunction(){
a = document.getElementById('myselect').value;
location.href="?a="+a;
}
</script>
</head>
<body>
<select id="myselect" onchange="myfunction()">
<option value="10" <?php if($_GET['a']== 10 ) echo "selected"; ?> > value 10 </option>
<option value="20" <?php if($_GET['a']== 20 ) echo "selected"; ?> > value 20 </option>
<option value="30" <?php if($_GET['a']== 30 ) echo "selected"; ?> > value 30 </option>
</select>
<?php
if (isset($_GET['a'])) {
$val = $_GET['a'];
echo $val;
}
else{
$val = 0;
echo $val;
}
?>
</body>
</html>
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"