为什么我的表单过早提交?

时间:2012-08-26 14:35:27

标签: php mysql

我在一个页面上有一个表单,该页面上的提交按钮将用户引导到表单的下一页。我正在尝试进行设置,以便在单击表单2上的提交按钮并填写所有表单时,将表单1中的信息与表单2的信息一起提交,但是,第一个表单的数据正在提交给我进行表格时的数据库2.任何想法为什么会这样?以及任何解决方案的想法?

另外,我从第一个表单中收到错误“undefined index GET”,我很难理解为什么?

提前致谢!

表单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>

表格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>

1 个答案:

答案 0 :(得分:2)

parent.php的action字段是child2.php。此外,您正在检查

if (isset($_POST['submit']))

由第一页设置为true,因此,它进入循环并在数据库中插入数据。这可以通过将其放在父文件中来解决

  <input id="submit" name="submitINIT" type="submit" value="Next">

一个可能的解决方案是,您可以提取第一个表单的值并将其存储在某些会话变量中,最后在最终提交时,您可以使用这些值进行插入。

在你的child2.php中,执行此操作

 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
}

此外,使用此

将第二个文件的操作更改为自身
<form action="child2.php" method="post" class="validate">

另外,在child2.php中进行此修改

<input id="submit" name="submit" type="submit" value="Submit"/>