javascript值传递给php

时间:2016-05-04 12:18:29

标签: javascript php jquery

我在复选框中使用添加方法,使用来自数据库的php动态值复选框值。如果我点击复选框,则值正在添加值。 我正在确认按钮中获取警报值。如何在PHP中传递该值。请建议我

的index.php

<input class="checkbox" type="checkbox" name="select" value="<?php echo $row['values'];?>">

<input type="submit" class="button" onclick="insert()" name="check" value="Confirm ">

的javascript

<script>
     function insert(){
      var el, i = 0;
      var total = 0;
      while(el = document.getElementsByName("select")[i++]) {
      if(el.checked) { total= total + Number(el.value);}
       }
     //alert(total);
     }
 </script>

1 个答案:

答案 0 :(得分:0)

如果您使用JQuery,则可以在不刷新浏览器的情况下执行此操作,例如:

的Javascript

//execute in method

$.post("php file name.php",
    {
        name: "Foo",
        lastname: "Bar"
    },
    function(data, status){
        //after php has been this function is executed, data contains the data you send back from your php.
        alert(data);
    });

PHP

<?php
    //check if variables are send
    if(isset($_POST["name"]) && isset($_POST["lastname"])) {
        $name = $_POST["name"];
        $lastname = $_POST["lastname"];
        //do something with variabels
        echo "give back a result";
    }
?>