SOLVED

Insert php variable into marketo form hidden field

Go to solution
Anonymous
Not applicable

Insert php variable into marketo form hidden field

I would like to have a marketo form hidden feild insert some php code.

<input type="hidden" name="Campaign_Name__c" class="mktoField mktoFieldDescriptor mktoFormCol" value="<?php echo $mkto_camp_name ?>" />

We put the <?php echo $mkto_camp_name ?> into the form designer campaign name hidden field, but it is rendering as php text and not rendering the Campaign name we grab from the wordpress php. Probably because marketo is rendering the form after the php is already rendered in the browser.

Is there any way to insert php code into the form field and have it render correctly?

1 ACCEPTED SOLUTION

Accepted Solutions
Anonymous
Not applicable

Re: Insert php variable into marketo form hidden field

<?php

$mkto_camp_name = (get_field_object("marketo_campaign_name")['value']);

$mkto_camp_content = (get_field_object("marketo_campaign_content")['value']);

?>

<script> 

var marketoVars = { 

   campaign:  "<?php echo $mkto_camp_name ?>",

   content:  "<?php echo $mkto_camp_content ?>"

MktoForms2.whenReady(function(form){ 

form.addHiddenFields({ 

   Campaign_Name__c : marketoVars.campaign,

   Campaign_Content__c : marketoVars.content

}); 

});

</script>

View solution in original post

17 REPLIES 17
SanfordWhiteman
Level 10 - Community Moderator

Re: Insert php variable into marketo form hidden field

It's not that Marketo is rendering the form "after" or "before" WP.  Marketo is rendering the form on a totally different server, so there's no way it would have direct access to your PHP variables.

You can however output your variables into the page (in <HEAD> would give easiest access) as JavaScript variables.  Then using the Forms JS API you can put those variables anywhere you want within the form HTML.

Anonymous
Not applicable

Re: Insert php variable into marketo form hidden field

Thanks Sanford. Can you point me to some sample code?

SanfordWhiteman
Level 10 - Community Moderator

Re: Insert php variable into marketo form hidden field

<script>

var marketoVars = {

   camp: "<?php echo $mkto_camp_name ?>"

}

</script>

Anonymous
Not applicable

Re: Insert php variable into marketo form hidden field

In marketo the form hidden field default is required. What do I put in there?

SanfordWhiteman
Level 10 - Community Moderator

Re: Insert php variable into marketo form hidden field

You don't need the field to be on the form at all in form editor.

Just

<script>

MktoForms2.whenReady(function(form){

form.addHiddenFields({

   Campaign_Name__c : marketoVars.camp

});

});

</script>

Anonymous
Not applicable

Re: Insert php variable into marketo form hidden field

Thank you!

Is camp: reserved?

How would I get the

Campaign_Content__c and Campaign_Name__c 

at the same time?

SanfordWhiteman
Level 10 - Community Moderator

Re: Insert php variable into marketo form hidden field

You're just building a JS object and then reading it in the form context.  This can be expanded to any fields you want.

<script>

var marketoVars = {

   campaign:  "<?php echo $mkto_camp_name ?>",

   content:  "<?php echo $mkto_content_name ?>"

}

MktoForms2.whenReady(function(form){ 

  form.addHiddenFields({

   Campaign_Name__c : marketoVars.campaign,

   Content_Name__c : marketoVars.content

  });

});

</script>

Robb_Barrett
Marketo Employee

Re: Insert php variable into marketo form hidden field

Couldn't he also just add it to the DOM using basic JS?

document.getElementByID('Campaign_Name__c').value.("{{my.Campaign ID}}");

Robb Barrett
SanfordWhiteman
Level 10 - Community Moderator

Re: Insert php variable into marketo form hidden field

No, you should never use DOM methods to assign Forms 2.0 values.  (a) it won't work at all if you don't synchronize with the form load; (b) you will lose values by not coercing them properly; (c) the values are supposed to persist on the form object, not the <FORM> tag.

But here I don't know what you're getting at at all: "{{my.Campaign ID}}" is a Marketo token.  You can't access that token from a 3rd-party page -- nor is it clear that the PHP variable that he is outputting into the page has anything to do with the Marketo Campaign.