Hi all. I need some help please with some bits of code. It may be very obvious to you guys but it's not to me.

I need to get a value from a form field in my web page into a part of some javascript code. When the page loads, php will echo a value to the hidden form field below. Example it may echo the value: "8888888888888". How can I get this value to appear in the javascript code in place of "xxxxxxxxxxxxx" ?

I think it is something along the lines of "Document.Form.formname.fieldname.value."

But not sure.

Can anyone help please?

<form action="" method="post" name="form1">
<input name="value1" type="hidden" value="8888888888888">
</form>

<script language="JavaScript" type="text/javascript">
<!--
var google_conversion_id = xxxxxxxxxxxxx;
var google_conversion_language = "en_GB";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "YQ4tCI-jgfy6rytfug";
//-->
</script>

What do I write in in place of xxxxxxxxxxxxx to get the form value to show?

Thank you

Mike

    Give the input element an ID attribute, and use document.getElementById()

      Thank you for your fast reply.

      I just did what you said I gave the element id "gcode". My source code in my page is now:

      <form action="" method="post" name="form1">
      <input name="value1" type="hidden" id="gcode" value="<?php echo $vargcode;?>">
      </form>
      
      <script language="JavaScript" type="text/javascript">
      <!--
      var google_conversion_id = document.getElementById(gcode);
      var google_conversion_language = "en_GB";
      var google_conversion_format = "3";
      var google_conversion_color = "ffffff";
      var google_conversion_label = "YQ4tCI-QjwEQp7Gy9gM";
      

      Now when I view the page source code from browsing the page the value sent in the header "888888" is now in the hidden form field but not in the javascript area.

      As below:

      <form action="" method="post" name="form1">
      <input name="value1" type="hidden" id="gcode" value="888888">
      </form>

      <script language="JavaScript" type="text/javascript">
      <!--
      var google_conversion_id = document.getElementById(gcode);
      var google_conversion_language = "en_GB";
      var google_conversion_format = "3";
      var google_conversion_color = "ffffff";
      var google_conversion_label = "YQ4tCI-QjwEQp7Gy9gM";

        You need quotes around the elements id in the javascript else javascript thinks it is a variable.

        document.getElementById('gcode')
        

        Now when I view the page source code from browsing the page the value sent in the header "888888" is now in the hidden form field but not in the javascript area.

        That sounds like you expecting to see the "888888" in the document.getElementById('gcode') when you view the source, but you will not.

          Write a Reply...