简体   繁体   中英

insert javascript value into form field

I have a javascript snippet:

<script language="JavaScript" type="text/javascript" src="/partner/display.php?token=id"></script>

It's from the affiliate program we use, iDevaffiliate.

When included in the html of a page, it reads a value (the affiliate id) to the page, so the visitor can see it.

I need to get this same value inserted into to a hidden form field such as:

<input type="hidden" name="affid" id="affid"" value="value goes here">

This will allow us to submit the affiliate id in the form as a hidden value.

Any suggestion on how to include the value outputed by the javascript snippet as a value into the hidden form field?

The script itself doesn't work as a value in the form, atleast when I tried it in different ways. Thanks for any help.

Here is example page with js in the page, and a sample form. The value -100 should be created and visible when viewing the page (from the js): http://www.lifeleap.org/100-1-3-19.html

After Help and More Research, This Is What I Have So Far:

Script prints value here: <div id="affjs"><script language="JavaScript" type="text/javascript" src="/partner/display.php?token=id"></script></div>

<script type="text/javascript">
$('#testform').submit(function () {
var outputValue=document.getElementById("affjs").innerHTML;
document.getElementById("aff").value=outputValue;
});
</script>

<form method="post" action="/scripttest3.php" id="testform">
<input type="hidden" name="aff" id="aff" value="">
<button type="submit" value="Submit">Submit</button>
</form>

It's not finished, but I think I'm on the right track. Maybe onsubmit script needs to be included inside form? Thanks for any suggestions.

You will need to find the value in the DOM and then store in your hidden input field.

var outputValue=document.getElementById("id-of-html-tag-where-output-value-is").innerHTML;
document.getElementById("id-of-hidden-input-field").value=outputValue;

You should do this on form submit.

Inside the Javascript you can use like this

document.getElementById("affid").value ='value goes here' ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM