how to retain the values of data entry in a placeholder during postbacks
<input type="text" name="name" value="<?PHP $name;?>" placeholder="Enter Name"/>
when php found any error and post it back
The data entry are all gone, user have to re enter all the information again.
What does value="<?PHP $name;?>" do? Is this supposed to print the variable? You would need to do this instead:
<?= $name; ?>
or
<?php echo $name;?>
<?php if empty($name){
$name='Enter Name';
} else {}?>
<input type="text" name="name" <?php echo $name;?> placeholder="Enter Name"/>
Try this:
<input type="text" name="name" value="<?PHP echo empty($_POST['name'])?'Enter Name':$_POST['name'];?>" />
In your PHP use your $_POST / $_GET variables to restore your user data:
<input type="text" name="name" value="<?= $_POST['name'] ?>" placeholder="Enter Name"/>
Where $_POST['name'] contains the data sent via a POST request from the form and 'name' is the name of your input.
Try this;
<input type="text" name="name" value="<PHP if (isset($name)){ echo $name; }?>"placeholder="Enter Name"/>
The isset will handle any undefined variable errors, if $name has not been piror to the submission
Related
I am new to php and just cant figure how to get the data from a a html textbox and then echo the var.
I am making a registration page, I do everything else but this simple part.
I have a preset value="hi" just for testing if the var populates.
Fyi in the future it will be done after i click a register button. just need to get this.
Thanks all
<input name="fName" id="fName" type="text" value="hi" />
and here is the php which i try to read the data into if the echo is test to check if it populates
<?php
$fName = $_POST['fName'];
//$fName = $_GET['fName'];
echo $fName;
?>
<input type="text" placeholder="NAME" name="name" value="<?php if(isset($_POST['name'])){ echo $_POST['name']; } ?>" />
<input type="text" name="phone" placeholder="PHONE" value="<?php if(isset($_POST['phone'])){ echo $_POST['phone']; } ?>" />
This should work for you:
You have to make a form and submit it! After that you can use the variable$_POST['fName']
<?php
if (isset($_POST['fName'])) //if you also want to check if it is empty use !empty($_POST['fName'])
echo $_POST['fName'];
?>
<form action="" method="post">
<input name="fName" id="fName" type="text" value="hi" />
<input type="submit" name="submit" value="submit!" />
</form>
How to pass a form value to next page in other form?
I have this code in the :
<form action="confirm.php" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
Email: <input type="text" name="email">
</form>
Now I want that in confirm.php to put some hidden input fields with this values, I tried this code:
<form action="nextpage.php" method="post">
//some other input fields...
<input type="hidden" name="firstname" value="<?php $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php $_POST['email']?>">
</form>
And so on in other 2 pages, and in the last page I want to email all this fields, I tried PHP _SESSION, but no luck with that, so I think that this can be more easier for me!
And something else I forgot to tell, on the second page (nextpage.php) action form variable I refer to a file that use this code:
<?php
header('Location: dear-'.$_POST['firstname'].'.php');
?>
<html>
<form>
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']?>">
</form>
</html>
In this case how to pass this values (firstname, lastname and email) on the next page ? I use that because I want to generate a page like this www.site.com/dear-name.php
Try this in confirm.php
<form action="nextpage.php" method="post">
//some other input fields...
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']?>">
</form>
You forgot to echo your variable.
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']; ?>">
Update:
You have two choices :
Store your data to session and retrieve to another page.
Pass your data using query string.
header('Location: dear-'.$_POST['firstname'].'.php?firstname='.$_POST['firstname'].'&lastname='.$_POST['lastname'].'&email='.$_POST['email']);
use echo:
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']; ?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']; ?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']; ?>">
and if this still not working, isset() your post variables:
if(isset($_POST['firstname'],$_POST['lastname'],$_POST['email'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
echo '<input type="hidden" name="firstname" value="$firstname">';
echo '<input type="hidden" name="lastname" value="$lastname">';
echo '<input type="hidden" name="email" value="$email">';
}
In your input text field, value is empty because of echo is missing. You need to add echo to print the value in your form. So that it can be sent into another page.
The second option is that you can use session_start() in top of each page.
and store the value of the variable in your session.
$_SESSION['name'] = "value_of_session"// in your case that is POST data
and this can be used in next page you need not to include hidden fields.
the most important thing is that You must write
<?php session_start() ?> // in each(you've three) page.
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
I am trying to set up an application form on my site where there is a main large application form on one page (parent.php)and a smaller form on another page (child2.php) that when users fill out some of their details and submit that smaller form, they are taken to the larger form and the details they have entered already appear in the corresponding textbox on the larger form along side some extra boxes for them to fill out(if that makes sense!)
I can get it to work in that the textboxes on the 2nd page display the values of the matching textboxes on the first page, but only when a value is set. As users can either access the main application form through the smaller form OR by directly accessing it, I need to have it so that if the value is set, the set value is displayed and will also be the value entered into the database OR if the value is not pre-set from the smaller form, the user can enter in their info to the main form and this is what's sent to the DB. I think I might need to use ifisset and have tried to do so but am getting nowhere.
Apologies for messy code and the set up of the textboxes as they are just for testing this out and I am still getting to grips with all this and would be grateful if anyone could help me/let me know if I'm on the right track/totally off. Thanks in advance!
Page 1 (parent.php)
<form action="child2.php" method="post" class="validate">
<div>
<input class="tb" type="text" name="fName" placeholder="first name" id="fName" value="<?php $fName ?>" required/><br/>
<br/>
<input class="tb" type="text" name="sName" placeholder="surname" id="sName" value="<?php $sName ?>" required/><br/>
<br/>
<input class="tb" type="email" name="email" required placeholder="email address" id="email" value="<?php $email ?>" required/>
<br/>
<input class="tb" type="address" name="address" placeholder="address" value="<?php $address ?>" id="address" />
<br/>
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
Page 2 (child2.php)
<?php
function renderForm($fName, $sName, $email, $address){
?>
<form action="" method="post" class="validate">
<label class="label">first name</label><input class="tb" type="text" id="fName" name="fName" value="<?php if (isset($fName)) { echo $fName = $_REQUEST['fName'];} else { echo "first name"; }?>"/>
</br>
<label class="label">surname</label><input class="tb" type="text" id="sName" name="sName" value="<?php if (isset($sName)) { echo $sName = $_REQUEST['sName'];} else { echo "surname"; }?>"/>
</br>
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php if (isset($email)) { echo $email = $_REQUEST['email'];} else { echo "email"; }?>"/>
</br>
<label class="label">address</label><input class="tb" type="text" id="address" name="address" value="<?php if (isset($address)) { echo $address = $_REQUEST['address'];} else { echo "address"; }?>"/>
</br>
What you're looking for are $_SESSION variables, which stay active until the user closes the browser or until the Session expires (I think the default PHP config time is 24 minutes..?). This is a lot more efficient than storing them in a database for temporal purposes.
So you can set the variables with $_SESSION['fName'] = $_POST['fName']; etc. and then call them later with echo $_SESSION['fName']; etc.
To utilize session variables you will need <?php session_start(); ?> at the beginning of your pages (before any HTML is used)
As suggested by khanahk, using SESSION variables will do your job. The problem with the solution you are thinking is that its fine only as long as you are passing values from one page to some other page, that too after so much mess.
So, you should instead, you should use something like this
session_start();
$email = $_SESSION['email'];
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php echo $email; ?>"/>
I've got a php variable like so.. $name = $_REQUEST['name']; I'd like to put it in a HTML form field's value e.g in here.. <input type="text" name="name" value=(php variable here) />
How would I do so?
Thanks.
value="<?php echo htmlspecialchars($name); ?>"
You can do it like this,
<input type="text" name="name" value="<?php echo $name;?>" />
But seen as you've taken it straight from user input, you want to sanitize it first so that nothing nasty is put into the output of your page.
<input type="text" name="name" value="<?php echo htmlspecialchars($name);?>" />