Need to post form data to an external URL

Hi Everyone,

I am not a complete newbie, but I am having a problem posting data from my contact form to an external URL. Once the data gets to the URL, it will be added to a database by their code. But I can’t figure out how to get the data to them. I have two php files. One that calls all of the functions – form, error checking, and sending an email with the data in it. The second file contains all of the functions that do each of the tasks called on the first page. That is all working perfectly.
However, now I need to add a function that will send the data to an external url in addition to sending it to the email. That’s where I’m running into trouble. I would appreciate any help anyone can provide with this. I don’t have any experience with this aspect of using php.

Thanks so much in advance.

Please post your form code.

Hi chasemarketing,

My code is pretty long and in two different files, do you want me to post it all here or can I send the files directly to your email?
DebsWebs

Hi there,

To better assist you and to give you the best possible answers from all members of this forum, please post your code here. It doesn’t matter how long your code is, just post each file in separate PHP blocks so we may help you as best as possible.

Thanks.

OK. Here it is:

My first bit of code is in a file that only calls the various functions. Here it is:
[php]<?php
include(‘includes/contact_form.inc.php’);
?>[/php]

Contact Information

Please use the convenient form below to contact us.

	[php]<?php
		switch($_GET[action]) {
			case "check";
			$errors=checkForm();
			if($errors!="ok") {

// print “

”;print_r($errors);print “
”; // using for checking the values in the errors array.
echo ’

Error!


The following error(s) occurred:

’;
foreach ($errors as $message) { // Print each error individually.
echo " - $message
\n";
}
echo ‘

Please try again.

’;
emailForm();
} else {
sendEmail();
}
break;
default:
emailForm();
break;
}
?>
[/php]

The second file is called from the first bit of code above and contains all of the form formatting and the validation as well as sending the form data to an email address. I need to send the form data to an external url in addition to sending it to the email address:
[php]

<? //contact_form.inc.php function emailForm() { print "
  1. Who Should We Contact?
  2. First Name:required
  3. Last Name:required
  4. E-mail address:required
  5. Phone:required
  6. Alternate Phone:
  7. Fax:
  8. Street address 1:required
  9. Street address 2:
  10. City:required
  11. State:required
  12. Zip Code:required

  1. Additional Information
  2. Arrival Date: Unknown January February March April May June July August September October November December 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 2011 2012 2013 2014 2015
  3. Departure Date: Unknown January February March April May June July August September October November December 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 2011 2012 2013 2014 2015


  4. What Information Would You Like?required
  5.   Brochure & Rates for the Hotel
      Information about The Conference Center
      Information for family reunions, church or school events
      Information on conference and business meetings
  6. Your questions or comments.required
  7. $_POST[Message]
  8. Required Fields are marked withrequired
   "; } function checkForm() { $errors=array(); //Initialize array. if(strlen($_POST[FirstName]) < 1) { //Check that field is filled in. $errors[] = "Please enter your first name."; } if(strlen($_POST[LastName]) < 1) { //Check that field is filled in. $errors[] = "Please enter your last name."; } if(strlen($_POST[HomePhone]) < 1) { //Check that field is filled in. $errors[] = "Please enter your phone number."; } if(strlen($_POST[EmailAddress]) < 1) { //Check that field is filled in. $errors[] = "Please provide your email address."; } if(strlen($_POST[Address1]) < 1) { //Check that field is filled in. $errors[] = "Please provide your street address."; } if(strlen($_POST[City]) < 1) { //Check that field is filled in. $errors[] = "Please provide your city."; } if(strlen($_POST[State]) < 1) { //Check that field is filled in. $errors[] = "Please provide your state."; } if(strlen($_POST[ZipCode]) < 1) { //Check that field is filled in. $errors[] = "Please provide your zip code."; } if(strlen($_POST[Message]) < 1) { //Check that field is filled in. $errors[] = "You did not enter a message in the 'message' text box."; } if($_POST[surname] !='') { //Check whether field is filled in. If it is, then don't send the email. exit(); } if(checkEmail($_POST[EmailAddress]) == "bad email") { //Check that email is in the correct form using checkEmail function. $errors[] = "Your email address is not valid. Enter a complete email address using the format: [email protected].";} if(checkMessage($_POST[Message]) == "bad message") {//Check for urls in the message content. $errors[] = "No Website URLs are permitted in the text field. If you need to send a link please do so without using the 'http://'. Just begin your link with 'www'."; } if (empty($errors)) { // if errors array is empty. return "ok"; } else { return $errors; } // print "
";var_dump($errors);print "
"; } // end function checkForm. function checkEmail($email) { if(!eregi("^([-!#\$%&'*+./0-9=?A-Z^_`a-z{|}~])+@([-!#\$%&'*+/0-9=?A-Z^_`a-z{|}~]+\\.)+[a-zA-Z]{2,6}\$",$email)) { return "bad email"; } } function checkMessage($message) { if (preg_match("/http/i",$message)) { return "bad message"; exit(); } } function sendEmail() { $product = $_POST['product']; if($product) { foreach ($product as $p) $products = implode( "\n", $product); } else { $products="No information selected."; } $contact = $_POST['contact']; $from = $_POST['email']; $body= "A request for information has been submitted from the Website:\n\n"; $body.="CONTACT INFORMATION\n\n"; $body.="First Name: $_POST[FirstName]\n"; $body.="Last Name: $_POST[LastName]\n"; $body.="Email: $_POST[EmailAddress]\n"; $body.="Phone Number: $_POST[HomePhone]\n"; $body.="Cell Phone: $_POST[CellPhone]\n"; $body.="Fax: $_POST[fax]\n\n"; $body.="ADDRESS\n\n"; $body.="Street1: $_POST[Address1]\n"; $body.="Street2: $_POST[Address2]\n"; $body.="City: $_POST[City]\n"; $body.="State: $_POST[State]\n"; $body.="Zip Code: $_POST[ZipCode]\n\n"; $body.="------------------------------------------------------------------\n\n"; $body.="Arrival Date: $_POST[month1] $_POST[day1], $_POST[year1]\n"; $body.="Departure Date: $_POST[month2] $_POST[day2], $_POST[year2]\n\n"; $body.="------------------------------------------------------------------\n\n"; $body.="REQUESTED INFORMATION:\n\n"; $body.="{$products}\n\n"; $body.="------------------------------------------------------------------\n\n"; $body.=stripslashes("MESSAGE: $_POST[Message]\n"); $headers = "From: ".$from; $ok = @mail('[email protected]', 'Information inquiry from the Website', $body, $headers); if ($ok) { print "
thank you graphic"; } else { print "
Mail could not be sent. Sorry! Please try again.
";} } ?>[/php]

Thanks so much.

Hi,

Thank you for that. Firstly, does your email function work fine? not that I see any problem with it… Although I haven’t looked at it that thoroughly.

Secondly, what exactly do you mean by “send the data to an external URL”? What will it be received by? Do you mean to POST the data?

Hi chasemarketing,

My email form is working perfectly. The only thing I really need is to POST the form data to a new url. This is for a company that tracks where visitors to the site come from. So they need to contact form data sent to their site along with the account number for my clients and the tracking keyword that tells them where the site visitor came from.

Here are the instructions I was given:
Javascript Dynamic Toll Free Numbers and Contact Us Forms (Preferred)
Please use the following instructions to integrate Javascript dynamic coding with a contact us form:

  1. Add a hidden field to the form, with the name „keyword‟ and the id „hidNavisKeyword‟. Set the value of this field to the NAVIS keyword of your main website default campaign that is used when a guest searches you organically. That keyword will most likely be “organic”.
  2. After the tag, add the following code, replacing the „defaultkeyword‟ with the keyword of your main website default campaign. (Most likely “organic”.)
  1. If your contact us form is posting directly to the NAVIS site, you‟re done. If you‟re processing the form yourself on your server, you need to make sure that the value in this hidden field is sent to our site along with the other data. If you were previously sending us a hard-coded keyword or campaign name with the data, please discontinue doing so.
    Integrating Contact Data in NAVIS Narrowcast - Form Submission Method
    If you are unable to use the Web Service you may configure your website to post the information directly to the following page:
    http://www.NavisTechnologies.info/Narrowcast2005/ELM/ELMContactPost.aspx
    If you do so, either GET or POST are supported as the form method, though POST is strongly preferred, as GET has total size limitations that may cause loss of data.
    Before you use this page, you will need to provide your NAVIS Client Advocate with the redirect URL that you would like the user to be redirected to after they submit the information to this page. Typically this will be the URL of a page on your site that thanks the user for their submission.
    The only required field in this case is Account. You do not supply a password in this method since it would then be available to anyone who visited the page. All other fields are optional, and can be omitted if not required. The value of boolean fields must be set to “yes” to evaluate to true, otherwise they will be false. The fields are matched on the exact field name, so it is vital that you name your form elements correctly. Please refer to the data field specification (below) for the exact name of the fields, and the maximum string lengths of string fields.
    You can also optionally provide the value „SendEmail=Yes‟ if you want Navis to send a copy of the form submission values to an email address separate from ELM. To specify this email address, contact your NAVIS Client Advocate. You will not want this address to forward to the following address either directly or indirectly: <YourNAVISAccount#>@navismarketing.

Then they gave me the Data Field Specifications. I have already updated my form to reflect the proper field names.

I know this is a lot of information, and I understand most of it, I just don’t know how to implement it.

Thanks so much for all of your help – I truly appreciate it.

Sponsor our Newsletter | Privacy Policy | Terms of Service