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 ";
}
}
?>
Related
If the combobox select the Archers the browser will echo Correct but if the combobox will not select the Archers the browser will echo, i want to know if my syntax is wrong.
Wrong. Error: Undefined index text_hname.
Here's the structure:
<form action="server.php" method="POST">
<img src="images/logo.png" class="logo">
<div class="container">
<h1>Account Details</h1>
<br>
<label class="username">Username</label>
<input type="text" name="text_username" class="text_user" placeholder="Enter username">
<label class="password">Password</label>
<input type="text" name="text_password" class="text_pass" placeholder="Enter password">
<label class="email">Email</label>
<input type="text" name="text_email" class="text_email" placeholder="Enter email">
<label class="cname">Character Name</label>
<input type="text" name="text_cname" class="text_cname" placeholder="Enter Character Name">
<label class="character">Select Character</label>
<select class="names" name="text_hname">
<option>Archer</option>
<option>Barbarian</option>
<option>Balloon</option>
<option>Witch</option>
<option>Spirit</option>
<option>Hog Rider</option>
<option>Minion</option>
</select>
<img src="images/">
<br>
<input type="submit" class="submit" name="submit">
</div>
</form>
//option
$hero = $_POST['text_hname'];
if (isset($_POST['text_hname'])) {
if ($hero == 'Archers') {
echo "Correct";
} else {
echo "wrong";
}
}
The problem is that if you're trying to assign $hero before you've checked if text_hname is set, it might not be defined.
Suggested refactor:
if (isset($_POST['text_hname'])) {
$hero = $_POST['text_hname'];
if ($hero == 'Archers') {
echo "Correct";
}
else
{
echo "wrong";
}
}
//Your select in form needs values. Same values that you are going to compare.
<select class="names" name="text_hname">
<option value="Archer">Archer</option>
<option value="Barbarian">Barbarian</option>
<option value="Balloon">Balloon</option>
<option value="Witch">Witch</option>
<option value="Spirit">Spirit</option>
<option value="HogRider">Hog Rider</option>
<option value="Minion">Minion</option>
</select>
//Php code
if (isset($_POST['text_hname'])) {
$hero = $_POST['text_hname'];
if ($hero == 'Archer') { //Its Archer, not Archers
echo "Correct";
}
else
{
echo "wrong";
}
}
$_POST['text_hname'] will return the selected option value , not option name .
Modify your select like this:
<select class="names" name="text_hname">
<option value="Archer">Archer</option>
<option value="Barbarian">Barbarian</option>
<option value="Balloon">Balloon</option>
<option value="Witch">Witch</option>
<option value="Spirit">Spirit</option>
<option value="Hog_Rider">Hog Rider</option>
<option value="Minion">Minion</option>
</select>
and server.php page
if (isset($_POST['text_hname'])) {
$hero = trim($_POST['text_hname']); //** to remove any whitespace
if ($hero == 'Archer') {
echo "Correct";
} else {
echo "wrong";
}
}
EDIT : it might be because your $_POST['text_hname'] has whitespace. Try trim() on $_POST['text_hname']
Hope it's helpful.
I've written this code... but this fails to display text... not sure what's wrong with the code. I'm new to PHP and trying to create a page that gets customer details and puts it in SQL.
<!DOCTYPE html>
<html>
<body>
<?php
$initial="";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
echo "Customer Intial: $initial <br>";
?>
</body>
</html>
Try this:
$initial="";
if(isset($_POST['initial'])){
$initial=htmlentities($_POST['initial']);
}
You correctly sent the POST however you didn't put the value given with the POST into the $initial variable.
<!DOCTYPE html>
<html>
<body>
<?php
$initial = "";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
$initial = filter_input(INPUT_POST, "initial");//GET the input in post method
if ($initial == 1) {
$initial_value = "Mr.";
} elseif ($initial == 2) {
$initial_value = "Mrs.";
} elseif ($initial == 3) {
$initial_value = "Ms.";
} elseif ($initial == 4) {
$initial_value = "M/s";
} else {
$initial_value = "Select initial";
}
echo "Customer Intial: $initial_value <br>";
?>
</body>
</html>
Try,
<form method="post" action=<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>>
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<br>
<br>
<?php
if(isset($_POST['initial'])){
$initial=$_POST['initial'];
} else {
$initial = "empty";
}
echo "Customer Intial : ".$initial;
?>
</body>
</html>
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"
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>