0

The 1st form will ask for a customer id. The relevant customer will be extracted from a database, then the 2nd form will ask for the new (changed) address to be then updated into the database. I want all this to happen on the same page. I have no problem with the database side of things BUT I just can't get the 2nd form to work properly. Nothing happens. What am I doing wrong ? Here is a sample of the relevant code :

<!DOCTYPE html>

<head><title>Test Page</title></head> 
<body> 
<h2>Data Collection</h2><p> 

<form name="Form1" method="post" action="<?php $_SERVER[ 'PHP_SELF' ]; ?>" >
Enter Customer ID <input type="text" name="cust" >
<br>
<input type="submit" name="submit1" value="Submit ID"><br>
</form>

<form name="Form2" method="post" action="<?php $_SERVER[ 'PHP_SELF' ]; ?>" >
New Address : <input type="text" name="newaddress">
<br>
<input type="submit" name="submit2" value="Submit Address"><br>
</form>

<?php
if ( $_SERVER[ "REQUEST_METHOD" ] == 'POST' ) {
if ( isset ( $_POST[ "submit1" ] ) ) {echo $_POST["cust"];}

if ( $_SERVER[ "REQUEST_METHOD" ] == 'POST' ) {
if ( isset ( $_POST[ "Submit2" ] ) ) {echo $_POST["newaddress"];}
                    }}

?>
</body> 
</html> 

The "newaddress" does not echo when 'Submit Address' button is pressed. Karl

1 Answers1

1

you have a typo in the if statement - the Submit2 is capitalised when in your code it is not - your php should be :

if ( isset ( $_POST[ "submit2" ] ) ) {echo $_POST["newaddress"];}

and you can simplify your if statement anyway, by removing one of the if's:

<?php
   if ( $_SERVER[ "REQUEST_METHOD" ] == 'POST' ) {
        if ( isset ( $_POST[ "submit1" ] ) ) {echo $_POST["cust"];}
        if ( isset ( $_POST[ "Submit2" ] ) ) {echo $_POST["newaddress"];}
   }
?>

and also for accessibility - you should include a label that is linked to the input ID - and you should be closing your inputs as well:

<label for ="newAddress">New Address : </label>
<input type="text" id="newAddress" name="newaddress" />

and also - you seem to have an opening <p> at the end of your <h2> but no closing </p>, It should be removed:

<h2>Data Collection</h2>
gavgrif
  • 15,194
  • 2
  • 25
  • 27