Domanda

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.

È stato utile?

Soluzione

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top