javascript - Jquery array send by ajax to php -
i viewed many post aboit still cant code work.
i want php array of checked checkboxes values.
heres code:
<!doctype html> <html> <head> <meta charset="utf-8"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> <title>untitled document</title> </head> <body> <?php echo'<form method="post"> <input type="checkbox" name="cim" value="valami">'; echo'<input type="checkbox" name="cim" value="valami2">'; echo'<input type="checkbox" name="cim" value="valami3">'; echo'<input type="checkbox" name="cim" value="valami4"> <input type="submit" value="felvisz" name="feladat"></form>'; if (isset($_post['feladat'])) { ?> <script type="text/javascript"> var checkedvalue = $('.messagecheckbox:checked').val(); var myjson = json.stringify(checkedvalue); $.ajax({ type: "post", url: "proba.php", data: { tomb : myjson }, success: function(){ alert("ok"); } }); </script> <?php var_dump($_post); $array = json_decode(stripslashes($_post['tomb'])); foreach($array $arr){ echo $arr; } } ?> </body> </html>
massages got: notice: undefined index: tomb in d:\programok\xamp\htdocs\szakdoga\dropbox\proba.php on line 48
warning: invalid argument supplied foreach() in d:\programok\xamp\htdocs\szakdoga\dropbox\proba.php on line 49
please can me solve this?
to array , must convert name accept multiple change input's :
name="cim"
to
name="cim[]"
also jquery ajax function should :
<script type="text/javascript"> $(function(){ $("form").on("submit",function(e){ e.preventdefault(); var checkedvalue = $(this).serialize(); $.ajax({ type: "post", url: "proba.php", data: checkedvalue, success: function(){ alert("ok"); } });//end ajax });//end form submit }); </script>
in php cim array example
var_dump($_post["cim"]);
hope helps
Comments
Post a Comment