从javascript数组中获取值并将其放入php变量中

从javascript数组中获取值并将其放入php变量中

问题描述:

I use daterangepicker() to get two dates ( begginning and ending ), I'd like to put those dates in php variables.. i've succeeded to do an alert() to show those variables using a button.. but i don't know how to put them in the php. here's my code if anyone have an idea..


<script type="text/javascript">

$(function(){
    $('#rangeBa, #rangeBb').daterangepicker();
 });
function test(){
    var dates = new Array();
    dates[0] = document.inputdate.rangeBa.value;
    dates[1] = document.inputdate.rangeBb.value;
    return dates; 
}

</script>

<body>
    <form name="inputdate" method="post">
        <input type="text" value="m/jj/aaaa" id="rangeBa" name="rangeBa"/>
        <input type="text" value="m/jj/aaaa" id="rangeBb" name="rangeBb"/>
    </form>
    <button onclick="alert(test())">Click on this button</button>

</body>

You have to add a submit input element in your form and add an action parameter in your form:

<form name="inputdate" method="post" action="yourfile.php">
    <input type="text" value="m/jj/aaaa" id="rangeBa" name="rangeBa"/>
    <input type="text" value="m/jj/aaaa" id="rangeBb" name="rangeBb"/>
    <input type="submit" value="Click to send" />
</form>

And in yourfile.php: you get the variables by $_POST['rangeBa'] and $_POST['rangeBb']

Feel free then to use ajax method if you don't want a refresh of the page.

After you submit your form you can find the form variables by name in $_POST. for example $_POST['rangeBa']