qadenza
qadenza

Reputation: 9293

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

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.

Upvotes: 0

Views: 512

Answers (3)

Anonymous Duck
Anonymous Duck

Reputation: 2998

I got a parse error on this line :

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

Add semicolon, i guess

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

Upvotes: 1

Sagar
Sagar

Reputation: 647

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

Upvotes: 2

hamed
hamed

Reputation: 8043

Try to use empty() instead of ==

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

Upvotes: 3

Related Questions