-2

I am using addtion method in checkbox using php dynamically value coming from database in checkbox value. if i am clicking checkbox the value is adding the values. i was getting the alert values in confirm button. how to pass that value in php. please suggest me

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>
MGS
  • 113
  • 1
  • 15
  • 3
    Use AJAX. It sends data from JavaScript to PHP. – Pupil May 04 '16 at 12:19
  • And internet is full of manuals. – u_mulder May 04 '16 at 12:21
  • Possible duplicate of [jQuery Ajax POST example with PHP](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – Epodax May 04 '16 at 12:29
  • @Epodax this posts asks "how it should be done", this is different from "how to do it with Ajax" – Adrien Brunelat May 04 '16 at 12:42
  • @AdrienBrunelat I'm not sure I understand, SO is not a step-by-step / guide / tutorial service, so It's off-topic instead? – Epodax May 04 '16 at 12:50
  • @Epodax I think it belongs to the uninteresting family of questions and should only be downvoted. There is no need for moderator intervention, it's an understandable question that can be answered and that is not a duplicate of another question. – Adrien Brunelat May 04 '16 at 12:54
  • @AdrienBrunelat A question can be closed as off-topic without moderator attention (and most often is), this site is after all community driven. – Epodax May 04 '16 at 12:55
  • @Epodax ok. Still, this question is "a practical, answerable problem that is unique to software development" so it's in the scoped defined in: http://stackoverflow.com/help/on-topic and is not off-topic – Adrien Brunelat May 04 '16 at 13:01

1 Answers1

0

If you use JQuery you can do it without your browser refreshing, an example:

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";
    }
?>
namlik
  • 182
  • 1
  • 12