我的程序首先要求用户输入客户号码。这是在按下提交1按钮时实现的。然后,此客户号用于从数据库中提取信息并显示它。然后必须更改此客户的地址,以便在第二个表单上要求进行此更改,并按下提交2按钮。
问题是:
enter customer no
框中消失,显示的信息也会消失。$custno
已失去其价值。我需要这个变量来更新数据库。为什么会这样?
我甚至对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>
答案 0 :(得分:1)
建议您将<input type="hidden" name="cust" value="<?php echo $custno; ?>">
添加到第二个表单。
当您提交第二张表单时,您的$_POST
数组会重置,这意味着$_POST['cust']
不再存在。没有它,您将$cust
定义为空值。