Insert php variable into marketo form hidden field | Community
Skip to main content
November 17, 2016
Solved

Insert php variable into marketo form hidden field

  • November 17, 2016
  • 2 replies
  • 11523 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by

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.


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

2 replies

Robb_Barrett
New Participant
November 23, 2016

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
New Participant
November 23, 2016

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.

November 23, 2016

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

SanfordWhiteman
New Participant
November 17, 2016

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.

November 18, 2016

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

SanfordWhiteman
New Participant
November 18, 2016

<script>

var marketoVars = {

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

}

</script>