私のプログラムは、最初にユーザーに顧客番号を入力するよう要求します。これは、サブミット1ボタンが押されたときに達成されます。この顧客は、データベースから情報を抽出して表示するために使用されます。その後、この顧客の住所を変更して、この変更に2番目のフォームが要求され、送信2ボタンが押されなければなりません。2番目のサブミットボタンを押すと変数の値が消えます
問題がある:
- 提出2ボタンが押されると、顧客がないが
enter customer no
ボックスから消え、表示された情報も表示されなくなります。 - 変数
$custno
の値が失われました。この変数を使用してデータベースを更新する必要があります。
どうしてですか? 私はsubmit 1 REQUEST METHOD
の場合はPOSTを使用し、submit 2 REQUEST METHOD
の場合はGET
を使用しました。ここで
私が使用しているコードです:
<!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」フォームを提出すると、2回目に「データB」フォームが送信されます。また、「データA」と「データB」、「データA」と一緒に提出していないかぎり失われます。 (または、Cookieやセッションのような別の場所に保存してください)。 – Epodax