Page 1 of 1

Post and Get - Simultaneously?

Posted: Thu May 20, 2010 7:07 am
by tike0rz
I have a basic form. 10 fields, one of which is a menu box. Name(textfield), Address(textfield), Phone number(textfield), County(menu box), etc., on a register.php page.

Upon submitting their information to the DB, I would like to have the client confirm correctness. So, I have a confirm correctness page(confirm.php), and an option to post to the DB barring that all of their information that they see is correct(thanks.php).

I have ONE issue. When the user clicks "NO" from the confirm.php page (meaning one or more fields are not correct), I have them taken back to a page (fix.php) with their information previously entered, and ready for fixing. All field values are transferred back to this form page EXCEPT for one field, the menu box. Why?

Here is a code snippet:

Confirm.php

Code: Select all

<?php
$NAME_FIRST = $_POST['NAME_FIRST'];
$NAME_LAST = $_POST['NAME_LAST'];
$EMAIL_ADDR = $_POST['EMAIL_ADDR'];
$STREET = $_POST['STREET'];
$APT_NO = $_POST['APT_NO'];
$CITY = $_POST['CITY'];
$STATE = $_POST['STATE'];
$ZIP_CODE = $_POST['ZIP_CODE'];
$COUNTY = $_POST['COUNTY'];
$PHONE = $_POST['PHONE'];

$username = "**";
$password = "**";
$hostname = "**";

$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); //establish connection with DB

$selectdb = mysql_select_db("carpool_db",$dbh) or die("Could not select the database.");//selecting db

echo "<BR><BR>name first: $NAME_FIRST<BR>";
echo "name last: $NAME_LAST<BR>";
echo "email address: $EMAIL_ADDR<BR>";
echo "street: $STREET<BR>";
echo "apt no: $APT_NO<BR>";
echo "city: $CITY<BR>";
echo "state: $STATE<BR>";
echo "zip: $ZIP_CODE<BR>";
echo "county: $COUNTY<BR>";
echo "phone: $PHONE<BR>";

echo "<br>Is this information correct?<br>";
echo "<table width='200' border='1'";
echo "<tr><td><a href='thanks.php?NAME_FIRST=$NAME_FIRST&NAME_LAST=$NAME_LAST&EMAIL_ADDR=$EMAIL_ADDR&STREET=$STREET&APT_NO=$APT_NO&CITY=$CITY&STATE=$STATE&ZIP_CODE=$ZIP_CODE&COUNTY=$COUNTY&PHONE=$PHONE'>Yes</a></td>";
echo "<tr><td><a href='fix.php?NAME_FIRST=$NAME_FIRST&NAME_LAST=$NAME_LAST&EMAIL_ADDR=$EMAIL_ADDR&STREET=$STREET&APT_NO=$APT_NO&CITY=$CITY&STATE=$STATE&ZIP_CODE=$ZIP_CODE&COUNTY=$COUNTY&PHONE=$PHONE'>No</a></td></table>";

?>
Fix.php

Code: Select all

<?php
$NAME_FIRST = $_GET['NAME_FIRST'];
$NAME_LAST = $_GET['NAME_LAST'];
$EMAIL_ADDR = $_GET['EMAIL_ADDR'];
$STREET = $_GET['STREET'];
$APT_NO = $_GET['APT_NO'];
$CITY = $_GET['CITY'];
$STATE = $_GET['STATE'];
$ZIP_CODE = $_GET['ZIP_CODE'];
$COUNTY = $_POST['COUNTY'];
$PHONE = $_GET['PHONE'];

print "<form name='theForm' action='confirm.php' method='POST'>";
print "<table width='300px' border='1'>Edit your Information:<br><tr><td>First Name: </td><td><input type='text' value='$NAME_FIRST' name='NAME_FIRST'></td></tr>";
print "<tr><td>Last Name:  </td><td><input type='text' value='$NAME_LAST' name='NAME_LAST'></td></tr>";
print "<tr><td>Email Address: </td><td><input type='text' value='$EMAIL_ADDR' name='EMAIL_ADDR'></td></tr>";
print "<tr><td>Street Address: </td><td><input type='text' value='$STREET' name='STREET'></td></tr>";
print "<tr><td>Apartment Number: </td><td><input type='text' value='$APT_NO' name='APT_NO'></td></tr>";
print "<tr><td>City: </td><td><input type='text' value='$CITY' name='CITY'></td></tr>";
print "<tr><td>State: </td><td><input type='text' value='$STATE' name='STATE'></td></tr>";
print "<tr><td>Zip Code: </td><td><input type='text' value='$ZIP_CODE' name='ZIP_CODE'></td></tr>";
print "<tr><td>County:  </td><td><select name='COUNTY' value='$COUNTY'>
    <option value=''>Select a County</option>
    <option value='Atlantic'>Atlantic</option>
    <option value='Bergen'>Bergen</option>
    <option value='Burlington'>Burlington</option>
    <option value='Camden'>Camden</option>
    <option value='Cape_May'>Cape May</option>
    <option value='Cumberland'>Cumberland</option>
    <option value='Essex'>Essex</option>
    <option value='Gloucester'>Gloucester</option>
    <option value='Hudson'>Hudson</option>
    <option value='Hunterdon'>Hunterdon</option>
    <option value='Mercer'>Mercer</option>
    <option value='Middlesex'>Middlesex</option>
    <option value='Monmouth'>Monmouth</option>
    <option value='Morris'>Morris</option>
    <option value='Ocean'>Ocean</option>
    <option value='Passaic'>Passaic</option>
	<option value='Philadelphia'>Philadelphia</option>
	<option value='Salem'>Salem</option>
    <option value='Somerset'>Somerset</option>
    <option value='Sussex'>Sussex</option>
    <option value='Union'>Union</option>
    <option value='Warren'>Warren</option>
</select></td></tr>";
print "<tr><td>Phone Number: </td><td><input type='text' value='$PHONE' name='PHONE'></td></tr>";
print "</table><input type='submit'></form>";

?>
Thanks in advance.

Re: Post and Get - Simultaneously?

Posted: Thu May 20, 2010 12:09 pm
by Chalks
to make a select option be selected, you have to make it like so:

Code: Select all

    <option value='Atlantic' selected='selected'>Atlantic</option>
Which means that in order to reset your form, you will need a test for every single option that says "if returned county is this county, print selected='selected' in the option tag".

It's a pain.

Re: Post and Get - Simultaneously?

Posted: Thu May 20, 2010 5:55 pm
by John Cartwright
Which is why many people will use some kind of view helper to render html inputs.. a quick and dirty example would be:

Code: Select all

function html_form_select($name, $data, $selectedvalue) {
   $html =  '<select name="'. $name .'">';
   foreach ($data as $key => $value) {
      $html .= '<option value="'. $key .'" '. ($key == $selectedvalue ? 'selected="selected"' : '') .'>'. $value .'</option>';
   }
   $html .= '</select>';

   return $html;
}

function html_form_input($name, $value) {
   return '<input name="'. $name .'" type="input" value="'. $value .'">';
}

//etc

$countydata = array(
   '' => 'Select a County',
   'Atlantic' => 'Atlantic',
   'Bergen' => 'Bergen'
   'Burlington' => 'Burlington',
   'Camden' => 'Camden'
   //etc
);

//input field
echo html_form_input('EMAIL_ADDR', !empty($_POST['EMAIL_ADDR']) ? $_POST['EMAIL_ADDR'] : '');

//select field
echo html_form_select('COUNTY', $countydata , !empty($_POST['COUNTY']) ? $_POST['COUNTY'] : '');