找到你要的答案

Q:foreach $_POST using jquery-php ajax

Q:使用jquery ajax _post foreach美元

<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);

In fact, I have much more textareas, but here are only two of them, just for example.

Something is wrong, because, if some textarea is empty, success function doesn`t work, i.e. nothing happens as response.

<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);

事实上,我有更多的文本区域,但这里是其中的两种,只是举例。

什么是错的,因为,如果某些区域是空的,成功的功能并` T工作,即不发生反应。

answer1: 回答1:

Try to use empty() instead of ==

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

尝试使用empty()代替= =

$msg = "sky";
foreach($_POST as $item){
   if (empty($item)) {$msg = "sea"}
};
echo json_encode($msg);
answer2: 回答2:

Send text area values which have data not empty ones. It can be done just by if else statement

发送有数据而非空的文本区域值。它可以做的只是如果其他声明

answer3: 回答3:

I got a parse error on this line :

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

Add semicolon, i guess

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

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

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

添加分号,我猜

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