2016-07-07 56 views
0

我的程序首先請求用戶輸入客戶編號。按下提交1按鈕即可實現此功能。然後,該客戶編號用於從數據庫提取信息並顯示它。然後該客戶的地址必須被更改,以便在第二個表單上詢問這個更改,並按下提交2按鈕。當按下第二個提交按鈕時,變量中的值消失

的問題是:

  1. 當提交2按下按鈕,顧客不從enter customer no框消失,顯示的信息也消失了。
  2. 變量$custno已失去其價值。我需要這個變量來更新數據庫。

這是爲什麼發生? 我甚至使用POST爲submit 1 REQUEST METHODGETsubmit 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

第一次提交表單提交'數據A'時,您第二次提交表單'數據B'時,除非您同時提交了'數據A'和數據B',然後'數據A'丟失了。 (或將其存儲在其他地方,如Cookie或會話)。 – Epodax

回答

1

建議您添加<input type="hidden" name="cust" value="<?php echo $custno; ?>">到你的第二個形式。

當您提交第二個表單時,您的$_POST數組將被重置,這意味着$_POST['cust']不再存在。沒有這個,你有$cust定義爲一個空值。

+0

<形式方法= 「GET」 行動= 「<?PHP的回聲用htmlspecialchars($ _ SERVER [」 PHP_SELF 「]);>?」 「> ​​
​​
我只是複製 「> ​​

+0

你能告訴我們你的代碼的更新版本? –

+0

你的建議並粘貼在'submit 2'行之前 –

相關問題