Link to home
Create AccountLog in
Avatar of gingera
gingera

asked on

Form: How to show confirmation page prior to form submission?

FORM PHP MYSQL APACHE LINUX


Hello,

I am a newbie (i.e. just started to learn PHP MySQL etc.). I have coded a very simple Customer Record data entry form (codes attached). On clicking Submit button, the data is inserted into the MySQL database, and a message "Record added" is displayed.

Now, I would like to be able to check the data entered prior to it being Inserted into the MySQL database. So my questions are...

- - - - - - - - - -
Questions:
- - - - - - - - - -
Right after clicking on Submit button on a form, how do I display:
(a) the data I have just entered - for checking,
(b) "Make Alteration" button to return to the form for alteration - if a mistake has been made,
(c) "Proceed" button to insert data into MySQL database,
(d) a warning message if a record with the customer no. already exists in the MySQL database, and therefore cancel the data entry, and
(e) return to a fresh form for another data entry?


What must I change to my codes to effect the above?

Many thanks in advance.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
FILENAME: customer_data_entry.html
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
<html>
<head>
<title>Customer Data Entry</title>
</head>
 
<body>
<b>Customer Data Entry</b>
<P>
 
<form method="POST" name="customer_data_entry" action="add_customer_record.php">
 
<table width="550" border="0" cellspacing="2" cellpadding="3">
  <tr>
    <td colspan="2" bgcolor="#000000"><span class="style1">Customer Information</span></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Customer No. </td>
    <td bgcolor="#CCCCCC"><input name="customer_no" type="text" id="customer_no"></td>
  </tr>
  <tr>
    <td width="159" bgcolor="#FFCC66">First Name </td>
    <td width="373" bgcolor="#CCCCCC"><input name="first_name" type="text" id="first_name"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Last Name </td>
    <td bgcolor="#CCCCCC"><input name="last_name" type="text" id="last_name"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Sex</td>
    <td bgcolor="#CCCCCC"><select name="sex" id="sex">
      <option value="Male">Male</option>
      <option value="Female">Female</option>
    </select></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Address Line 1 </td>
    <td bgcolor="#CCCCCC"><input name="address_line_1" type="text" id="address_line_1"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Address Line 2 </td>
    <td bgcolor="#CCCCCC"><input name="address_line_2" type="text" id="address_line_2"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">City</td>
    <td bgcolor="#CCCCCC"><input name="city" type="text" id="city"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">State</td>
    <td bgcolor="#CCCCCC"><input name="state" type="text" id="state"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Post Code</td>
    <td bgcolor="#CCCCCC"><input name="post_code" type="text" id="post_code"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Country</td>
    <td bgcolor="#CCCCCC"><input name="country" type="text" id="country"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="Submit to database" /></td>
  </tr>
</table>
 
</form>
</body>
</html>
 
 
 
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
FILENAME: add_customer_record.php
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
<?php
 
  // Connect to mysql database
 
$connection = mysql_connect("localhost", "username", "password");
if (!$connection)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("customer_database", $connection);
 
 
  // Insert form values into database
 
  $query = "INSERT INTO customer_records (
`customer_no` ,
`first_name` ,
`last_name` ,
`sex` ,
`address_line_1` ,
`address_line_2` ,
`city` ,
`state` ,
`post_code` ,
`country`
)
VALUES (
'$_POST[customer_no]',
'$_POST[first_name]',
'$_POST[last_name]',
'$_POST[sex]',
'$_POST[address_line_1]',
'$_POST[address_line_2]',
'$_POST[city]',
'$_POST[state]',
'$_POST[post_code]',
'$_POST[country]'
)";
 
 
  // Confirmation Message: Error or OK message
 
if (!mysql_query($query, $connection))
  {
  die('Error: ' . mysql_error());
  }
 echo "Record added";
 
?>

Open in new window

flowchart-data-checking-on-form-.gif
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

You already have the basics.  Steps 1, 2, 3a, and 3b from your flowchart each indicate a unique page which fulfills some function in your application.  Step 4 is simply step 1 in disguise.  You just need to code each one for what they need to do.
 
With which step are you having trouble?
Avatar of gingera
gingera

ASKER

Hi routinet,

Thanks very much for your response.

I am a complete newbie in PHP and MySQL. I have absolutely NO IDEA how to code Steps 2, 3a and 3b. I am only capable of Step 1 (codes attached above).

So I need the codes to implement Steps 2-4.

Thanks again.
ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of gingera

ASKER

routinet, you really are a GENIUS!  :-) The codes work beautifully!

One small issue though... if I click "Make Changes" button, the original inputs are gone. What must I do to retain the original inputs for amendments?

These are the codes I used:



-----------------------------------------
Filename: customer_data_entry.php
-----------------------------------------
 
<html>
<head>
<title>Customer Data Entry</title>
</head>
 
<body>
<b>Customer Data Entry</b>
<P>
 
<form method="POST" name="customer_data_entry" action="verify_form.php">
 
<table width="550" border="0" cellspacing="2" cellpadding="3">
  <tr>
    <td colspan="2" bgcolor="#000000"><span class="style1">Customer Information</span></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Customer No. </td>
    <td bgcolor="#CCCCCC"><input name="customer_no" type="text" id="customer_no"></td>
  </tr>
  <tr>
    <td width="159" bgcolor="#FFCC66">First Name </td>
    <td width="373" bgcolor="#CCCCCC"><input name="first_name" type="text" id="first_name"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Last Name </td>
    <td bgcolor="#CCCCCC"><input name="last_name" type="text" id="last_name"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Sex</td>
    <td bgcolor="#CCCCCC"><select name="sex" id="sex">
      <option value="Male">Male</option>
      <option value="Female">Female</option>
    </select></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Address Line 1 </td>
    <td bgcolor="#CCCCCC"><input name="address_line_1" type="text" id="address_line_1"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Address Line 2 </td>
    <td bgcolor="#CCCCCC"><input name="address_line_2" type="text" id="address_line_2"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">City</td>
    <td bgcolor="#CCCCCC"><input name="city" type="text" id="city"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">State</td>
    <td bgcolor="#CCCCCC"><input name="state" type="text" id="state"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Post Code</td>
    <td bgcolor="#CCCCCC"><input name="post_code" type="text" id="post_code"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Country</td>
    <td bgcolor="#CCCCCC"><input name="country" type="text" id="country"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="Submit to database" /></td>
  </tr>
</table>
 
</form>
</body>
</html>
 
 
 
-----------------------------------------
Filename: verify_form.php
-----------------------------------------
 
<?
 
  // Connect to mysql database
 
$connection = mysql_connect("localhost", "username", "password");
if (!$connection)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("customer_database", $connection);
 
 
$custnum = $_POST['customer_no'];
$firstname = $_POST['first_name'];
$lastname = $_POST['last_name'];
$sex = $_POST['sex'];
$addr1 = $_POST['address_line_1'];
$addr2 = $_POST['address_line_2'];
$city = $_POST['city'];
$state = $_POST['state'];
$postcode = $_POST['post_code'];
$country = $_POST['country'];
 
// check for a duplicate customer number
$dupe_error = false;
$query = "SELECT * FROM customer_records WHERE customer_no = '" . 
    mysql_real_escape_string($custnum) . "'";
if (!($result=mysql_query($query))) {
  // query failed.  report and ...
  die();
}
if (mysql_num_rows($result) > 0) { $dupe_error = true; }
 
// now display the form
if ($dupe_error) {
  echo "<h2 style=\"color:red\">ERROR: Customer number $custnum is already in ",
      "the database.  Please create a new customer number.</h1><br /><br />";
}
?>
 
 
<html>
 
<head>
 
<script type="text/javascript">
  function add_data() {
    var o = document.forms['verify_form'];
    o.action="add_customer_record.php";
    o.submit();
    return true;
  }
</script>
 
</head>
 
<body>
 
Customer Number: <?=$custnum;?><br />
First Name: <?=$firstname;?><br />
Last Name: <?=$lastname;?><br />
Sex: <?=$sex;?><br />
Addr1: <?=$addr1;?><br />
Addr2: <?=$addr2;?><br />
City: <?=$city;?><br />
State: <?=$state;?><br />
Post Code: <?=$postcode;?><br />
Country: <?=$country;?><br />
 
<form action="customer_data_entry.php" method="POST" name="verify_form">
  <input type="hidden" name="customer_no" value="<?=$custnum;?>" />
  <input type="hidden" name="first_name" value="<?=$firstname;?>" />
  <input type="hidden" name="last_name" value="<?=$lastname;?>" />
  <input type="hidden" name="sex" value="<?=$sex;?>" />
  <input type="hidden" name="address_line_1" value="<?=$addr1;?>" />
  <input type="hidden" name="address_line_2" value="<?=$addr2;?>" />
  <input type="hidden" name="city" value="<?=$city;?>" />
  <input type="hidden" name="state" value="<?=$state;?>" />
  <input type="hidden" name="post_code" value="<?=$postcode;?>" />
  <input type="hidden" name="country" value="<?=$country;?>" />
  <input type="submit" name="goback" value="Make Changes" />
  <input type="button" name="adddata" value="Confirm Data Submission" onclick="javascript:add_data();" <?=
    (($dupe_error)?'disabled="disabled" ':'');?>/>
</form>
 
</body>
</html>
 
 
 
-----------------------------------------
Filename: add_customer_record.php
-----------------------------------------
 
<?php
 
  // Connect to mysql database
 
$connection = mysql_connect("localhost", "username", "password");
if (!$connection)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("customer_database", $connection);
 
 
  // Insert form values into database
 
  $query = "INSERT INTO customer_records (
`customer_no` ,
`first_name` ,
`last_name` ,
`sex` ,
`address_line_1` ,
`address_line_2` ,
`city` ,
`state` ,
`post_code` ,
`country`
)
VALUES (
'$_POST[customer_no]',
'$_POST[first_name]',
'$_POST[last_name]',
'$_POST[sex]',
'$_POST[address_line_1]',
'$_POST[address_line_2]',
'$_POST[city]',
'$_POST[state]',
'$_POST[post_code]',
'$_POST[country]'
)";
 
 
  // Confirmation Message: Error or OK message
 
if (!mysql_query($query, $connection))
  {
  die('Error: ' . mysql_error());
  }
 echo "Record added";
 
?>

Open in new window

SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of gingera

ASKER

Hi routinet,

I followed your example but the form no longer appears when browsed?

The codes below...

 
<html>
<head>
<title>Customer Data Entry</title>
</head>
 
<body>
<b>Customer Data Entry</b>
<P>
 
<?php
$custnum = ((isset($_POST['customer_no'])?$_POST['customer_no']:'');
$firstname = ((isset($_POST['first_name'])?$_POST['first_name']:'');
$lastname = ((isset($_POST['last_name'])?$_POST['last_name']:'');
$sex = ((isset($_POST['sex'])?$_POST['sex']:'');
$addr1 = ((isset($_POST['address_line_1'])?$_POST['address_line_1']:'');
$addr2 = ((isset($_POST['address_line_2'])?$_POST['address_line_2']:'');
$city = ((isset($_POST['city'])?$_POST['city']:'');
$state = ((isset($_POST['state'])?$_POST['state']:'');
$postcode = ((isset($_POST['post_code'])?$_POST['post_code']:'');
$country = ((isset($_POST['country'])?$_POST['country']:'');
?>
 
 
 
<form method="POST" name="customer_data_entry" action="verify_form.php">
 
<table width="550" border="0" cellspacing="2" cellpadding="3">
  <tr>
    <td colspan="2" bgcolor="#000000"><span class="style1">Customer Information</span></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Customer No. </td>
    <td bgcolor="#CCCCCC"><input name="customer_no" type="text" id="customer_no" value="<?=$custnum;?>"></td>
  </tr>
  <tr>
    <td width="159" bgcolor="#FFCC66">First Name </td>
    <td width="373" bgcolor="#CCCCCC"><input name="first_name" type="text" id="first_name" value="<?=$firstname;?>"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Last Name </td>
    <td bgcolor="#CCCCCC"><input name="last_name" type="text" id="last_name" value="<?=$lastname;?>"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Sex</td>
    <td bgcolor="#CCCCCC"><select name="sex" id="sex" value="<?=$sex;?>">
      <option value="Male">Male</option>
      <option value="Female">Female</option>
    </select></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Address Line 1 </td>
    <td bgcolor="#CCCCCC"><input name="address_line_1" type="text" id="address_line_1" value="<?=$addr1;?>"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Address Line 2 </td>
    <td bgcolor="#CCCCCC"><input name="address_line_2" type="text" id="address_line_2" value="<?=$addr2;?>"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">City</td>
    <td bgcolor="#CCCCCC"><input name="city" type="text" id="city" value="<?=$city;?>"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">State</td>
    <td bgcolor="#CCCCCC"><input name="state" type="text" id="state" value="<?=$state;?>"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Post Code</td>
    <td bgcolor="#CCCCCC"><input name="post_code" type="text" id="post_code" value="<?=$postcode;?>"></td>
  </tr>
  <tr>
    <td bgcolor="#FFCC66">Country</td>
    <td bgcolor="#CCCCCC"><input name="country" type="text" id="country" value="<?=$country;?>"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" value="Submit to database" /></td>
  </tr>
</table>
 
 
 
</form>
</body>
</html>

Open in new window

SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of gingera

ASKER

That's it! Thanks so much, Routinet.

PS: A ) is missing in Line 5.


Final version:
<?php
$custnum = ((isset($_POST['customer_no']))?$_POST['customer_no']:'');
$firstname = ((isset($_POST['first_name']))?$_POST['first_name']:'');
$lastname = ((isset($_POST['last_name']))?$_POST['last_name']:'');
$sex = ((isset($_POST['sex']))?$_POST['sex']:'');
$addr1 = ((isset($_POST['address_line_1']))?$_POST['address_line_1']:'');
$addr2 = ((isset($_POST['address_line_2']))?$_POST['address_line_2']:'');
$city = ((isset($_POST['city']))?$_POST['city']:'');
$state = ((isset($_POST['state']))?$_POST['state']:'');
$postcode = ((isset($_POST['post_code']))?$_POST['post_code']:'');
$country = ((isset($_POST['country']))?$_POST['country']:'');
?>

Open in new window

Avatar of gingera

ASKER

Thanks routinet!

I have learnt a lot!