Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How do I customize this PHP Script for Gravity forms? WordPress

  • SOLVED

I am trying to send form data to a 3rd party system via a URL query string.

I am currently using Gravity forms version 1.6.2. The tech support at gravity forms suggested I use the gform_after_submission hook and create custom PHP under the form_display.php file. They referred me to this basic script below.

01
<?php
02
add_action("gform_after_submission", "set_post_content", 10, 2);
03
function set_post_content($entry, $form){
04

05
//getting post
06
$post = get_post($entry["post_id"]);
07

08
//changing post content
09
$post->post_content = "Blender Version:" . $entry[7] . "<br/> <img src='" . $entry[8] . "'> <br/> <br/> " . $entry[13] . " <br/> <img src='" . $entry[5] . "'>";
10

11
//updating post
12
wp_update_post($post);
13
}
14
?>


My question is the following:

How do I set the URL for submission to be http://23ww.com/d.ashx?
Also I need to include each field ID.

Currently the post string looks something like this:

http://23ww.com/d.ashx?ckm_campaign_id=4&ckm_key=TQ&ckm_subid=&first_name=Joe&last_name=Smith&address=recoded+st+&address2=program+name&city=Austin&state2=TX&zip_code=78758&email_address=Accounting%40hotmail.com&phone=732&phone_type=HOME&phone_cell=732&phone2_type=CELL&best_time=&ip_address=66.60.0.0&opt_in=Y&grad_year=2003&adkey=INC04712&program_code=Criminal+Justice+%28Associate%26%23039%3Bs%29&military=No&vendor_token=176&source_code=CCI1133&affiliate_id=SEO&Express_consent=Y&AdvSearchTerm=&lead_url=google.com&server_source=192.168.01&loe=High+School+Diploma&country2=USA

I believe Gravity forms uses form field IDs like {name:3}

Once the data is passed, I would like to read the response from the third party system which should be in an XML format and only has two values.

1) "success"
2) "reject"

Based on the success response, I would like to send the user to a thank you page of www.cnn.com

If I get a reject response, I would like to send them to www.google.com for example.

Answers (1)

2011-12-21

John Cotton answers:

I can't promise this will work as I don't have Gravity Forms installed any more (I didn't like it very much ) and so can't test this. Also, you haven't told us what is in your form...

However, I recall that you get an array of fields from $form. Assuming that you have a form value for each query string parameter you need to send, something like this will work:


function set_post_content( $entry, $form ) {

$querystring = http_build_query( $form['fields'] );

$path = 'http://23ww.com/d.ashx?'. $querystring;

curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
$r = curl_exec($ch);
curl_close($ch);

if( strstr( $r, 'success' ) ) {
$location = 'http://www.cnn.com';
} else {
$location = 'http://www.google.com';
}

header( 'Location: '.$location );
exit();
}
add_action( 'gform_after_submission', 'set_post_content', 10, 2 );


I've not bothered with any sophisticated processing of the XML as you seem to only need to know whether the string contains 'success' or not...

Let me know how you get on.
JC


ksailam comments:

John - thanks for your quick reply. I agree Gravity forms needs some work. Do you suggest any other form plugins?

For the form fields, each one does have an ID. An example form screenshot is attached.

I believe this sample query string gives you the form element ids.
ckm_campaign_id=&ckm_key=&ckm_subid=&first_name={Name (First):1.3}&last_name={Name (Last):1.6}&address={address (Street Address):2.1}&address2={address (Address Line 2):2.2}&city={address (City):2.3}&state2={state2:9}&zip_code={address (Zip / Postal Code):2.5}&email_address={email_address:5}&phone={phone:23}&phone_type={phone_type:7}&phone_cell={phone_cell:24}&phone2_type={phone2_type:6}&best_time=&ip_address={ip}&opt_in={opt_in:11}&grad_year={grad_year:21}&adkey={adkey:12}&program_code={program_code:8}&military={military:15}&vendor_token={entry_id}&source_code={source_code:13}&affiliate_id={affiliate_id:16}&Express_consent={opt_in:11}&AdvSearchTerm={AdvSearchTerm:17}&lead_url={entry_url}&server_source=192.168.01&loe={loe:14}&country2={country2:22}

Let me know if this info helps.

My question on your sample would be where do I put each of the form fields to be captured and sent to the third party system?

I can send you an email to a live example if you wish.