php - 当按下第二个提交按钮时,变量中的值消失

标签 php html css post get

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

问题是:

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

为什么会这样? 我什至将 POST 用于 submit 1 REQUEST METHOD,将 GET 用于 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>

最佳答案

推荐你加<input type="hidden" name="cust" value="<?php echo $custno; ?>">到你的第二个表格。

当您提交第二个表单时,您的 $_POST数组被重置,这意味着 $_POST['cust']不复存在。没有它你就有$cust定义为空值。

关于php - 当按下第二个提交按钮时,变量中的值消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38246096/

相关文章:

javascript - Laravel 验证在处理时间表时似乎不起作用

php - 从 Linux 命令行检查有效的 docx

css - 我可以用 CSS 完成这个吗?

javascript - jquery 脚本在桌面上工作,但在 iphone 上滞后

php - 如何在 GPU 上启用 php 运行 python 脚本?

php - 我应该如何使用 Zend Framework 创建我的 HTML5 Manifest?

html - 将背景颜色限制为标题宽度

html - Bootstrap 3 Column Site {高度 : 100%;} not working

css - 水平居中

php - 每次打印从a到z的字母