How to pass jquery values to php without page loading -
i want pass jquery value "selected" fetchdata.php without reloading page. how can this?
here code:
<!doctype html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script> <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.min.js" type="text/javascript"> </script> <script> $(document).ready(function() { $("#buttonclass").click(function() { getvalueusingclass(); }); }); function getvalueusingclass() { var chkarray = []; $(".chk:checked").each(function() { chkarray.push($(this).val()); }); /* join array separated comma */ var selected; selected = chkarray.join('#') + "#"; if (selected.length > 1) { $.ajax({ url: "fetchdata.php", //this page handle sql insert type: "get", data: "val=" + selected, //the data sending some-page.php success: function() { console.log("ajax request successfull"); }, error: function() { console.log("ajax request failure"); } }); //alert("you have selected " + selected); } else { alert("please @ least 1 of checkbox"); } } </script> </head> <body> <div id="checkboxlist"> <div><input type="checkbox" value="1" class="chk"> value 1</div> <div><input type="checkbox" value="2" class="chk"> value 2</div> <div><input type="checkbox" value="3" class="chk"> value 3</div> <div><input type="checkbox" value="4" class="chk"> value 4</div> <div><input type="checkbox" value="5" class="chk"> value 5</div> <div> <input type="button" value="get value using class" id="buttonclass"> </div> </html>
fetchdata.php
<?php foreach($_get['val'] $r) { print_r($r); } ?>
i using method receive data , for-each loop print array, not getting values in php file.
change ajax function below , make sure fectdata.php in same folder or give correct path.
$.ajax({ url: 'fetchdata.php', type:'get', data: {val:selected}, success: function(data) { console.log("ajax request successfull"); } });
Comments
Post a Comment