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

时间:2016-07-07 12:57:18

标签: php html css post get

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

问题是:

  1. 当按下提交2按钮时,客户不会从enter customer no框中消失,显示的信息也会消失。
  2. 变量$custno已失去其价值。我需要这个变量来更新数据库。
  3. 为什么会这样? 我甚至对submit 1 REQUEST METHOD使用了POST,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>
    

1 个答案:

答案 0 :(得分:1)

建议您将<input type="hidden" name="cust" value="<?php echo $custno; ?>">添加到第二个表单。

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