foreach $_POST using jquery-php ajax


<textarea id="ap1" rows="1"></textarea>
<textarea id="ap2" rows="1"></textarea>

JS

var ap1 = $('#ap1').val();
var ap2 = $('#ap2').val();
$.ajax({
    type: "POST",
    dataType: "json",
    url: "ajax.php",
    data: {ap1:ap1, ap2:ap2},
    success: function (data) {
    $("#info").html(data).slideDown("slow");
    }
});

ajax.php

$msg = "sky";
foreach($_POST as $item){
    if ($item == "") {$msg = "sea"}
};
echo json_encode($msg);

实际上,我还有更多的textareas,但这里只有两个,例如。

有些问题,因为,如果某些文本area为空,则 success函数无效,即没有任何响应发生。

尝试使用empty()代替==

$msg = "sky";
foreach($_POST as $item){
   if (empty($item)) {$msg = "sea"}
};
echo json_encode($msg);

发送具有数据而不是空数据的文本区域值。只需使用其他语句

就可以完成

我在这一行上有解析错误:

if ($item == "") {$msg = "sea"}

添加分号,我猜

if ($item == "") {$msg = "sea";}

最新更新