0

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

另外,我从第一个表单中收到错误“未定义的索引 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”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<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”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<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>
4

1 回答 1

2

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

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

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

  <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"/>
于 2012-08-26T14:50:56.460 回答