lundi 12 mars 2018

Passing variable based on checkbox to php script upon pressuing submit button

I have the following html code in which I am trying to define a variable days and pass it off to an php based code upon pressing submit. Below is a snippet of my code for the html:

<html>

<script>
var days="fiveday";
function forecast_parm() {
    var x = document.getElementById("myCheck").checked;
    var  output = document.getElementById("myCheck").value;
    if (!x){
        output = "fiveday";
    }
    document.getElementById("days").innerHTML = output;
}
</script>

<body>
    <form action=\"check_proc.php\" method=\"get\" target=\"_blank\">
        Checkbox: <input type="checkbox" id="myCheck" value="tenday" onchange="forecast_parm()">
        <p>Click the "Try it" button to display the value of the value attribute of the checkbox.</p>
        <p id="days"></p>
        <center><input type=\"submit\" value=\"Create Process\"></center>
    </form>
</body>
</html>

Upon clicking on the submit button I would like to have the variable days be passed off to the php script check_proc.php. Here is a snippet of that php script:

<?php
    $days = $_GET['days'];
    echo "<br>Day is $days</br>";
<?

There are other processes that will use the variable days. Do you know how to tweak either the initial html code or the php script check_proc.php to get the desired results? That is that upon submit I should have displayed on the web browser "Day is tenday" if the box is checked and "Day is fiveday" if the box is not checked in the initial html page.




Aucun commentaire:

Enregistrer un commentaire