Karl Phelan
Karl Phelan

Reputation: 21

Value in variable disappears when a second submit button is pressed

My program first requests the user to enter a customer no. This is achieved when submit 1 button is pressed. Then this customer no is used to extract information from a database and display it. Then the address of this customer has to be altered so this change is asked for on the second form and submit 2 button is pressed.

The problem is:

  1. When submit 2 button is pressed the customer no disappears from the enter customer no box and the information displayed also disappears.
  2. The variable $custno has lost its value. I need this variable to update the database.

Why does this happen? I even used POST for the submit 1 REQUEST METHOD and GET for the submit 2 REQUEST METHOD.

Here is the code I have used:

<!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>

Upvotes: 0

Views: 570

Answers (1)

Robert
Robert

Reputation: 6967

Recommend you add <input type="hidden" name="cust" value="<?php echo $custno; ?>"> to your second form.

When you submit your second form your $_POST array is reset, meaning that $_POST['cust'] no longer exists. Without that you have $cust defined as an empty value.

Upvotes: 1

Related Questions