ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  PHP Form Handling
Subject:   Processing 2 Forms On One Page
Date:   2007-10-28 19:33:00
From:   jblifesetyles
Hi all... first, thanks in advance for the help.


I have a php page with 2 forms one for creating a country, and another for creating a liason. The liason has to be assigned to a country that has been created in the first form.


I get the form to load and everything, but it keeps posting blank rows back to the database. I'm a relative newb, hence the probably simple solution.


Here's the full code for my form(s)


[code]
<?php


/* Make sure we have admin rights */


require_once('admin-header.php');
require_once('admin.php');


if (!current_user_can('edit_users') )
wp_die(__('Cheatin’ uh?'));


/* Posting to Countries */


if (array_key_exists('gl_country_submit', $_POST)) {
$addcountry = "INSERT INTO gl_countries VALUES('$country_id','$country_name','$flag_url','$thumb_url')";


mysql_query($addcountry);


print_r($addcountry);
}


/* Posting to Liasons */


if (array_key_exists('gl_liason_submit', $_POST)) {
$addliason = "INSERT INTO gl_liasons VALUES('$liason_id','$country_id2','$liason_firstname','$liason_lastname','$organization','$email','$site_url','$img_url','$address1','$address2','$address3','$phone','$notes')";


mysql_query($addliason);


print_r($addliason);
}


?>


<!-- Form to add a country -->


<div class="wrap">
<h2>Add Country</h2>
<form method="post" action="<?php echo $PHP_SELF;?>">
<input type="hidden" name="country_id" value="null">
<table>
<tr>
<td align="left">Country</td>
<td><input type="text" name="country_name"></td>
</tr>
<tr>
<td align="left">Flag URL</td>
<td><input type="url" name="flag_url"></td>
</tr>
<tr>
<td align="left">Flag Thumbnail URL</td>
<td><input type="url" name="thumb_url"></td>
</tr>
<tr>
<td colspan="4">


<input type="submit" value="Add Country" name="country_submit"></td>
</tr>
<input type="hidden" name="_submit_check_country" value="1"/>
<input type="hidden" name="gl_country_submit" value="process">
</table>
</form>


<?php
if (array_key_exists('_submit_check_country', $_POST)) {
echo("<?php $country_name?> is now svailable for Liasons!");
}
?>
</div>


<!-- Form to add a liason -->


<div class="wrap">
<h2>Add Liason</h2>
<form method="post" action="<?php echo $PHP_SELF;?>" >
<input type="hidden" name="liason_id" value="null">
<table>
<tr>
<td align="left">Country</td>
<td><?php
$query = "SELECT * FROM gl_countries";
$results = mysql_query($query);
echo '<select name="country_id">';
while ($thisrow = mysql_fetch_array($results))
{
echo '<option value="'.$thisrow['country_id2'].'">'.$thisrow['country_name'].'</option>';
}
echo '</select>';?></td>
</tr>
<tr>
<td align="left">First Name</td>
<td><input type="text" name="liason_firstname"></td>
</tr>
<tr>
<td align="left">Last Name</td>
<td><input type="text" name="liason_lastname"></td>
</tr>
<tr>
<td align="left">Organization</td>
<td><input type="text" name="organization"></td>
</tr>
<tr>
<td align="left">E-Mail Address</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td align="left">Website / Blog</td>
<td><input type="url" name="site_url"></td>
</tr>
<tr>
<td align="left">Photo URL</td>
<td><input type="url" name="img_url"></td>
</tr>
<tr>
<td align="left">Address 1</td>
<td><input type="text" name="address1"></td>
</tr>
<tr>
<td align="left">Address 2</td>
<td><input type="text" name="address2"></td>
</tr>
<tr>
<td align="left">Address 3</td>
<td><input type="text" name="address3"></td>
</tr>
<tr>
<td align="left">Phone #</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td align="left">Notes</td>
<td><TEXTAREA rows="5" cols="18" name="notes" wrap="physical"></TEXTAREA></td>
</tr>
<tr>
<td colspan="4">


<input type="submit" value="Add Liason" name="liason_submit"></td>
</tr>
<input type="hidden" name="_submit_check_liason" value="1"/>
<input type="hidden" name="gl_liason_submit" value="process">
</table>
</form>


<?php
if (array_key_exists('_submit_check_liason', $_POST)) {
echo("<?php $liasons_firstname $liason_lastname?> has been added!");
}
?>


</div>
[/code]