Pre-populate a form from a link - php

Apologies if this question has already been asked...
I need to the data that is entered in form1, to be sent to form2 through the URL, form2 to read the data from the URL, populate the fields of form2, submit, then redirect to a thanks page.
I was thinking of sending the information through GET in the URL.
I don't want form2 to be seen by the user, just form1 and then the thanks page if successful.
I did try this using the below method...
index.php
<?php
session_start();
?>
<form method="POST" name="test" id="test" action="process.php">
<label for="fname">First name</label>
<input type="text" name="fname" id="fname" /><br />
<label for="lname">Last name</label>
<input type="text" name="lname" id="lname" /><br />
<label for="email">Email</label>
<input type="text" name="email" id="email" /><br />
<input type="submit" value="Submit" />
</form>
process.php
<?php
session_start();
//Collect data set in the URL
if (isset($_POST['fname'])) { $fname = trim($_POST['fname']); }
if (isset($_POST['lname'])) { $lname = trim($_POST['lname']); }
if (isset($_POST['email'])) { $email = trim($_POST['email']); }
// Prepare web to lead link
$url = 'success.php?fname='.$fname.'&lname='.$lname.'&email='.$email;
// GO!
$ch = curl_init($url);
curl_exec($ch);
curl_close($ch);
?>
success.php
<?php
session_start();
//Collect data set in the URL
if (isset($_GET['fname'])) { $fname = trim($_GET['fname']); }
if (isset($_GET['lname'])) { $lname = trim($_GET['lname']); }
if (isset($_GET['email'])) { $email = trim($_GET['email']); }
?>
<!DOCTYPE html>
<html>
<head>
<title>Form test</title>
</head>
<body>
<form>
<label for="fname">First name</label>
<input type="text" name="fname" id="fname" value="<?php echo isset($fname); ?>" /><br />
<label for="lname">Last name</label>
<input type="text" name="lname" id="lname" value="<?php echo isset($lname); ?>" /><br />
<label for="email">Email</label>
<input type="text" name="email" id="email" value="<?php echo isset($email); ?>" />
</form>
</body>
</html>
index.php being form1, process.php collecting the data and submitting the form2, success.php being form2
Any suggestions?

The form contents when submitted in index.php will be transferred to process.php using POST method and in process.php, it prepares the link and redirects the user to success.php using header() (instead of curl), and then in success.php, the form input fields are pre-populated by using <?php if(isset($fname)) echo $var; ?> (you were doing <?php echo isset($fname); ?> which will simply output 1 or 2 depending on the condition)
The changed code should look like this (tested):
process.php:
<?php
session_start();
//Collect data set in the URL
if (isset($_POST['fname'])) { $fname = trim($_POST['fname']); }
if (isset($_POST['lname'])) { $lname = trim($_POST['lname']); }
if (isset($_POST['email'])) { $email = trim($_POST['email']); }
// Prepare web to lead link
$url = 'success.php?fname='.$fname.'&lname='.$lname.'&email='.$email;
// GO!
header("Location: $url");
?>
success.php:
<?php
session_start();
//Collect data set in the URL
if (isset($_GET['fname'])) { $fname = trim($_GET['fname']); }
if (isset($_GET['lname'])) { $lname = trim($_GET['lname']); }
if (isset($_GET['email'])) { $email = trim($_GET['email']); }
?>
<!DOCTYPE html>
<html>
<head>
<title>Form test</title>
</head>
<body>
<form>
<h2> Submitted Form </h2>
<label for="fname">First name</label>
<input type="text" name="fname" id="fname" value="<?php if(isset($fname)) echo $fname; ?>" /><br />
<label for="lname">Last name</label>
<input type="text" name="lname" id="lname" value="<?php if(isset($lname)) echo $lname; ?>" /><br />
<label for="email">Email</label>
<input type="text" name="email" id="email" value="<?php if(isset($email)) echo $email; ?>" />
</form>
</body>
</html>
I hope this helps. Good luck!

You can send the data with POST and then save it to the SESSION, so it's available from any of your pages; the user will still be able to see the data but would require some more work by having to use the developer tools or Firebug.

Related

How to move from current PHP page to another PHP page if condition true?

I am working on a web project where I want to move from one PHP page to another Php page if condition true...
In below login PHP page, I am getting username and password using $_POST[]. if both username and password got matched in (if statement) of current PHP login page then, I want to jump to another PHP page(choice.php) specified in header function below after if.
<html>
<body>
<head>
</head>
<form method="post" action="login.php">
<div id="div1">
<h1>welcome to bizdiary</h1>
<div id="div2">
<label >Username</label>
<input id="name" type="text" name="username" value=""
placeholder="username" />
<label >Password</label><input type="text" name="password" value=""
placeholder="password"/>
<input type='submit' name="login" value="login" >
</form>
<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
if($username=='root' && $password=='tiger'){
header( "Location:http://localhost/bizdiary/choice.php" ); die;
}
}
?>
This code should work:
The HTML in top of the file.
Remove the action in your form.
<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
$host = $_SERVER['HTTP_HOST'];
// Put in here the conditional that the request need to accomplish to redirect.
if($username=='root' && $password=='tiger'){
header("Location: http://{$host}/choice.php");
}
}
?>
<html>
<body>
<head>
</head>
<body>
<form method="post">
<div id="div1">
<h1>welcome to bizdiary</h1>
<div id="div2">
<label >Username</label>
<input id="name" type="text" name="username" value="" placeholder="username" />
<label >Password</label>
<input type="text" name="password" value="" placeholder="password"/>
<input type='submit' name="login" value="login" >
</form>
</body>
</html>
You should mysql control. Example
if ($_POST){
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
if (empty($username) or empty($password)){
echo 'Don't leave blank.';
}else {
$user = mysql_query('SELECT * FROM user WHERE username = "'.$username.'" AND password = "'.$password.'"');
if (mysql_num_rows($user)){
header('Location: asd.php');
}else {
echo 'Didn't find user.';
}
}
}

2 different action in one form

I have a form called addcustomer.php . it's contain firsname, lastname, mobile fields.i have also 2 button on this form. onr button for saving data and the others for sending sms to customers.
i want to when i click second button 3 data fields passed to another form called smsinfo.php.
now action for saving data works good but when direct to smsinfo . there is no data on this form .
<form method ="POST" name = "custform" action="">
<div class="cell_2_2"><label class="lblfields">Firstname:</label> <input type="text" name="firstname" autocomplete="off"/></div>
<div class="cell_3_2"><label class="lblfields">Lastname :</label> <input type="text" name="lastname" autocomplete="off"/></div>
<div class="cell_5_2"><label class="lblfields">Mobile :</label> <input type="text" name="mobile" autocomplete="off"/></div>
<input type="submit" name="submit" value="Save"/>
<input type="submit" name="sendsms" value="Sms"/>
if (isset($_POST['submit']))
{
Saving code here
} else if (isset($_POST['sendsms'])) {
header("Location: smsinfo.php");
}
here code of smsinfo.php:
<?php
$Fname = $_REQUEST['firstname'];
$Lname = $_REQUEST['lastname'];
$Mob = $_REQUEST['mobile'];
?>
<html>
<body>
<form action="sendingsms.php" method="POST">
<input style="width: 100px;" name="firstname" type="text" value="<?php echo $firstname; ?>"/>
<input style="width: 100px;" name="lastname" type="text" value="<?php echo $lastname; ?>"/>
<input style="width: 100px;" name="mobile" type="text" value="<?php echo $mobile; ?>"/>
</form>
</body>
</html>
thanks all and sorry for my poor english

read data from text box into php var and then echo

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>

Clear html text fields

I have an HTML form for submitting and retrieves data from MySQL database with two buttons, "Save/Submit" and "New/Reset"
It fetch data correctly from MySQL database but when I click on New/Reset button for new contact entry it couldn't clear forms text fields. My HTML and PHP codes are as under:
<?php
//Database Connection file.
include'connect.php';
$sql = mysql_query("SELECT * FROM contact_list WHERE id='1'");
While($result = mysql_fetch_assoc($sql)){
$fname = $result['fname'];
$lname = $result['lname'];
$email = $result['email'];
$contact = $result['contact'];
}
if(isset($_POST['fname'])&&isset($_POST['lname'])&&isset($_POST['email'])&&
isset($_POST['contact'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$contact = $_POST['contact'];
if($sql = mysql_query("INSERT INTO contact_list VALUES ('', '$fname', '$lname',
'$email', '$contact')")){
echo'Contact Save Successfully.';
}else{
echo'Contact not save.';
}
}
?>
<html>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>">
//Clean all fields of forms for new entry.
<input type="reset" value="New">
//Save or submit form data into mysql database
<input type="submit" value="Save">
</form>
</html>
You can do this easily by using jQuery
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$("#btnReset").click(function(){
$("#fname").val("");
$("#lname").val("");
$("#email").val("");
$("#contact").val("");
});
});
</script>
</head>
<form action="sample.php" method="POST">
First Name:<input type="text" name="fname" value="<?php if(isset($fname))
{echo $fname;}?>" id="fname">
Last Name:<input type="text" name="lname" value="<?php if(isset($lname))
{echo $lname;}?>" id="lname">
Email:<input type="text" name="email" value="<?php if(isset($email))
{echo $email;}?>" id="email">
Contact:<input type="text" name="contact" value="<?php if(isset($contact))
{echo $contact;}?>" id="contact">
//Clean all fields of forms for new entry.
<input type="reset" value="New" id="btnReset">
//Save or submit form data into mysql database
<input type="submit" value="Save" id="btnSave">
</form>
</html>

Why is my form submitting too early?

I have a form on one page, and the submit button on that page leads the user to the next page of the form. I'm trying to set it up so that the info from form 1 is submitted along with form 2's info when the submit button on form 2 is clicked and all forms have been filled out, however, the first form's data is being submitted to my database when proceeding to form 2. Any ideas why this is the case? And any ideas of a solution?
Also, I am getting an error "undefined index GET" from the first form and I am struggling to understand why?
Thanks in advance!
Form 1 (parent.php)
<?php session_start();
$_SESSION['fName']=$GET['fName'];
$_SESSION['sName']=$GET['sName'];
$_SESSION['email']=$GET['email'];
$_SESSION['address']=$GET['address'];
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>table2</title>
</head>
<h1>Is this in xamppfiles and htdocs?</h1>
<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="Next">
</div>
</form>
</body>
</html>
Form 2 (child2.php)
<?php
session_start();
include("connect.php");
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>table2</title>
</head>
<body>
<?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 = $_SESSION['fName'];} else { if(!isset($fName)) { echo ""; }}?>"/>
</br>
<label class="label">surname</label><input class="tb" type="text" id="sName" name="sName" value="<?php if (isset($sName)) { echo $sName = $_SESSION['sName'];} else { if(!isset($sName)) { echo ""; }}?>"/>
</br>
<label class="label">email</label><input class="tb" type="email" id="email" name="email" value="<?php if (isset($email)) { echo $email = $_SESSION['email'];} else { if(!isset($email)) { echo ""; }}?>""/>
</br>
<label class="label">address</label><input class="tb" type="text" id="address" name="address" value="<?php if (isset($address)) { echo $address = $_SESSION['address'];} else { if(!isset($address)) { echo ""; }}?>""/>
</br>
<input id="submit" type="submit" value="Submit"/>
</form>
<?php
}
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$fName = mysql_real_escape_string(htmlspecialchars($_POST['fName']));
$sName = mysql_real_escape_string(htmlspecialchars($_POST['sName']));
$email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
// check to make sure both fields are entered
if ($fName == '' || $sName == '' || $email == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($fName, $sName, $email, $address, $error);
}
else
{
// save the data to the database
mysql_query("INSERT formtest SET fName='$fName', sName='$sName',email='$email', address='$address'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: child2.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','','');
}
?>
</body>
</html>
The action field of your parent.php is child2.php . Also, you are checking for
if (isset($_POST['submit']))
which is set true by your first page, hence, it goes inside the loop and inserts the data in your database. This can be resolved by placing this in your parent file
<input id="submit" name="submitINIT" type="submit" value="Next">
A possible solution can be, you extract the values of the first form and store it in some session variables , and finally at final submission, you can use those values for insertion.
In your child2.php, do this
if (isset($_POST['submitINIT'])){
// store all the available values in some session variables
$_SESSION['value1']=$POST['fName'];
$_SESSION['value2']=$POST['sName'];
$_SESSION['value3']=$POST['email'];
$_SESSION['value4']=$POST['address'];
}
if (isset($_POST['submit'])){
// proceed after final submission
}
Also, change the action of 2nd file to itself using this
<form action="child2.php" method="post" class="validate">
Also, do this modification in your child2.php
<input id="submit" name="submit" type="submit" value="Submit"/>

Categories