0

I am [successfully] storing a snippet of jQuery inside a php variable, with values inside the snippet being populated by php script like so:

...//collect necessary variables

$script = "
  <script type='text/javascript'>
    (function($) {
      analytics.identity('" . $cid . "', {
        created: '" . $created . "',
        email: '" . $email . "',
        ...: '" . $whatever . "'
      });
    })(jQuery);
  </script>
";

return $script;

I can also [successfully] get the name attribute of all forms on the page like so:

 <script type='text/javascript'>
    (function($) {
      $('form').each(function() {
        var formname = $( this ).attr('name');
        if(formname !== undefined) {
          console.log(index + ':' + encodeURIComponent(formname));
        };
      });
    })(Jquery);
</script>

The problem I'm having (maybe obviously) is the lack of experience with javascript to know how to incorporate the two so my $script would look like so:

$script = "
  <script type='text/javascript'>
    (function($) {
      analytics.identity('" . $cid . "', {
        created: '" . $created . "',
        email: '" . $email . "',
        ...: '" . $whatever . "'
      });
      analytics.trackForm($('form[name="formname1"]'),'Form Submitted', {
        lead: formname
      });
      analytics.trackForm($('form[name="formname2"]'),'Form Submitted', {
        lead: formname
      });
      ...//(n) number of form names
    })(jQuery);
  </script>
";

Latest script added directly to the footer:

<script type="text/javascript">
  (function($) {
   $('form').each(function() {
    var formname = $(this).attr('name');
    if( formname !== undefined) {
      console.log( formname );
      var forms = $('form[name="' + formname + '"]');
      var trackforms = analytics.trackForm(forms, 'Submitted Optin Form', { leadmagnet: "'" + formname + '"' });
      return trackforms;
    }
   });
  })(jQuery);
</script>

Console.log outputs the one form currently on the page, and if I add another, it outputs that correctly also, but the rest of the code is simply written as is and I'm not getting it.

Thanks again.

1 Answer 1

1

document.write(...) is adding the string to the document not to the script.

You need to return the functions you want.

$script = "
  <script type='text/javascript'>
    (function($) {
      analytics.identify('" . $ifs_id . "', {
        created: '" . $created . "',
        email: '" . $email . "',
        firstName: '" . $first_name . "',
        leadsource: '" . $lead_source ."'
      });
      $('form').each(function( index ) {
        var formname = $( this ).attr('name');
        if( formname !== undefined) {
          //console.log( index + ':' + formname );
          var forms = $('form[name=\"+formname+\"]);
          var trackform = analytics.trackForm(forms, 'Submitted Opt In Form', {
             leadmagnet : $( this ).attr('name')
           });
          return trackform;

        }
      });
    })(jQuery);
   </script>
";

return $script;
Sign up to request clarification or add additional context in comments.

6 Comments

Does the trackform need to be enclosed in quotes so it returns it as a string, because that's not working...
No. When the loop runs it goes through each form ... and then returns the function analytics.trackForm(...).
I am missing something...it's adding it to the string and not outputting the variables
Still not working... I added the snippet not working directly on the page and it's simply outputting the script, not the expected results of the loop - help!
It's adding this text to the php string. But when the browser loads the page with this javascript it runs the loop which runs the analytics.trackForm(...) function. You won't see the output of the form names etc. in the loop but you don't need the see it. It's running the loop and the function.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.