What is wrong js and php objects [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
In test.php i have this code
<?php
if (isset($_POST['user'])) {
echo "hi";
}
exit; ?>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
var user = {
'name': 'Wayne',
'country': 'Ireland',
'selected': new Array(1, 5, 9)
};
var userStr = JSON.stringify(user);
$.ajax({
url: 'test.php',
type: 'POST',
data: {user: userStr},
success: function(response){
//do whatever.
}
});
</script>
</head>
</html>
I changed it, but isset($_POST['user']) = false because my page is empty so why?
All this code is in test.php

You are not printing / visualizing the actual response of the ajax.
<?php
if (isset($_POST['user'])) {
$user_string = $_POST['user'];
var_dump($user_string);
$user_asarr = json_decode($user_string, true); // associative array
var_dump($user_asarr);
exit();
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
var user = {
'name': 'Wayne',
'country': 'Ireland',
'selected': new Array(1, 5, 9)
};
var userStr = JSON.stringify(user);
$.ajax({
url: 'test.php',
type: 'POST',
data: {user: userStr},
success: function(response){
console.log(response); // check out your console!
}
});
</script>

Related

if else statement is returning always same value [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am new about PHP and ajax
Ajax return data=="OK" or data=="NO" depends on the email in the database
but if-else block returns always same value.It prints always last block like "email is available!" I couldn't find the mistake.
$.ajax({
type: 'post',
url: 'check_email.php',
data: {email:email,},
success: callback});}
function callback(data){
email_error = false;
$("#email_error").css("display","block");
if(data =="OK"){
$("#email_error").html("Email was already used!");
$("#email_error").css("color","#990000");
$("#email_error").show();
email_error = true;
}else{
$("#email_error").html("Email is available!");
$("#email_error").css("color","#F1F0D1");
$("#email_error").show();
email_error = false;
}
}
ajax
var email = "example#example.com";
$.ajax(
{
type: 'post',
url: 'check_email.php',
data:
{
email: email,
},
success: callback
});
function callback(data)
{
email_error = false;
$("#email_error").css("display", "block");
if (data == "OK")
{
$("#email_error").html("Email was already used!");
$("#email_error").css("color", "#990000");
$("#email_error").show();
email_error = true;
}
else if(data =="NO")
{
$("#email_error").html("Email is available!");
$("#email_error").css("color", "#F1F0D1");
$("#email_error").show();
email_error = false;
}
}
html
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="check_email.js"></script>
<title></title>
</head>
<body>
<div id="email_error">
</body>
</html>
php
<?php
echo "NO";
?>
I got a simple solution in link Only I changed "OK" with "\r\nOK".
$.ajax({
type: 'post',
url: 'check_email.php',
data: {email:email,},
success: callback});}
function callback(data){
email_error = false;
$("#email_error").css("display","block");
if(data =="\r\nOK"){
$("#email_error").html("Email was already used!");
$("#email_error").css("color","#990000");
$("#email_error").show();
email_error = true;
}else{
$("#email_error").html("Email is available!");
$("#email_error").css("color","#F1F0D1");
$("#email_error").show();
email_error = false;
}
}

Getting JSON data with jquery ajax not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm trying to understand how to fetch and display data with jquery ajax. I have a php page (data.php)that successfully retrieves data from a mysql database and encodes that data into a json array. Client side I have a page called get.php. I just can't figure out why my script will not fetch any data from data.php I get nothing in the firebug console.
data.php
echo json_encode($mydata);
which outputs:
[
{
"id":"236",
"title":"The Jungle Book"
},
{
"id":"235",
"title":"The Shallows"
},
{
"id":"232",
"title":"For Your Eyes Only"
},
{
"id":"231",
"title":"Ice Giants"
}
]
get.php
<script>
("button").click(function(){
{
$.ajax({
url: 'data.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data[0];
var title = data[1];
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname);
}
});
});
</script>
<h3>Output: </h3>
<button>Get Data</button>
<div id="output"></div>
You have few mistake like: you didn't specify jquery($) for button
selector, you use multiple bracket { inside click function, inside
ajax success you have assigned full object against id and title it
should be id=data[0]['id'] and title=data[0]['title] and another
mistake there no defined variable vname. php better json output you should use header('Content-Type: application/json'); in data.php.
Try this:
index.php
<h3>Output: </h3>
<button>Get Data</button>
<div id="output"></div>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
$("button").click(function(){
$.ajax({
url: 'data.php',
data: "",
dataType: 'json',
success: function(data){
//console.log(data);
var id = data[0].id;
var title = data[1].title;
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+title);
}
});
});
</script>
data.php
<?php
header('Content-Type: application/json'); //use header to specify data type
//echo json_encode($mydata); // un-comment this line
echo '[{"id":"236", "title":"The Jungle Book"}, {"id":"235", "title":"The Shallows"}, {"id":"232", "title":"For Your Eyes Only"}, {"id":"231", "title":"Ice Giants"} ]'; // comment this line
?>
<script>
$("button").click(function()
{
$.ajax({
url: 'data.php',
data: "",
dataType: 'json',
success: function(data)
{
var id = data[0];
var title = data[1];
$('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname);
}
});
});
try like this

Data doesn't transfer to PHP from Ajax [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to pass data that comes from a prompt, but the PHP doesn't receive the data.
<script>
function test(){
var r=prompt("blaksasda");
if(r){
$.ajax({
type: "POST",
url: "file.php",
data: {
param:r,
}
});} };
</script>
PHP file:
<?php
echo "$_POST['param']";
?>
Are you sure PHP doesn't receive the data? Probably Ajax call works correctly. The problem is that you don't handle the echoed result.
Try the following:
$.ajax({
type: "POST",
url: "file.php",
data: {param: r}
}).done(function(result) {
alert(result);
});
Ok, let me add the whole thing.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
function test(){
var r=prompt("blaksasda");
if(r){
$.ajax({
type: "POST",
url: "test.php",
data: { param: r }
}).done(function(result){
alert(result);
}).fail(function(result){
alert('failed: ' + result);
});
}
}
</script>
</head>
<body>
<button onclick="test()">Click me!</button>
</body>
</html>
test.php looks like this
<?php
if (isset($_POST['param'])) {
echo $_POST['param'];
} else {
echo 'Whoops';
}
So when I click on the button 'Click Me', it's going to call the test function and give you a prompt and you type the text in and when you submit, you get the result back from test.php. I tried it and it worked fine.
UPDATE:
Instead of doing an alert in the success handler, just set the result like this:
.done(function(result){
$('#result').html(result);
});
Your page will look something like this. Whatever you get from your page, your span will have that. Hope it makes sense.
<body>
<button onclick="test()">Click me!</button>
<br/>
Prompt result: <span id="result"></span>
</body>

AJAX success function not executing

I'm trying to create a simple AJAX call for testing, but have encountered a problem. I have nested in my AJAX call a success function which should pop an alert message but it doesn't. Checking firebug, the POST is successful and responds with "A20" (without quotations). Is there something wrong in my code?
index.php (view)
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="init.js"></script>
<script src="jquery-1.10.2.min.js"></script>
</head>
<body>
<button id="your_button">Push me</button>
</body>
</html>
init.js
$(function() {
$('#your_button').bind("click", function() {
var json_data = {"category": "A", "size": "20"};
$.ajax({
url: "posted.php",
dataType: "json",
type: "POST",
cache: false,
data: {"data": json_data},
success: function (data) {
if (!data.error) {
alert('k');
} else {
alert('error!');
}
}
});
});
});
posted.php
$category = $_POST['data']['category'];
$tsize = $_POST['data']['size'];
echo ($category);
echo ($size);
Try this -
$(function() {
$('#your_button').bind("click", function() {
var json_data = {"category": "A", "size": "20"};
$.ajax({
url: "posted.php",
dataType: "json",
type: "POST",
cache: false,
data: json_data,
success: function (data) {
if (!data.error) {
alert('k');
} else {
alert('error!');
}
}
});
});
});
Posted.php
$category = $_POST['category'];
$tsize = $_POST['size'];
//echo ($category);
//echo ($tsize);
echo json_encode($_POST);
Your want json data but you were not echoing json data
this is not right:
data: {"data": json_data}
do like this:
data: {data: json_data}
You need to set proper headers in your PHP and send a valid json response from PHP file. Add these lines to your PHP
header('Access-Control-Allow-Origin: *');
header('Content-type: application/json');
and echo back some valid json from it like echo '{"auth":"true","error":"false"}';
First you are using two jquery libraries, remove any one of them.
Second replace data: {"data": json_data}, with data: json_data,.
Third on posted.php use $category = $_POST['category'] and $tsize = $_POST['size'];.
Hope it will help you.

Ajax request for PHP to return HTML [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have an element on a page that when clicked will make an ajax request to a receiver.php file:
<script>
function send(id){
$.ajax({
type: 'POST',
dataType: 'html',
url: 'receiver.php',
data: {id: id}
});
}
</script>
<img src="foo.bar" onclick="send(id)"/> <!-- simplified -->
My idea is that this receiver.php file will receive the id, then output a whole page of HTML based on that id
However, when I click on the element, the new HTML page that I expect doesn't show up. When I go to the Chrome inspector, Network tab I can see the request and the response is exactly the HTML content I need, but why doesn't it change to that new page and stay on the old page instead?
EDIT: This is my receiver.php, for testing purpose:
<html>
<head></head>
<body>
<?php
echo "<p>".$_POST['comid']."</p>";
echo "<p> foo foo </p>";
?>
</body>
</html>
this is the response:
<html>
<head></head>
<body>
<p>3</p><p> foo foo </p> </body>
</html>
Perhaps in your receiver.php script you're doing something like this, where you determine which HTML page to output depending on the id that it is receiving.
switch ($_POST['id']) {
case 'foo':
$filepath = 'bar';
break;
...
}
readfile($filepath);
You would have to reflect that in your AJAX query, using the success function of $.ajax.
function send(id) {
$.ajax({
type: 'POST',
dataType: 'html',
url: 'receiver.php',
data: {id: id},
success: function (data) {
// Replace the whole body with the new HTML page
var newDoc = document.open('text/html', 'replace');
newDoc.write(data);
newDoc.close();
}
});
}
You have to do something with the result that comes back from you AJAX call:
function send(id){
$.ajax({
type: 'POST',
dataType: 'html',
url: 'receiver.php',
data: {id: id},
success: function(data){
console.log(data);
}
});
}

Categories