How do I have one form save two leads | Community
Skip to main content
March 4, 2015
Question

How do I have one form save two leads

  • March 4, 2015
  • 9 replies
  • 4295 views
I have a form with fields like name, email, zipcode, guest name, guest email, guest zipcode. How would I get the "guest" fields to save as a separate lead?

I was directed to http://developers.marketo.com/blog/server-side-form-post/

And this is what I have so far. Would cURL be the best way to do this? and how do I save as 2 leads does marketo have a way of doing this or would I need to write an SQL statement.

<?php
extract($_POST);
$url = 'http://app-x.marketo.com/index.php/leadCapture/save';
$fields = array(
            'FirstName' => urlencode($first_name),
            'LastName' => urlencode($last_name),
            'Email' => urlencode($email),
            'PostalCode' => urlencode($postalcode),
            'Guest_Last_Name' => urlencode($guest_last_name),
            'Guest_Name' => urlencode($guest_first_name),
            'Guest_Email_Address' => urlencode($guest_email),
                );
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
?>
<form id="theform" action="http://app-x.marketo.com/index.php/leadCapture/save" method="POST">
<input type="hidden" name="munchkinId" value="munchkinId#">
<input type="hidden" name="formid" value="forimd#">
First Name:
<input type="text" name="FirstName">
<br>
Last Name:
<input type="text" name="LastName"><br>
Email Address:
<input type="text" name="Email"><br>
Zip Code:
<input type="text" name="PostalCode"><br>
Colleague First Name:
<input type="text" name="Guest_Last_Name"><br>
Colleague Last Name:
<input type="text" name="Guest_Name"><br>
Colleague Email:
<input type="text" name="Guest_Email_Address"><br>
</form>
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

9 replies

March 4, 2015
I'll be interested to see the answer to this as well.
SanfordWhiteman
Level 10
March 4, 2015
@Tim C Don't see where SQL would be involved in any way.

The solution is pretty simple, you just run through the cURL function twice, first time passing it the 'Email' => urlencode($email)', second time 'Email' => urlencode($guest_email), etc. There's no magic, it's two form posts.

P.S. I also want to note that you aren't validating your input.  extract($_POST) offers zero security.  (Yes, a point might be made that you are proxying the data straight back to Marketo and it's up to them to validate it, but it is still very poor practice to put user input into global PHP scope.)
March 4, 2015
Running through the cURL function twice will make two separate leads with different id numbers? I did not think it would be that easy! 

Other Questions: 

How would an admin be able to select this form?
Will it automatically override the form relating to the form ID?
Where would be the best location to host this cURL function and form?
 
SanfordWhiteman
Level 10
March 4, 2015
Why wouldn't it be easy?  You basically have a two forms' worth of data posted at once, so you just split them up before talking to Marketo.  Marketo has no idea it came to you as a single form.

I don't really understand your questions about "selecting this form" -- where?  

Is the initial form (the one that's two forms in one) hosted on your own server?
March 5, 2015
Well in "Design Studio" under "Forms" there is a list of forms. Will the form I made overide the form thats in there with the matching formid?

The initial form was made inside of marketo. Im not sure how the hosting works with that but its not on my own server.
SanfordWhiteman
Level 10
March 5, 2015
"Override" the form in Marketo isn't the right term... I think "Post as if it were the Design Studio form" is more appropriate.

When you do a server-side form post, you're impersonating a browser-side ("client-side") form post.  The Marketo server doesn't care whether the data comes in via a browser, or a server language like PHP, or even a custom app you could build in whatever language you want.  It just cares that the data looks right: that is, (1) it contains a formID that also exists in Marketo and (2) the fields match up.

Some of the things about your setup aren't quite clear to me.  Do you have access to a server on which you are going to place that PHP code? And that PHP-capable server is also the server that receives the form post from your custom form?  Please explain in as much detail as you can.

P.S. I might also note that you don't need a server-side form post to do this.  You could do it all from the browser if you were using an embedded Marketo form (in the onSuccess handler, change the form fields and then send the form a second time).
SanfordWhiteman
Level 10
March 5, 2015
Here's a sample form that posts twice, showing it doesn't need server-side anything: http://jsfiddle.net/sanford/kq0f4v2g/show

In this demo, the form posts once, then switches the First Name and Last Name fields and posts again automatically.  How to manipulate other fields is left as an exercise for the reader, as they say.
Grégoire_Miche2
Level 10
January 30, 2016

Hi Sanford,

Don't we need to clean the mkto token in the second submit ?

-Greg

SanfordWhiteman
Level 10
January 30, 2016

You may want to, yes, but that's an orthogonal issue so I didn't want to make the demo too busy.

You can also submit the secondary email first, then you don't need to worry about it (since the session will be associated with the last submit).

EDIT: I updated the demo to include clearing the Munchkin token value.

March 5, 2015
Does the onSuccess handler work with forms 1.0?
SanfordWhiteman
Level 10
March 5, 2015
@Tim C No idea. I think you should make migrating to Forms 2.0 a priority because there's so much cool stuff you can do.

Provided the user has JavaScript enabled, any HTML form can post to multiple destinations, or post multiple permutations of its values to the same destination. But it is easier if your forms builder contains such useful callbacks.