当按下第二个提交按钮但第一个表单仍然存在时,第二个php表单消失

时间:2016-06-06 15:25:44

标签: php html forms

第一个表单要求提供客户ID。 获得该信息后,将从数据库中获取客户ID详细信息并进行显示。 第二种形式允许用户修改客户详细信息。 但是当按下第二个表单提交按钮时,第二个表单将在第一个表单保留时消失。我还需要第二种形式。 为什么会这样? 我已阅读有关此问题的答案,但仍无法解决。 以下是示例代码:

<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <br>
    <label>Enter Customer ID <input type="number" name="cust" min="1" autofocus/></label>
    <br>
    <input type="submit" name="submit1" value="Submit"/><br>
</form>

<?php
function goprintcust($custno) {
    echo "<br>"."Customer ".$custno." details are now listed here.....changes will be made below below"."<br>"; getnextform();
}

function getnextform() {
?>
<table>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
        <tr>
            <td><label>New Company Name:</td><td> <input type="text" name="newcompname"/></label></td>
        </tr>
        <tr>
            <td><label>New Address1 :</td>
            <td>  <input type="text" name="newaddress1"/></label></td>
        </tr>
        <tr>
            <td>New Address2 :</td><td>  <input type="text" name="newaddress2"/></label></td>
        </tr>
    </td>
</tr>
<tr>
    <td><label>New Address3 :</td>
    <td>  <input type="text" name="newaddress3"/></label></td>
</tr>
<tr>
    <td><label>New E-mail :</td>
    <td>  <input type="text" name="newemail"/></label></td>
</tr>
<tr>
    <td><label>New Website :</td>
    <td>  <input type="text" name="newwebsite"/></label></td>
</tr>
<tr>
    <td><label>New Phone no :</td>
    <td>  <input type="text" name="newphoneno"/></label></td>
</tr>
<tr>
    <td><input type="submit" name="submit2" value="Submit"/></td>
</tr>
<br>
</table>
</form>
<?php $y=0;
}

if ($_SERVER["REQUEST_METHOD"] == 'POST') {
    if (isset($_POST["submit1"])) {
        $custno = $_POST["cust"];
        goprintcust($custno);
    }
    if (isset($_POST["submit2"])) {
        echo "Customer changes updated";
    }
}
?>
</body>
</html>

有什么想法吗? 卡尔

1 个答案:

答案 0 :(得分:0)

只需将getnextform();添加到您的if (isset ($_POST["submit2"]))

即可

并假设您希望表单预先填写提交的数据以便于编辑,然后使用value三元组输入的isset属性。

<!DOCTYPE html>
<html>
<body>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<br>
<label>Enter Customer ID <input type="number" name="cust" min="1" autofocus/></label>
<br>
<input type="submit" name="submit1" value="Submit"/><br>
</form>

<?php
function goprintcust($custno) {echo "<br>"."Customer ".$custno." details are now listed here.....changes will be made below below"."<br>"; getnextform();}

function getnextform() {
?>
<table>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<tr><td><label>New Company Name:</td><td> <input type="text" name="newcompname" value="<?php echo isset($_POST['newcompname'])?$_POST['newcompname']:''?>"/></label></td></tr>

<tr><td><label>New Address1 :</td><td>  <input type="text" name="newaddress1" value="<?php echo isset($_POST['newaddress1'])?$_POST['newaddress1']:''?>"/></label></td></tr>

<tr><td>New Address2 :</td><td>  <input type="text" name="newaddress2" value="<?php echo isset($_POST['newaddress2'])?$_POST['newaddress2']:''?>"/></label></td></tr></td></tr>

<tr><td><label>New Address3 :</td><td>  <input type="text" name="newaddress3" value="<?php echo isset($_POST['newaddress3'])?$_POST['newaddress3']:''?>"/></label></td></tr>

<tr><td><label>New E-mail :</td><td>  <input type="text" name="newemail" value="<?php echo isset($_POST['newemail'])?$_POST['newemail']:''?>"/></label></td></tr>

<tr><td><label>New Website :</td><td>  <input type="text" name="newwebsite" value="<?php echo isset($_POST['newwebsite'])?$_POST['newwebsite']:''?>"/></label></td></tr>

<tr><td><label>New Phone no :</td><td>  <input type="text" name="newphoneno" value="<?php echo isset($_POST['newphoneno'])?$_POST['newphoneno']:''?>"/></label></td></tr>

<tr><td><input type="submit" name="submit2" value="Submit"/></td></tr><br>
    </table>
</form>
<?php $y=0;     }


if ($_SERVER["REQUEST_METHOD"] == 'POST' ) {
if (isset ($_POST["submit1"])) {$custno=$_POST["cust"]; goprintcust($custno);}
if (isset ($_POST["submit2"])) {echo "Customer changes updated";getnextform();}
                     }
?>
</body>
</html>