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; ?>"/>
Related
This question already has answers here:
Submit HTML form on self page
(5 answers)
Closed 2 years ago.
I have a page for people to submit their information /collect.php but when submitting it keeps going to the root. Is it possible to submit the form and make it go to the same collect?
Here is the code I have so far:
<?php if (!empty($_POST)):
$firstname = htmlentities($_POST['firstname']);
$lastname = htmlentities($_POST['lastname']);
$email = htmlentities($_POST['email']);
$phone = htmlentities($_POST['phone']);
echo $firstname."<br/>";
echo $lastname."<br/>";
echo $email."<br/>";
echo $phone."<br/>";
else: ?>
<form action="." method="post">
<input type="text" name="firstname" placeholder="First name"><br/>
<input type="text" name="lastname" placeholder="Last name"><br/>
<input type="email" name="eamil" placeholder="Email address"><br/>
<input type="phone" name="number" placeholder="Phone number"><br/>
<input type="submit">
</form>
<?php endif; ?>
You're almost there, to submit to the same page you can use the $_SERVER["PHP_SELF"] which is a super global variable that returns the filename of the currently executing script.
You'll also want to be careful with your input names, email is misspelled and the form for the phone number is named number while the post is labelled phone. Also, the correct the correct HTML5 for a phone number is tel.
<?php if (!empty($_POST)):
$firstname = htmlentities($_POST['firstname']);
$lastname = htmlentities($_POST['lastname']);
$email = htmlentities($_POST['email']);
$phone = htmlentities($_POST['phone']);
echo $firstname."<br/>";
echo $lastname."<br/>";
echo $email."<br/>";
echo $phone."<br/>";
else: ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="text" name="firstname" placeholder="First name"><br/>
<input type="text" name="lastname" placeholder="Last name"><br/>
<input type="email" name="email" placeholder="Email address"><br/>
<input type="tel" pattern="([0-9]{3}) [0-9]{3}-[0-9]{4}" name="phone" placeholder="(123) 456-7890" required><br/>
<input type="submit">
</form>
<?php endif; ?>
I have some html forms that I verify completeness with php. The issue I have is that when one required form is not filled, the filled forms are cleared.
The HTML
<p>Email: <span class="required">* <?php echo $EmailErr;?></span><input type="text" name="Email" placeholder="Email" /></p>
<p>Comments: =input type="text" name="Comments" maxlength="75" placeholder="Comments"/></p>
This is the PHP
if (empty($_POST["Email"])) {
$EmailErr = "";
} else {
$Email = validateEmail($_POST["Email"]);
}
if (empty($_POST["Comments"])) {
$Comments = "";
} else {
$Comments = test_input($_POST["Comments"]);
}
The question remains, how do I prevent the other forms from being cleared upon submission?
You should do a client side validation in order to retain the values on your form.
That being said you should still have server validation.
There is different way to do it, using javascript/jquery or even simply adding required attribute to your tag, for example:
<input type="text" name="Email" placeholder="Email" required/>
for javascript:
http://www.w3schools.com/js/js_validation.asp
for jquery , here is a good plugin:
http://jqueryvalidation.org/
<p>
Email: <span class="required">*<?php echo $EmailErr; ?></span>
<input type="text" name="Email" placeholder="Email" value="<?php echo!empty($_POST['Email']) ? htmlspecialchars($_POST['Email']) : '' ?>"/>
</p>
<p>
Comments: <textarea name="Comments" maxlength="75" placeholder="Comments"><?php echo!empty($_POST['Comments']) ? htmlspecialchars($_POST['Comments']) : '' ?></textarea>
</p>
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>
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
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