PHP validation resets the form fields - php

if(isset($_POST['submit'])){
$domain=$_POST['domain'];
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$tel=$_POST['tel'];
if($domain==""){
$error="<h4>Enter Domain </h4>";
}elseif($fname == ""){
$error="<h4>Enter Firstname </h4>";
}elseif($sname == "")
{
$error="<h4 >Enter Surname</h4>";
}elseif($tel=="")
{
$error="<h4 >Enter telephono no</h4>";
}
else {
$sql11=mysql_query("INSERT INTO domain VALUES('','$domain','$fname','$sname','$tel','$mobile','$email','$company','$address','$city','$country','$pcode','$tele',
'$fax','$qus','$ans')");
echo $sql;
$db->query($sql);
}
}
<div><?php echo $error; ?></div>
<form action="" method="post" name="classic_form" id="classic_form">
<div><h4>Personal details:</h4></div><div style="margin-left: 109px;">
<div>Domain</div>
<input type="text" name="domain" id="domain" value="" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="" />
<div>Mobile:</div>
</form>
In my registration page, i used php validation. After the user submit the form if it shows validation errors it also resets all the fields. How can i resolve this problem? Without reset the fields i have to show the php validation errors. I also used in each input value. But it shows
"Notice: Undefined index: domain in D:\xampp\htdocs\deena\domainreg.php on line 82" . Please help me to resolve this problem

<input type="text" name="domain" id="domain" value="<?php echo isset($domain) ? $domain : ''; ?>" />

You have to pass all your values to php, and send back to html to feed your fields.

Its not 'resetting your fields' .. Your form is being submitted, hence the page is being reset and fields are therefore loading empty. Place the $_POST[] values in the field values upon page load:
<input type="text" name="domain" id="domain" value="<?php echo $domain ?>" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="<?php echo $fname?>" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="<?php echo $sname?>" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="<?php echo $tel?>" />

Simple. Just add the variables to the input values:
<input type="text" name="domain" id="domain" value="<?php echo $domain; ?>" />
You should also protect the outputted value, against cross site scripting:
<input type="text" name="domain" id="domain" value="<?php echo htmlspecialchars($domain); ?>" />

In the value field:
<input type="text" name="domain" id="domain"
value="<?php if(isset($_POST['domain'])){echo $_POST['domain'];} ?>">
Didn't test it. But i think it should work.

In input tag add the php value as like value="" So that it will echo if the variable is posted or it will show the empty one

Related

check if input has a number or just letters then echo result on submit

Im making a form that checks the Name input and echos results when the submit button is pressed using a IF statement. But i cant get it to work. What am i doing wrong?
<form id="AUTO" method="post" action="" novalidate>
<input type="hidden" name="token" value="<?php echo $token; ?>"/>
<input type="hidden" name="miles" value=""/>
<div id="contact_name">FULL NAME: *<br>
<input id="element_2_1" name="name" class="element text" size="15"
maxlength="15" value="" type="text" placeholder="FULL NAME">
</div>
<input type="submit" name="submit" class="submit action-button" value="SUBMIT" />
</form>
$Name = $_POST['Name'];
if (isset($_POST['submit'])) {
//checks name field for a number if one exist echo has a number
if (preg_match('|[0-9]', $Name)) {
echo 'has a number';
} else {
echo 'Does not contain a number';
}
}
The form input tag is named name however you are trying to get value POST index of Name.
Correct would be
$Name = $_POST['name'] ;
Keep the case of names.
Forms are key are sensitive if you set the form name as lowercase
<input id="element_2_1" name="name" class="element text" size="15"
maxlength="15" value="" type="text" placeholder="FULL NAME">
and in server side you should catch it as lowercase like this below.
$name = $_POST['name'];
That's how you can call the variable from the form in your server side.
Hope this helps you to solve your problem.
Thanks
<form id="AUTO" method="post" action="" novalidate>
<input type="hidden" name="token" value="<?php echo $token; ?>"/>
<input type="hidden" name="miles" value=""/>
<div id="contact_name">FULL NAME: *<br>
<input id="Name" name="Name" class="element text" size="15" maxlength="15" type="text" placeholder="Full Name" value=""></div>
<input type="submit" name="submit" class="submit action-button" value="SUBMIT" />
</form>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$Name = $_POST['Name'] ;
if (preg_match('#[0-9]#',$Name)){
echo 'has number';
}else{
echo 'no number';
}
?>

populate input field with PHP variable

I am creating a pizza ordering site with php. Now I want to echo the variable passed through the URL within the form. I know how to retrieve this data: <?php echo $_GET["numpizzas"]; ?>. But I don't know the proper way to add it to my html form field. any help is much appreciated
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="<?php echo "hi"; ?>" >
//Money field does not populate with number, I just see <?php echo $_GET[
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
<?php echo $_GET["numpizzas"]; ?>
I also tried storing the integer in a variable $howmanypizzas = $_GET["numpizzas"]; ?>but it still doesn't show up as the field value.
<?php echo $_GET["numpizzas"]; ?> does not only retrieve the data. echo also outputs it to the html response (the screen)
since you are allready passing your html with an ECHO, you can do:
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="'.$_GET["numpizzas"].'" >
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
Explanation: echo is a function that recieves a string and outputs it to html.
So, with the concatenation operator . you can inject the $_GET["numpizzas"] variable into your html, as a string, and pass it to the echo function, wich outputs it to the browser.
Another way to solve it is to only invoke PHP where you need to process your logic, just like #pavithra answer, wich also works.
You're already echoing and trying to echo inside of that. You need to concatenate your variable with the string that you are echoing, see PHP Strings:
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="' . $_GET["numpizzas"] . '">
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
You might also consider Heredoc syntax.
<input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
but i dont get why do you get this is as a get variable.hope this works
<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label> <input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>

trying to show the email and password Written in email and password inputs issue

I´m trying to show in a '<pre></pre>' the values that are passed in the form.
But Its not working and give my some errors:
-> Notice: Undefined index: email in
-> Notice: Undefined index: pass
-> Notice: Undefined index: remember
I already tried put isset like this:
$f['email'] = mysql_real_escape_string(isset($_POST['email']));
$f['pass'] = mysql_real_escape_string(isset($_POST['pass']));
$f['save'] = mysql_real_escape_string(isset($_POST['remember']));
And this way above I dont have erros but the data that I write in the form inputs dont show in my
echo '<pre class="debug">';
print_r($f);
echo '</pre>';
Can you see something that Im doing wrong?
My code:
<?php
if(isset($_POST['sendLogin']))
{
$f['email'] = mysql_real_escape_string($_POST['email']);
$f['pass'] = mysql_real_escape_string($_POST['pass']);
$f['save'] = mysql_real_escape_string($_POST['remember']);
echo '<pre class="debug">';
print_r($f);
echo '</pre>';
}
?>
<?php
if(!isset($_GET['remember']))
{
?>
<form name="login" action="" method="post">
<label>
<span>Email:</span>
<input type="text" class="radius" name="<?php if($f['email']) echo $f['email']; ?>" />
</label>
<label>
<span>Password:</span>
<input type="password" class="radius" name="<?php if($f['pass']) echo $f['pass']; ?>" />
</label>
<input type="submit" value="Login" name="sendLogin" class="btn" />
<div class="remember">
<input type="checkbox" name="remember" value="1"
<?php if(isset($f['save'])) echo 'checked="checked"' ?>
/>
I think that you have made mistake with name attribute. You should have static name="email" and name="pass" in the inputs. While your current name attribute change to value:
<input type="text" class="radius" name="email" value="<?php if($f['email']) echo $f['email']; ?>" />
<input type="password" class="radius" name="pass" value="<?php if($f['pass']) echo $f['pass']; ?>" />
Your inputs must have the name attribute in order to be accessible in the PHP
<form name="login" action="" method="post">
<label>
<span>Email:</span>
<input type="text" class="radius" value="<?php if($f['email']) echo $f['email']; ?>" name="email" />
</label>
<label>
<span>Password:</span>
<input type="password" class="radius" value="<?php if($f['pass']) echo $f['pass']; ?>" name="password" />
</label>
<input type="submit" value="Login" name="sendLogin" class="btn" />
<div class="remember">
<input type="checkbox" name="remember" value="1"
<?php if(isset($f['save'])) echo 'checked="checked"' ?>
/>
In <input type="text"> as well as in <input type="password"> block, provide a meaningful value for the name attribute.
Here in your code provide name="email" and for the next one name="pass".
Also to display the email/pass values after the form submission, give it in value attribute.
Right now you wrongly given inside the name attribute.
value="<?php if($f['email']) echo $f['email']; ?>" and
value="<?php if($f['pass']) echo $f['pass']; ?>"

how to get input box values which is called by ajax?

<form action="chngschdl.php" method="post">
<fieldset>
<label class="home">Flight Name</label> <select name="select_catalog_query" ><?php echo $options1; ?></select>
<br/><br/>
<label class="home">Starting Route</label> <input class="text" type="text" name="stroute" onKeyUp="numericFilter(this);" /> <label class="home">Deperture Time</label> <input class="text" type="text" name="stdrt" /><br/>
<label class="home">Ending Route</label> <input class="text" type="text" name="enroute" onKeyUp="numericFilter(this);" /> <label class="home">Arrival Time</label> <input class="text" type="text" name="enart" /><br/>
<label class="home">Break Route Number</label> <input class="text" type="number" name="bpn" maxlength='1' onkeyup="Bpoint(this.value)" /><br/>
<label id='bp' > <?php //$i=$_SESSION['point'];$_SESSION['bp']=$broute[$i];?></label>
<button class="btn">Go</button>
</fieldset>
</form>
ajax getting data from:
<?php
session_start();
if( $_SESSION['type']!='admin')
{
header("Location: index.php");
return;
}
$point=$_GET['q'];
$_SESSION['point']=$point;
$bpoption="";
for($i=0;$i<$point;$i++)
{
echo "<label>Break Route[$i]<label> <input class=text type=text name=broute[$i] /> ";
echo "<label>Arrival Time[$i]<label> <input class=text type=text name=bart[$i] /> ";
echo "<label>Departure Time[$i]<label> <input class=text type=text name=bdrt[$i] /> ";
echo "<br/><br/>";
}
?>
i have to collect every data of Break Route, Arrival time and Departure time... how i suppose to get this data and use it in another page?? is there is other way around by not using ajax... bt it must be remembered that Break Route is not predefined...
since your form fields are named like "name[$i]" it doesn't matter how many ajax-populated fields there are.
All you have to do on the page processing the form submit is:
foreach ($_POST["name"] as $name) {
// do things
}

HTML checkbox field is being passed to PHP as checked even when it is not

First of all thanks in advance, this has been very frustrating and I'm hoping someone can see something I'm not, I am definitely no php expert. Well here' what is going on.
I have a form where I have a checkbox for people to opt in to our newletter. The form element looks like this:
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
That is then submitted to a php file with this code:
if (isset($_POST['newsletter']) && $_POST['newsletter'] == 'Yes'){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
$newsletter is then inserted into a database field.
The issue is that whether the box is checked or not it is being sent to php as true, so every entry is receiving the newsletter.
Any help would be greatly appreciated! Thanks!
Here's the full form minus the option list for the sake of brevity
<form method="post" action="contact.php" name="contactform" id="contactform">
<fieldset>
<legend>Please fill in the following form all fields are required, thanks!</legend>
<label for=firstName accesskey=F><span class="required">*</span>First Name</label>
<input name="firstName" type="text" id="firstName" size="30" value="" />
<br />
<label for=lastName accesskey=L><span class="required">*</span>Last Name</label>
<input name="lastName" type="text" id="lastName" size="30" value="" />
<br />
<label for=email accesskey=E><span class="required">*</span>Email</label>
<input name="email" type="text" id="email" size="30" value="" />
<br />
<label for=city accesskey=C><span class="required">*</span>City</label>
<input name="city" type="text" id="city" size="30" value="" />
<br />
<label for=state accesskey=S><span class="required">*</span>State</label>
<select name="state" type="text" id="state">
<option value="AL">Alabama</option>
...
<option value="WY">Wyoming</option>
</select>
<br />
<label for=newsletter accesskey=N class="checkbox">Signup for Cloverton's Newsletter</label>
<input name="newsletter" type="checkbox" id="newsletter" value="Yes" style="width:20px;" />
<br />
<p><span class="required">*</span> Are you human?</p>
<label for=verify accesskey=V> 3 + 1 =</label>
<input name="verify" type="text" id="verify" size="4" value="" style="width: 30px;" /><br /><br />
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
Your code is correct. You most likely have a problem with your database insert/update logic. Why do you assume it is the PHP form handling?
This has just dawned on me.
If a checkbox is unchecked it isn't set in the $_POST superglobal. So if !isset($_POST['newsletter']) then it wasn't checked - if isset($_POST['newsletter']) it was checked.
Edit: Remove the 'yes' part - the value will never be yes, just true or 'on'.
Edit 2:
I've tested this to death. Change your code to:
if (isset($_POST['newsletter'])){
echo "newletter yes";
$newsletter = 1;
}else{
echo "newsletter no";
$newsletter = 0;
}
Remove the value="Yes" attribute from your checkbox input also. If you want it checking by default use checked="checked".

Categories