【问题标题】:Value in variable disappears when a second submit button is pressed按下第二个提交按钮时变量中的值消失
【发布时间】:2016-07-07 12:57:18
【问题描述】:

我的程序首先要求用户输入客户编号。这是在按下提交 1 按钮时实现的。然后这个客户号用于从数据库中提取信息并显示它。然后必须更改此客户的地址,以便在第二个表单上要求此更改并按下提交 2 按钮。

问题是:

  1. 当按下提交 2 按钮时,客户编号从 enter customer no 框中消失,显示的信息也消失。
  2. 变量$custno 已失去其值。我需要这个变量来更新数据库。

为什么会这样? 我什至将 POST 用于submit 1 REQUEST METHODGET 用于submit 2 REQUEST METHOD

这是我使用的代码:

<!DOCTYPE html>
<html>
<body>

<?php
$custno=""; $custexists=1; // $custexists=1 means the customer exists in the database
$newaddress1=""; $newaddress2="";

if ($_SERVER["REQUEST_METHOD"] == 'POST' ) {
if (isset ($_POST["submit1"])) {$custno=$_POST["cust"];

goprintcust($custno,$custexists,$newaddress1,$newaddress2);}}

?>
<div style="position: fixed; left: 14px; top: 10px;">
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<br>
<label>Enter Customer No. <input type="number" name="cust" min="1" value="<?php echo $custno; ?>"/></label>
<br>
<input type="submit" name="submit1" value="Submit 1"/><br>
</form>

<?php
function goprintcust($custno,$custexists,$newaddress1,$newaddress2) {
?><div style="position: relative; left: 8px; top:80px;"><?php
// Here search database for $custno and if it exists set $custexists=1
if ($custexists==1) {echo "current data for customer $custno is as follows ..."; // Print the current customer data from the database here
             getnewinput($newaddress1,$newaddress2);}} // we assume the customer exists in the database
                                   // so now get the new data to write into the database


function getnewinput($newaddress1,$newaddress2) {
?>
<div style="position: fixed; left: 14px; top: 60px;">
<table>

<form method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"">
<tr><td><label>New Address 1 :</td><td>  <input type="text" name="newaddress1" value="<?php echo $newaddress1; ?>"/></label></td></tr>
<br>
<tr><td><label>New Address 2 :</td><td>  <input type="text" name="newaddress2" value="<?php echo $newaddress2; ?>"/></label></td></tr>
<br>
<input type="hidden" name="cust" value="<?php echo $custno; ?>">
<tr><td><input type="submit" name="submit2" value="Submit 2"></td></tr><br>
</form>
</table>
<?php             }



if ($_SERVER["REQUEST_METHOD"]=="GET") {
if (isset ($_GET["submit2"])) {
$newaddress1=$_GET["newaddress1"]; $newaddress2=$_GET["newaddress2"];
getnewinput($newaddress1,$newaddress2);
?><div style="position: absolute; left: 8px; top:130px;"><?php
// Here write the new Address into the database              
echo "<br>"."Database updated for Customer No. ".$custno;
}}

?>
</body>
</html>

【问题讨论】:

  • 第一次提交表单Data A被提交,第二次提交表单Data B被提交,除非你也提交Data A TOGETHER WITH Data B然后Data A丢失了。 (或将其存储在其他地方,例如 cookie 或会话)。

标签: php html css post get


【解决方案1】:

建议您在第二个表单中添加&lt;input type="hidden" name="cust" value="&lt;?php echo $custno; ?&gt;"&gt;

当您提交第二个表单时,您的 $_POST 数组将被重置,这意味着 $_POST['cust'] 不再存在。否则,您将 $cust 定义为空值。

【讨论】:

  • "">


    跨度>
  • 我刚刚复制了您的推荐并将其粘贴在“提交 2”行之前
  • 您是否将 $custno 传递给生成该表单的函数 getnewinput() ?如果不是,则该函数无法调用该变量。
  • 是的,我从调用它的两个点将 $custno 传递给函数
  • 当提交 2 被按下时,提交 1 是否也会被重新提交?
猜你喜欢
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 2013-07-13
相关资源
最近更新 更多