简体   繁体   中英

Pass a javascript variable to another page when form is submitted. with just html and Javascript

Good day, I am trying to pass a java script variable along with 2 user inputs. I am unable to get the code to work the email and name go to HighScore.php just fine, but I keep getting zero for the hidden field. highscore1 is the name of the variable. I don't know J Query, so I need a HTML java-script solution. Thank you for looking.

 <form method="post" name="form" action="HighScore.php">
            <input type="text" placeholder="Enter Name" name="username">
            <input type="text" placeholder="Enter Email" name="email">
            <input type="hidden" name="highscore1" id="hidden_score" value= highscore1>
            <input type="submit" name="add" value=Enter>

        </form>

I think in your case, you should first use javascript to get/set hidden_score input, you can use it anywhere in your html page, as long as you ensure that input is fully rendered, you can simply add it inside of $(function() { /*you can put your code here*/ }) for jQuery version, and document.addEventListener('DOMContentLoaded', function(event) { /*you can put your code here*/ }) for javascript version

<script>
document.getElementById("hidden_score").value = "Set value here";
</script>

And then, in your HighScore.php, you can use this for getting that value depend on form method: $_POST['hidden_score'] for post and $_GET['hidden_score'] for get

function setHighScore()
{
    document.getElementById("hidden_score").value = "Set value here";

}

now you can call the above function on any event that meets your requirement

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