tags:

views:

75

answers:

3

i use json_encode to send back data from php to jquery through ajax.

and i have noticed that jquery just allow us to use ONE echo in php.

if i use echo json_encode($array);.... and then one more echo json_encode($array2); in the same php file, then it would not work.

or if i use echo json_encode($array); and then another echo "hello"; then it stops working too.

am i correct?

the problem is that when i use

$users = mysqli_fetch_assoc($login_user_result);

in the ajax called php file together with

echo json_encode($array);

it doesnt work. it sends the $array correctly but together with a bunch of other code because of the line above it.

but i have to use mysqli_fetch_assoc to get the data from the database.

what is the work around for this?

EDIT: here is the ajax call i used:

        $.ajax({
            url: "static/js/ajaxcall_login.php",
            type: "POST",
            data:
            {
                username:       $("#login_box .username").val(),
                password:       $("#login_box .password").val()
            },
            dataType: "json",
            success: function(data)
            {
                 ................
            }
        )};
+1  A: 

if i use echo json_encode($array);.... and then one more echo json_encode($array2); in the same php file, then it would not work.

You need to merge the tow arrays before json encod.

i have noticed that jquery just allow us to use ONE echo in php.

Not true, if you download and check the jQuery Autocomplete Plugin demo, you will see that is used multiple echo inside loop.

Can you show your jQuery code? Seem like you are not defined the data type correctly in your Ajax Request. Please check it.

Ei Maung
+1  A: 

JSON has a specific syntax that will become corrupted if you output different values.

For example:

$a = array('a', 'b', 'c');
echo json_encode($a);

Would give you:

(['a','b','c'])

...which is interpreted as an array in javascript. If you did

$a = array('a', 'b', 'c');
echo json_encode($a);
echo "Hello";

then this would be printed out:

(['a','b','c'])hello

which would cause parsing issues.

To print out two arrays, create a structure that holds both arrays:

$a = array();
$b = array();
$c = array('first' => $a, 'second' => $b);
echo json_encode($c);
Bill Zeller
Where are the parentheses coming from? A JSON encoding of that array would be just `["a","b","c"]` (also, the double quotes...)
Thanatos
if the phpfile includes a lot of code it is very difficult to not have another echo here and there...for example is i use $users = mysqli_fetch_assoc($login_user_result); in the same file it echoes a lot of code back to jquery. how do i work around this issue?
never_had_a_name
+1  A: 

I didn't see anything wrong with your Ajax Request.

Here is the test code and it worked perfectly...

Request:

$(document).ready(function() {
    $.ajax({
     url: "json.php",
     type: "POST",
     dataType: "json",
     success: function(data) {
      alert(data);
     }
    });

});

Source:

$arr1 = array(3.14, 123, "foo");
$arr2 = array("one", "two", "three");

$arr = array_merge($arr1, $arr2);

echo json_encode($arr);

Only one thing to note...

If you set dataType to json in jQuery Ajax Request, the respnse (echo) must be qualified JSON. That is why you can't echo another string along with JSON.

Ei Maung
the problem is when i use $users = mysqli_fetch_assoc($login_user_result); in the php file it echoes back a bunch of code together with the json string. and that makes it stop working. i used firebug to see the data echoed back. so how can i do to use the line above without interferring with jquery json?
never_had_a_name