Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submission to 2 forms at once 1

Status
Not open for further replies.

shamrox7

Programmer
Aug 1, 2007
14
US
I have a very long form that the user inputs data about a client. Within the form are fields for various contacts for that client company.
Am I able to use one submit and have it submit some of the fields to the client table and some of the fields data to the contacts table? At the same time, I'd need to link the ID that is autogenerated from the submit into the client table, so that they are linked later for use. (the id would be added as a field in the contact table)
Can this be done, and if so, what might the code look like?

PHP/MYSQL in usage. Thanks.
 
you cannot direct a form to more than one script (web page). the action attribute of a form tag tells the browser to send (by either get or post) all the data to that page.

you could write some javascript to send the values of the form to one page and then to another page, sequentially.

but, the same effect is very easily provided for within php.

consider this

Code:
<form method="post" action="processForm.php">
<input type="text" name="firstName"/><br/>
<input type="text" name="lastName"/><br/>
<textarea name="address"></textarea<br/>
<input type="submit" name="submit" value="submit"/>
</form>
Code:
if (isset($_POST['submit'])){
  connectToDb('myhostname','myusername', 'mypassword', 'mydatabase');

  //run one script
  $id = saveNameData();

  //run the second script
  saveAddressData($id);
  echo "all saved";
} else {
 header ('Location: index.html');
}
function saveNameData(){
 $query = "Insert into nametable set firstName=".clean($_POST['firstName']).", lastName=".clean($_POST['lastName']);
 mysql_query($query) or die(mysql_error());
 return mysql_insert_id();
}
function saveAddressData($id=NULL){
 if(empty($id)) die("No foreign key");
 $query = "Insert into addresstable set contactID=$id, $address=".clean($_POST['address']);
 mysql_query($query) or die(mysql_error());
}
function clean($data){
 $f = function_exists('mysql_real_escape_string') ? 'mysql_real_escape_string' : 'mysql_escape_string';
 return "'".$f($data)."'";
}
function connectToDb($host, $user, $pwd, $database){
 mysql_connect($host, $user, $pwd) or die (mysql_error());
 mysql_select_db($database) or die(mysql_errod());
}
 
Ok, that makes perfect sense. Thank you for spelling it out so clearly. I'll give it a go.
 
Ok, I took a run at the method you suggested, but I am running into a brick wall. for some reason, when I submit the form, it tries to load the processing page and gives me a 404 error. I have uploaded the file and verified it's exisitance. Would something in my code below be causing this? Also, do I have my code written correctly?
Thanks.

Code:
<?php require_once('../Connections/spectrum.php'); ?>
<?php  include_once "../mainfile.php"; //load the global vars and functions 

if (isset($_POST['submit'])){
  mysql_select_db($database_spectrum, $spectrum)or die(mysql_errod());

  //run one script
  $id = saveNameData();

  //run the second script
  saveContactData($id);
  echo "all saved";
} else {
 header ('Location: index.html');
}
function saveNameData(){
 $query = "Insert into spec_client set name=".clean($_POST['name']).", address1=".clean($_POST['address1']).", address2=".clean($_POST['address2']).", city=".clean($_POST['city']).", state=".clean($_POST['state']).", zip=".clean($_POST['zip']).", salesrepid=".clean($_POST['salesrepid']).", billingsendto=".clean($_POST['billingsendto']).", billingaddress1=".clean($_POST['billingaddress1']).", billingaddress2=".clean($_POST['billingaddress2']).", billingcity=".clean($_POST['billingcity']).", billingstate=".clean($_POST['billingstate']).", billingzip=".clean($_POST['billingzip']).", billingnotes=".clean($_POST['billingnotes']).", accountsname=".clean($_POST['accountsname']).", accountsphone=".clean($_POST['accountsphone']).", accountsemail=".clean($_POST['accountsemail']).", accountsfax=".clean($_POST['accountsfax']).", accountssecname=".clean($_POST['accountssecname']).", accountssecphone=".clean($_POST['accountssecphone']).", accountssecemail=".clean($_POST['accountssecemail']).", accountssecfax=".clean($_POST['accountssecfax']).", accountsnotes=".clean($_POST['accountsnotes']).", officelocationcity=".clean($_POST['officelocationcity']).", officelocationstate=".clean($_POST['officelocationstate']).", officelocationnotes=".clean($_POST['officelocationnotes']).", bookingonfile=".clean($_POST['bookingonfile']).", bookingagreeneed=".clean($_POST['bookingagreeneed']).", bookingsendto=".clean($_POST['bookingsendto']).", bookingemail=".clean($_POST['bookingemail']).", bookingfax=".clean($_POST['bookingfax']).", bookingnotes=".clean($_POST['bookingnotes']).", relationshipnotes=".clean($_POST['relationshipnotes']).", discountpublic=".clean($_POST['discountpublic']).", discountprivate=".clean($_POST['discountprivate']).", discounteclass=".clean($_POST['discounteclass']).", discountnotes=".clean($_POST['discountnotes']).", additionalnotes=".clean($_POST['additionalnotes']);
 mysql_query($query) or die(mysql_error());
 return mysql_insert_id();
}
function saveContactData($id=NULL){
 if(empty($id)) die("No foreign key");
 $query = "Insert into spec_contacts set companyid=$id, $firstname=".clean($_POST['firstname'])." lastname=".clean($_POST['lastname']).", address1=".clean($_POST['c_address1']).", address2=".clean($_POST['c_address2']).", city=".clean($_POST['c_city']).", state=".clean($_POST['c_state']).", zip=".clean($_POST['c_zip']).", notes=".clean($_POST['c_notes']);
 mysql_query($query) or die(mysql_error());
}
function clean($data){
 $f = function_exists('mysql_real_escape_string') ? 'mysql_real_escape_string' : 'mysql_escape_string';
 return "'".$f($data)."'";
}
?>
 
i have not looked at your code as if you are getting a 404 error then there is a problem further up the chain. the file does not exist at the position relative to the webroot that you think. this could be because you have put the file somewhere else or because your webserver may be case sensitive.
 
Can't make rhyme or reason out of it. It's on the server, everything matches, but still getting 404.
As far as syntax goes, did I get it right?
 
looks ok but mysql_errod should be mysql_error in the mysql_select_db line.

bit difficult to tell though, until you are able actually to run it.

is it possible that the 404 is given over the index.html file rather than the processForm file? if that's the case then change the header line to an absolutely qualified url (
 
Ok, that's exactly what was happening with the 404. So now I at least know that the else statement is kicking it over to index.html. Is there anyway to debug and find out why??
 
the submit value is not set. perhaps you called it Submit or some other name. note vars are caps sensitive.

debug methodology would include dumping the contents of $_POST to the screen. this will break the redirect but that's fine for debugging.

Code:
echo "<pre>".print_r($_POST, true)."</pre>";
 
Well it turns out it was the submit button didn't have the name set. It's always the little things right?

thanks a ton for taking the time to work me through this.
 
I'm guessing my code is still flawed on the process page, because I have dumped the $_POST to the screen, it's all there, but it fails after that. Nothing is submitted to the database.
 
And here is the entire code for the process page. Perhaps you can see an error in my code that I'm missing.

Code:
<?php require_once('../Connections/spectrum.php'); ?>
<?php  include_once "../mainfile.php"; //load the global vars and functions 
echo "<pre>".print_r($_POST, true)."</pre>";
if (isset($_POST['submit'])){

  //run one script
  $id = saveNameData();

  //run the second script
  saveContactData($id);
  echo "all saved";
} else {
 header ('Location: index.html');
}
function saveNameData(){
 $query = "Insert into spec_client set name=".clean($_POST['name']).", address1=".clean($_POST['address1']).", address2=".clean($_POST['address2']).", city=".clean($_POST['city']).", state=".clean($_POST['state']).", zip=".clean($_POST['zip']).", salesrepid=".clean($_POST['salesrepid']).", billingsendto=".clean($_POST['billingsendto']).", billingaddress1=".clean($_POST['billingaddress1']).", billingaddress2=".clean($_POST['billingaddress2']).", billingcity=".clean($_POST['billingcity']).", billingstate=".clean($_POST['billingstate']).", billingzip=".clean($_POST['billingzip']).", billingnotes=".clean($_POST['billingnotes']).", accountsname=".clean($_POST['accountsname']).", accountsphone=".clean($_POST['accountsphone']).", accountsemail=".clean($_POST['accountsemail']).", accountsfax=".clean($_POST['accountsfax']).", accountssecname=".clean($_POST['accountssecname']).", accountssecphone=".clean($_POST['accountssecphone']).", accountssecemail=".clean($_POST['accountssecemail']).", accountssecfax=".clean($_POST['accountssecfax']).", accountsnotes=".clean($_POST['accountsnotes']).", officelocationcity=".clean($_POST['officelocationcity']).", officelocationstate=".clean($_POST['officelocationstate']).", officelocationnotes=".clean($_POST['officelocationnotes']).", bookingonfile=".clean($_POST['bookingonfile']).", bookingagreeneed=".clean($_POST['bookingagreeneed']).", bookingsendto=".clean($_POST['bookingsendto']).", bookingemail=".clean($_POST['bookingemail']).", bookingfax=".clean($_POST['bookingfax']).", bookingnotes=".clean($_POST['bookingnotes']).", relationshipnotes=".clean($_POST['relationshipnotes']).", discountpublic=".clean($_POST['discountpublic']).", discountprivate=".clean($_POST['discountprivate']).", discounteclass=".clean($_POST['discounteclass']).", discountnotes=".clean($_POST['discountnotes']).", additionalnotes=".clean($_POST['additionalnotes']);
mysql_select_db($database_spectrum, $spectrum) or die(mysql_error());
mysql_query($query) or die(mysql_error());
return mysql_insert_id();
}
function saveContactData($id=NULL){
 if(empty($id)) die("No foreign key");
 $query = "Insert into spec_contacts set companyid=$id, firstname=".clean($_POST['firstname']).", lastname=".clean($_POST['lastname']).", address1=".clean($_POST['c_address1']).", address2=".clean($_POST['c_address2']).", city=".clean($_POST['c_city']).", state=".clean($_POST['c_state']).", zip=".clean($_POST['c_zip']).", notes=".clean($_POST['c_notes']);
mysql_select_db($database_spectrum, $spectrum) or die(mysql_error());
mysql_query($query) or die(mysql_error());
}
function clean($data){
 $f = function_exists('mysql_real_escape_string') ? 'mysql_real_escape_string' : 'mysql_escape_string';
 return "'".$f($data)."'";
}

?>
 
there are some data points you are adding that don't appear to exist in the form. e.g.
Code:
bookingagreeneed=".clean($_POST['bookingagreeneed']).",

where do these vars come from?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top