简体   繁体   English

jQuery数组通过Ajax发送到php

[英]Jquery array send by ajax to php

I viewed so many post aboit this but still cant get my code to work. 我查看了很多关于此的帖子,但仍然无法使我的代码正常工作。

I want to get a php array of my checked checkboxes values. 我想获取我选中的复选框值的php数组。

heres my 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 as $arr){
    echo $arr;
}

}
?>

</body>
</html>

Massages i got: Notice: Undefined index: tomb in D:\\programok\\xamp\\htdocs\\SZAKDOGA\\Dropbox\\proba.php on line 48 我得到的按摩:注意:未定义索引:第48行的D:\\ programok \\ xamp \\ htdocs \\ SZAKDOGA \\ Dropbox \\ proba.php中的坟墓

Warning: Invalid argument supplied for foreach() in D:\\programok\\xamp\\htdocs\\SZAKDOGA\\Dropbox\\proba.php on line 49 警告:第49行的D:\\ programok \\ xamp \\ htdocs \\ SZAKDOGA \\ Dropbox \\ proba.php中为foreach()提供的参数无效

Please someone can help me to solve this? 请有人可以帮我解决这个问题吗?

To get an array , you must convert the name to accept multiple so change the input's : 要获取数组,必须将名称转换为接受多个,因此更改输入的:

name="cim"

to

name="cim[]"

Also your jquery ajax function should be this : 您的jquery ajax函数也应该是这样的:

<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 the cim will be the array example 在php中,cim将是数组示例

var_dump($_POST["cim"]);

hope it helps 希望能帮助到你

currently, you are testing for checked boxes with class messageChecked . 目前,您正在使用messageChecked类测试复选框。 assign your checkboxes that class, give your form an id , then test for checked condition on each checkbox, 为该类分配复选框,为您的表单指定id ,然后在每个复选框上测试检查条件,

 $('#yourForm').each(function() {
    if($('.messageChecked').is(':checked')) {
        checkedValue.push($('.messageChecked:checked').val());
    }
 }

now send it to your php script via ajax, 现在通过ajax将其发送到您的php脚本,

 $.ajax({        
   type: "POST",
   url: "proba.php",
   data: { tomb : (checkedValue) },
   success: function(){
      alert("OK");
   }
 }); 

if done like this you can remove json_decode and stripslashes from your $_POST statement 如果做这样你可以删除json_decodestripslashes$_POST声明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM