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"/>
Related
I am trying to write a simple html form which requires the user to enter the correct email and password, using $_SERVER as the action for my form. I do not want to send the POST info to another php page, but I want to do it on the same page instead.
I have set two variables, $correct_email and $correct_password.
Here is the form;
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="submit" value="Submit">
</form>
Here is what I am trying to get work in PHP
$correct_email = "(my personal email)";
$correct_password = "password"];
if ($_POST['eml']) == $correct_email && ($_POST['pwd']) == $correct_password {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
It is not working, please help! I am also unclear on whether the PHP should come before or after the form.
Also, I would like to have the form be cleared from view, on the page, when the correct info is entered.
Thanks so much
Change the name of the submit button - never call anything "submit" in a form
Put the test at the top
You had issues with the ( and ) in the test
Also an issue with a ] after "password"
<?php
if ($_POST["subBut"] === "Submit") {
$correct_email = "(my personal email)";
$correct_password = "password";
if ($_POST['eml'] == $correct_email && $_POST['pwd'] == $correct_password) {
echo "<h1>You are logged in</h1>";
} else {
echo "<h1>error</h1>";
}
}
else {
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]); ?>">
<label for="eml">Email:</label>
<input type="email" name="eml" id="eml" required>
<br>
<label for="pwd">Password:</label>
<input type="password" name="pwd" id="pwd" required>
<br>
<input type="hidden" name="checkMe" value="12345">
<input type="submit" name="subBut" value="Submit">
</form>
<?php } ?>
I followed a online tutorial to connect my php code with MySQL. After I click the submit button, it said it cannot find the object (page). I did not see any code in my code? Any idea for me how to debug this code?
<?php
$user = 'root';
$pass = 'xxxxxx';
$db = 'testDB';
$db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");
echo"Great work!";
$id ="";
$fname="";
$midname="";
$lname="";
function getPosts()
{
$posts =array();
$posts[0]=$_POST['Contact_ID'];
$posts[1]=$_POST['first_name'];
$posts[2]=$_POST['middle_name'];
$posts[3]=$_POST['last_name'];
return $posts;
}
//search
if(isset($_POST['search']))
{
$data = getPosts();
$search_Query="select * from Contact where contact_ID =$data[0]";
$search_Result=mysql_query($db, $search_Query);
if($search_Result)
{
if(mysql_num_rows($search_Result))
{
while($row=mysql_fetch_array($search_Result))
{
$id =$row['contact_ID'];
$fname =$row['first_name'];
$midname =$row['middle_name'];
$lname =$row['last_name_name'];
}
}
else
{
echo"No data for this Id";
}
}
else
echo"Result error";
}
?>
<!DOCTYPE Html>
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<form action="php_insert_update_delete_search.php" method="post">
<input type="number", name="contact_ID" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text", name="first_name" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
<input type="text", name="middle_name" placeholder="Middle Name" value="<?php echo $midname;?>"><br><br>
<input type="text", name="last_namename" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">
</div>
</form>
</body>
</html>
remove php_insert_update_delete_search.php from the action of your form. The form is being redirected to php_insert_update_delete_search.php page when you click search button
<form action="" method="post">
<input type="number" name="contact_ID" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text" name="first_name" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
<input type="text" name="middle_name" placeholder="Middle Name" value="<?php echo $midname;?>"><br><br>
<input type="text" name="last_namename" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">
</div>
</form>
See You Have applied an action in your form tag . This action means that when ever a user will click on any button related to that form he or she will be redirected to that page (mentioned in action ) . So u need to make a page php_insert_update_delete_search.php and has to apply logic there for inserting the data .
If your wanting the form to go to another page to run code to process what you need
<form action="pageyouneed.php" method="post">
if you want to use the current page to parse your code:
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
$_SERVER["PHP_SELF"]; // Will return current document to parse code.
I have created a multipage form that is storing the data in sessions. Instead of using method="post" I am using method="get" to allow the user to click the back button and hold the data on previous pages to edit. I also have email validation happening in the PHP script.
How would I be able to have a submit button advance to the next page in the form and a Reset button to clear the session and stay on this page (or reload the page) without the session data appearing within the form fields?
I have tried modifying someone else's suggestion using the following without success. Here is the PHP script at the start of the document:
<?php
session_start();
foreach ( $_GET as $key=>$value ) {
if ( $key!="submit" ) {
$value = htmlentities(stripslashes(strip_tags($value)));
$_SESSION[$key] = $value;
}
}
$firstname = $_SESSION['Firstname'];
$lastname = $_SESSION['Lastname'];
$email = $_SESSION['Email'];
if(isset($_GET['clear'])) {
session_destroy(); // Or other session-unsetting logic
header("Location: form.php"); // Reload your page
}
else if(isset($_GET['submit'])) {
//next page logic
header("Location: form-2.php"); // Reload your page
}
?>
Here is the form within the page body:
<form class="mod-columns-form" method="get" name="mailform" action="form-2.php" onsubmit="return validateForm();">
<fieldset>
<label for="Firstname">First Name <span class="mod-error">*</span></label>
<input type="text" name="Firstname" id="Firstname" placeholder="first name" value="<? echo $firstname; ?>" />
<label for="Lastname">Last Name <span class="mod-error">*</span></label>
<input type="text" name="Lastname" id="Lastname" placeholder="last name" value="<? echo $lastname; ?>" />
<label for="Email">Email <span class="mod-error">*</span></label>
<input type="text" name="Email" id="Email" placeholder="yourname#domain.com" value="<? echo $email; ?>" />
<button name="submit" type="submit" value="submit" class="submit">Next</button>
<button name="clear" type="submit" value="clear">Clear</button>
</fieldset>
</form>
to free you from the data in the form , you can create a button , which perform reset the variables.
<button name="reset" value="resert">RESET</button>
you need to check the session this page, creating a small code block in php .
<?php
session_start();
if (!isset($_SESSION['Firstname']) && $_SESSION['Lastname'] && $_SESSION['Email'])
{
print "error";
}
?>
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.
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>