0

I am doing next...

request in php from html (jQuery)

$.post("index.php?res=ok", { username: user, userpass: pass }, function(data) {
    $("#signin_ok").html("You sign in now "+data.name+". Your password is: "+data.pass);
    $("#signin_ok").show()
    .animate({fontSize:"120%"}, 100).css({color:"#32CD32"})
    .animate({fontSize:"100%"}, 100)
    .animate({fontSize:"100%"}, 3000)
    .animate({fontSize:"120%"}, 200, function() {
        $(this).css({display:"none"});
    });
});

return from php in html (jQuery)

if(isset($_REQUEST['res']) && $_REQUEST['res'] == "ok") {
    if(isset($_POST['username']) && isset($_POST['userpass'])) {
        $username = $_POST['username'];
        $userpass = $_POST['userpass'];
        echo json_encode(array("name" => $username, "pass" => $userpass));
    }
}

but returns "You sign in now undefined. Your password is: undefined"

What's wrong? How to fix it?

P.S.

When I add "json" like this

$.post("index.php?res=ok", { username: user, userpass: pass }, function(data) {
    . . . . . . 
    . . . . . .
}, "json");

no reaction

What was going on???

3
  • Have you tried to use fiddler or firebug to see what the server is passing back? Commented Feb 17, 2011 at 16:48
  • Do a console.log(data) and see (in Chrome for example) what is the result. If the result is string, you'll have to eval() it Commented Feb 17, 2011 at 16:55
  • Result is {"name":"sabotagnik","pass":"66666666666666"} I'll must eval() it in PHP or in JS? If in PHP: $arr = array("name" => $username, "pass" => $userpass); eval("\$arr = \"$arr\";"); echo json_encode($arr); and result is "Array" Commented Feb 21, 2011 at 12:28

4 Answers 4

2

Try adding the JSON headers to the PHP script with

header('Content-type: application/json');
// then your code

Also, seems your just returning the passed data, why don't you just use user and pass as the data, rather than data.user and data.pass?

Sign up to request clarification or add additional context in comments.

1 Comment

no reaction :(. It is just an example.. that is why I don't use user. I need return data from php only
0

Does this do the trick?

$.post("index.php?res=ok", { username: user, userpass: pass }, function(data) {
    $("#signin_ok").html("You sign in now "+data[0].name+". Your password is: "+data[0].pass);
    $("#signin_ok").show()
    .animate({fontSize:"120%"}, 100).css({color:"#32CD32"})
    .animate({fontSize:"100%"}, 100)
    .animate({fontSize:"100%"}, 3000)
    .animate({fontSize:"120%"}, 200, function() {
        $(this).css({display:"none"});
    });
});

2 Comments

Don't forget to make sure your PHP script sets the JSON headers: header('Cache-Control: no-cache, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Content-type: application/json');
I have tried next... if(isset($_REQUEST['res']) && $_REQUEST['res'] == "ok") { if(isset($_POST['username']) && isset($_POST['userpass'])) { header("Cache-Control: no-cache, must-revalidate"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Content-type: application/json"); $username = $_POST['username']; $userpass = $_POST['userpass']; echo json_encode(array("name" => $username, "pass" => $userpass)); } } but It doesn't work
0

I'll respond with a $.ajax call because that tends to be what I use, since $.post just wraps $.ajax anyways...

$.ajax({

     type: "POST",
     url: "index.php?res=ok",
     data: { "username" : user, "userpass": pass },
     success: function(msg) {

          var data = $.parseJSON(msg);
          alert(data.username);
          alert(data.userpass);

     }

});

Comments

0

I have solved this problem myself, I think so :)

I have created new file (new.php):

<?php
if(isset($_REQUEST['res']) && $_REQUEST['res'] == "ok") {
    if(isset($_POST['username']) && isset($_POST['userpass'])) {
        $username = $_POST['username'];
        $userpass = $_POST['userpass'];
        $arr = array("name" => $username, "pass" => $userpass);
        echo json_encode(array("name" => $username, "pass" => $userpass));
    }
}
?>

and passed request from HTML to PHP (new.php)

. . .
$.post("new.php", { "username": user, "userpass": pass, "res": "ok" }, function(data) {
    $("#signin_ok").html("You sign in now "+data['name']+". Your password is: "+data['pass']);
    console.log(data);
    $("#signin_ok").show()
        .animate({fontSize:"120%"}, 100).css({color:"#32CD32"})
        .animate({fontSize:"100%"}, 100)
        .animate({fontSize:"100%"}, 3000)
        .animate({fontSize:"120%"}, 200, function() {
            $(this).css({display:"none"});
        });
}, "json");
. . .

and I got result :) but it's stupid, why it's doesn't work in index.php???

1 Comment

Oh my God. It was so easy. I have added exit; after json_encode(..); and that is all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.