gpt4 book ai didi

php - 为什么我的表单提交得太早?

转载 作者:行者123 更新时间:2023-11-29 08:47:45 26 4
gpt4 key购买 nike

我在一个页面上有一个表单,该页面上的提交按钮会将用户引导到表单的下一页。我正在尝试进行设置,以便当单击表单 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>

最佳答案

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

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

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

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

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

在您的 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"/>

关于php - 为什么我的表单提交得太早?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12130972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com