I have a very simle form, i wish the user to choose whether to sort ascending or decending. From the select form, I will use the answer to give the search results in required order. My problem is that the form is not giving a result to the page and both 'if' statements are satisfied. I am completely stumped. Can any one shed light? Thank you
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<label for="sort">Sort by:</label>
<select name="thesort">
<option value="Lowest">Lowest first</option>
<option value="Highest">Highest first</option>
</select>
</form>
<?php
if(isset($_POST["thesort"])){
echo "selection has been made";
}
?>
<?php if($_POST["thesort"]=="Highest"){ echo 'selected="selected"';}
{
echo "<p> choice is DESC </p>";
}
?>
<?php if($_POST["thesort"]=="Lowest"){ echo 'selected="selected"';}
{
echo "<p> choice is ASC </p>";
?>
Why double curly braces? PHP will execute the second one in ever case.
if($_POST["thesort"]=="Highest")
{ echo 'selected="selected"';}
{echo "<p> choice is DESC </p>";}
Your code modified:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<label for="sort">Sort by:</label>
<select name="thesort">
<option value="Lowest">Lowest first</option>
<option value="Highest">Highest first</option>
</select>
</form>
<?php
if(isset($_POST["thesort"])){
echo "selection has been made";
}
if($_POST["thesort"]=="Highest"){
echo 'selected="selected"';
echo "<p> choice is DESC </p>";
}
if($_POST["thesort"]=="Lowest"){
echo 'selected="selected"';
echo "<p> choice is ASC </p>";
}
?>
This is a problem:
<?php if($_POST["thesort"]=="Highest"){ echo 'selected="selected"';}
{
echo "<p> choice is DESC </p>";
}
?>
a) Those brackets aren't doing what I think you think they're doing. In particular, the second set are irrelevant; that code will always execute
b) Why are you echo'ing 'selected=..' here? It's not in the context of an open <option tag. For example, you probably want something like:
echo '<option value="Highest';
if ($_POST["thesort"]=="Highest")
{
echo ' selected="selected"';
}
echo '">Highest first</option>';
<?php
$sort = 'Lowest'; // define the default
if (isset($_POST['thesort']) && $_POST['thesort'] == 'Highest') {
$sort = 'Highest';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<label for="sort">Sort by:</label>
<select name="thesort">
<option value="Lowest"<?php if ($sort == 'Lowest') print(' selected="selected"'); ?>>Lowest first</option>
<option value="Highest"<?php if ($sort == 'Highest') print(' selected="selected"'); ?>>Highest first</option>
</select>
</form>
Related
I trying to make search option based on a SELECT box and user input (both are mandatory). But in the following code both correct and wrong input are displaying as wrong input. Can someone please explain what is wrong in the code.
Here is HTML
<form action="" method="POST">
<select name="selectOpt">
<option>Select a list</option>
<option value="one">ID</option>
<option value="delName">Dealer Name</option>
<option value="medName">Medical Name</option>
</select>
<input type="text" name="uinput" placeholder="Enter Search Key"/>
<button type="submit" name="submit">search</button>
</form>
PHP
if(isset($_POST['submit'])){
if(!empty($_POST['selectOpt']) && !empty($_POST['uinput'])){
if($_POST['selectOpt']=='one'){
$id = $_POST['selectOpt'];
if (!preg_match("/^[0-9]*$/",$id)){
echo "not valid";
}else{
echo "valid";
}
}
}else{
echo "Enter Value";
}
}
Your pattern /^[0-9]*$/, which is written to require a sequence of zero or more digits, doesn't match any of the possible values for your select box, which are all alphabetic strings.
<form action="save.php" method="POST">
<select name="selectOpt">
<option>Select a list</option>
<option value="one">ID</option>
<option value="delName">Dealer Name</option>
<option value="medName">Medical Name</option>
</select>
<input type="text" name="uinput" placeholder="Enter Search Key"/>
<button type="submit" name="submit">search</button>
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['selectOpt']) && !empty($_POST['uinput'])){
if($_POST['selectOpt'] == "one"){
$id = $_POST['selectOpt'];
if (preg_match("/^[0-9]*$/",$id)){
echo "not valid";
}else{
echo "valid";
echo $input = $_POST['uinput'];
}
}
else{
echo "PLEASE Enter Proper and Valid Search ";
}
}
else{
echo "PLEASE Enter Value ";
}
}
?>
So I have 2 drop down lists, I need to chose a year from them. Then I need to show the chosen year and the number of population for that year.
For example:
For year 1800: the population is : 3,929,214
For year 1900: the population is : 76,212,168
Population increased by: 72,282,954.
So here is my code until now:
<html>
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
<h1>Population Change Calculator</h1>
<?php
$population=[3929214,5236631,7239881,9638453,12866020,17069453,23191876,31443321];
$year=(range(1790, 1860, 10));
$array= array_combine($year, $population);
?>
<form method="post">
<p><label for="year1">Year 1:</label>
<select name="year1">
<option value=""></option>
<?php
foreach ($array as $year1=>$population){ ?>
<option value="<?php echo $year1; ?>"><?php echo $year1; ?></option>
<?php }
?>
</select>
</p>
<p>
<label for="year2">Year 2:</label>
<select name="year2">
<option value=""></option>
<?php
foreach ($array as $year2=>$population){ ?>
<option value="<?php echo $year2; ?>"><?php echo $year2; ?></option>
<?php }
?>
</select>
</p>
<input type="submit" name="submit" value="Submit">
<br>
</form>
<?php
$ini_set = ini_set('display_errors', 1);
error_reporting(E_ALL);
$empty=true;
if(isset($_POST['submit'])){
if(empty($_POST['year1'])){
$empty=FALSE;
print "<p>Please choose Year 1.</p>";
}
if(empty($_POST['year2'])){
$empty=false;
print "<p>Please choose Year 2.</p>";
}
}
I cant figure out how to print the chosen year and the population for that year.
I am new to PHP, thank you in advance.
It seems weird that you are setting the value of your 'year' options to the population. Perhaps this is a mistake..?
You should change them to use the year:
<option value="<?php echo $year1; ?>"><?php echo $year1; ?></option>
Once you do that, you can print your desired values by doing something like this:
if (isset($_POST['submit'])) {
if (empty($_POST['year1'])) {
$empty = FALSE;
print "<p>Please choose Year 1.</p>";
} else {
print "<h1>Year 1: {$_POST['year1']}</h1>";
print "<p>Population: {$array[$_POST['year1']]}</p>";
}
if (empty($_POST['year2'])) {
$empty = false;
print "<p>Please choose Year 2.</p>";
} else {
print "<h1>Year 2: {$_POST['year2']}</h1>";
print "<p>Population: {$array[$_POST['year2']]}</p>";
}
}
found this from one of the answers from this question
And I would like to ask, is there something wrong with this?
I've var_dump the column course right it displays BSIT.
But I couldn't get if the user is BSIT it goes to the BSCS selected option.
<select class="form-control" name="course">
<?php
if ($row['course'] == 'BSIT') {
echo "<option selected>BSIT</option>";
echo "<option>BSCS</option>";
} else {
echo "<option selected>BSCS</option>";
echo "<option>BSIT</option>";
}
?>
</select>
Actually you should declare variable for $row=['course']
<select class="form-control" name="course">
<?php
if ($course == 'Bachelor of Science in Information Technology') {
echo "<option value='Bachelor of Science in Information Technology' selected>Bachelor of Science in Information Technology</option>";
echo "<option value='Bachelor of Science in Computer Science'>Bachelor of Science in Computer Science</option>";
} else {
echo "<option value='Bachelor of Science in Computer Science' selected>Bachelor of Science in Computer Science</option>";
echo "<option value='Bachelor of Science in Information Technology'>Bachelor of Science in Information Technology</option>";
}
?>
</select>
I've tested it. It's Working
just use this code
<?php
// $row['course'] = 'BSCS';
?>
<select class="form-control" name="course" >
<option <?php if($row['course'] == 'BSIT' ) { echo "selected='selected'"; }?> >BSIT</option>
<option <?php if($row['course'] == 'BSCS' ) { echo "selected='selected'"; }?> >BSCS</option>
</select>
no need to check condition and change $row['course'] = 'BSIT'; for you test
Just switch the content of your curly brackets. Then BSCS will show up as selected when $row['course'] holds 'BSIT' and BSCS for anything else.
You have missed the select tag here. Also assign value to the option to retrieve it back
echo "<select>";
if ($row['course'] == 'BSIT') {
echo "<option selected>BSIT</option>";
echo "<option>BSCS</option>";
} else {
echo "<option selected>BSCS</option>";
echo "<option>BSIT</option>";
}
echo "</select>";
Instead of using echo each time the best practice is to store the content in a variable and echo it after
<?php
$msg="";
//retrieve the $row['course'] from the DB. Fetch the result and store
if ($row['course'] == 'BSIT') {
$msg.= "<option selected>BSIT</option>";
$msg.= "<option>BSCS</option>";
} else {
$msg.= "<option selected>BSCS</option>";
$msg.= "<option>BSIT</option>";
}
?>
<select class="form-control" name="course" >
<?php echo $msg;?>
</select>
I think this will more help to you. Please try this one
<select class="form-control" name="course" value="<?php echo $row['course']; ?>">
<option value="BSIT">BSIT</option>
<option value="BSCS">BSCS</option>
</select>
That coure value will be show in dropdown box..
I am doing PHP validations for my html values. However when PHP validation fails and I return back to the page, the select tag form data is cleared. Is there anyway to do save and reload the form data in php
<?php
$qualific=$passingyear="";
$qualificErr=$passingyearErr="";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
//qualification validations starts here
if(empty($_POST["qualif"]))
{
$qualificErr="* Qualification is Required";
$valid=false;
}
else
{
$qualific=test_input($_POST["qualif"]);
}
//qualification validations starts here
/*yearOfPassing validation starts here*/
if(empty($_POST["yearpass"]))
{
$passingyearErr="* Year Of Pass is Required";
$valid=false;
}
else
{
$passingyear=test_input($_POST["yearpass"]);
}
/*yearOfPassing validation starts here*/
//if valid then redirect
if($valid){
include 'database.php';
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';
exit;
}
}
<form method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Qualification<span class="error">*</span>:</label>
<select name="qualif">
<option label="Select"></option>
<option>Below SSC(10 Std)</option>
<option>SSC(10 Std) passed</option>
<option>HSC(12 Std) passed</option>
<option>Graduate</option>
<option>Post Graduate</option>
</select>
<span class="error"><?php echo $qualificErr?></span> <br />
<br />
<label>Year of passing<span class="error">*</span>: </label>
<select name="yearpass">
<option label="Select"></option>
<option>1975</option>
<option>1976</option>
<option>1977</option>
</select>
</form>
try <?php if($_POST["qualif"] == "<value>") echo "selected"; ?> in each option tag.
like
<option <?php if($_POST["qualif"] == "Below SSC(10 Std)") echo "selected"; ?>>Below SSC(10 Std)</option>
Currently you are using <option> without values.
Use like :
<option value="SSC" <?php if(isset($_POST['qualif']) && $_POST['qualif'] == 'SSC') { echo "selected"; } ?> >SSC</option>
your are missing value parameter inside <option>.
you need to add some code inside <option> tag
Please try following code hope this will solve the issue.
<select name="qualif">
<option label="Select"></option>
<option value="Below SSC(10 Std)" <?php if($qualific == 'Below SSC(10 Std)') {?> selected <?php } ?>>Below SSC(10 Std)</option>
<option value=">SSC(10 Std) passed" <?php if($qualific == '>SSC(10 Std) passed') {?> selected <?php } ?>>SSC(10 Std) passed</option>
<option value="HSC(12 Std) passed" <?php if($qualific == 'HSC(12 Std) passed') {?> selected <?php } ?>>HSC(12 Std) passed</option>
<option value="Graduate" <?php if($qualific == 'Graduate') {?> selected <?php } ?>>Graduate</option>
<option value="Post Graduate" <?php if($qualific == 'Post Graduate') {?> selected <?php } ?>>Post Graduate</option>
</select>
<span class="error"><?php echo $qualificErr?></span> <br />
<br />
<label>Year of passing<span class="error">*</span>: </label>
<select name="yearpass">
<option label="Select"></option>
<option value="1975" <?php if($passingyear == '1975') {?> selected <?php } ?>>1975</option>
<option value="1976" <?php if($passingyear == '1976') {?> selected <?php } ?>>1976</option>
<option value="1977" <?php if($passingyear == '1977') {?> selected <?php } ?>>1977</option>
</select>
You should use jquery
<script type="text/javascript">
$(document).ready(function()
{
$("#id").val("<?php echo $_POST['qualif']; ?>");
});
</script>
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"