How do I have one form save two leads

Anonymous
Not applicable

How do I have one form save two leads

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>
Tags (1)
11 REPLIES 11
Anonymous
Not applicable

Re: How do I have one form save two leads

I'll be interested to see the answer to this as well.
SanfordWhiteman
Level 10 - Community Moderator

Re: How do I have one form save two leads

@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.)
Anonymous
Not applicable

Re: How do I have one form save two leads

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 - Community Moderator

Re: How do I have one form save two leads

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?
Anonymous
Not applicable

Re: How do I have one form save two leads

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 - Community Moderator

Re: How do I have one form save two leads

"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 - Community Moderator

Re: How do I have one form save two leads

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

Re: How do I have one form save two leads

Hi Sanford,

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

-Greg

SanfordWhiteman
Level 10 - Community Moderator

Re: How do I have one form save two leads

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.