Reputation: 361
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???
Upvotes: 0
Views: 243
Reputation: 361
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???
Upvotes: 0
Reputation: 3351
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);
}
});
Upvotes: 0
Reputation: 41433
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?
Upvotes: 2
Reputation: 6679
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"});
});
});
Upvotes: 0