我的程序首先請求用戶輸入客戶編號。按下提交1按鈕即可實現此功能。然後,該客戶編號用於從數據庫提取信息並顯示它。然後該客戶的地址必須被更改,以便在第二個表單上詢問這個更改,並按下提交2按鈕。當按下第二個提交按鈕時,變量中的值消失
的問題是:
- 當提交2按下按鈕,顧客不從
enter customer no
框消失,顯示的信息也消失了。 - 變量
$custno
已失去其價值。我需要這個變量來更新數據庫。
這是爲什麼發生? 我甚至使用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>
第一次提交表單提交'數據A'時,您第二次提交表單'數據B'時,除非您同時提交了'數據A'和數據B',然後'數據A'丟失了。 (或將其存儲在其他地方,如Cookie或會話)。 – Epodax