SOLVED

Re: Insert php variable into marketo form hidden field

Go to solution
Anonymous
Not applicable

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

<?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
Robb_Barrett
Level 10

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

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.

Anonymous
Not applicable

With that code and the hidden fields deleted from the form, it does not work.

SanfordWhiteman
Level 10 - Community Moderator

Need a lot more than "does not work." Post a link to a URL. Also note I have no idea what your actual PHP var names are.

Anonymous
Not applicable

<?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>

SanfordWhiteman
Level 10 - Community Moderator

OK, but I need a URL to see if the code is throwing console errors.

Anonymous
Not applicable

Screen Shot 2016-11-23 at 2.31.04 PM.png

So MktoForms2 is being called before it is loaded?

What to do?

SanfordWhiteman
Level 10 - Community Moderator

forms2.min.js creates the MktoForms2 object.

So any refs to MktoForms2 need to be after that <script> loads.

Anonymous
Not applicable

Thanks. I moved it down below the form into the footer and it works!

SanfordWhiteman
Level 10 - Community Moderator

Great! If you could mark one of my answers (maybe the last one w/code) as Correct...

SanfordWhiteman
Level 10 - Community Moderator

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

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

SanfordWhiteman
Level 10 - Community Moderator

<script>

var marketoVars = {

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

}

</script>

Anonymous
Not applicable

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

SanfordWhiteman
Level 10 - Community Moderator

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

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

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>