cannot get value from php ajax

how to get value from ajax response in php
how to return json data from php to ajax
ajax json response php
send response from php to ajax
how to get array response in jquery ajax
multiple return values from php with jquery ajax
ajax return json
how to get value from ajax response in javascript

Cannot get value from php true AJAX.

My php code is

<?php
$name =  $_POST['name'];
$hobby = $_POST['hobby'];
if (!empty($name and $hobby)){
echo 'Data was succesfully captured';
}else {
echo 'Data was not captured'
}

my html code is

<div id="result"></div>

<form method="post">
<input name="name" type="text" id="name">
<br>
<input name="hobby" type="text" id="hobby">
<input name="snd_btn" type="button" id="snd_btn" value="Save">
</form>

JS

$(document).ready(function(){
$('#snd_btn').click(function() {
var name = $('#name').val();
var hobby = $('#hobby').val();
$.ajax({
 url: "save.php",
 type: "POST",
dataType: 'json',
data: { name, hobby,
success: function(result) {
$('#result').html(result);
  },
}
 });
 });
   });

if i Change in js to

success: function() {
$('#result').html('Data was succesfully captured');
},

it work but not from php

This one is wrong.

if (!empty($name and $hobby)) {

Please replace that with:

if (!empty($name) and !empty($hobby)) {

You have to check for emptiness of the variable for each one.

Cant get PHP variables using AJAX, You are using json_encode incorrectly. From the documentation: string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] ). The confirm is not called because the ajax request is not success so that it will not call the successCallBack. Please check if you call the internal URL and it really return value.

jQuery AJAX Call to PHP Script with JSON Return · GitHub, <form action="return.php" class="js-ajax-php-json" method="post" <input type="​text" name="favorite_restaurant" value="" placeholder="Favorite restaurant" /> so your browser doesn't actually route the request to the URL specified in the  We've used the submit event of the form element, which will be triggered when a user clicks on the submit button. In that event handler, we've initiated the AJAX call, which submits the form data to the login.php file using the POST method asynchronously. Once we receive a response from the server,

First of all you have check that the ajax call will be successfully done or not

If ajax call was successfully done then,

Check the response of success data by print the in console.

Your php code should be like this

<?php
$name = $_POST['name']; 
$hobby = $_POST['hobby']; 
 if ($name and $hobby ){ 
        echo json_encode('Data was succesfully captured');
     }else {
        echo json_encode('Data was not captured'); 
    }

From php you have to return data in form of json.

And js side:

$(document).ready(function(){ 
    $('#snd_btn').click(function() { 
        var name = $('#name').val(); 
        var hobby = $('#hobby').val();
        $.ajax({ 
             url: "save.php", 
             type: "POST", 
             dataType: 'json', 
             data: { name:name, hobby:hobby}  // data in { key : value}
             success: function(result) { 
             res = JSON.parse(res);
             console.log(res);// display in developertools > console
                 $('#result').html(res); 
             },
 }); }); });

Why i can't get custom fields value or post ID via Ajax?, So, the AJAX request needs the post ID, but it doesn't have that information, so send it! data: { action: 'getmyfunctionform1', post_id: <?php echo  In ajax your data is not in right syntax, Data should be in the pair of key value – Arzeb Mansuri Jun 24 '18 at 18:56 Ok i will check and tell you – Arzeb Mansuri Jun 24 '18 at 19:19 add a comment |

Can't get a JSON object in response to an Ajax request with wp_ajax , log of my "posts" var I get a string 'Array'. No matter how I pass the $list variable in PHP it will always return a string. The query returns posts elsewhere, so it's not  When to use GET? Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page.

How can I get some values created in JavaScript into php, use AJAX , I have read some posts about using Ajax and Json to communicate directly with php to outline an object and I need to get the values created into my php code. is always a , between the pairs and I can not find where that is being inserted. I'm using ajax to get a bunch of messages. The get call works fine, but I don't know how to get the specific key values from the object in the array. I've used console.log to get the actual array which is here:

How to handle AJAX request on the same page - PHP, In this tutorial, I show how you can send and receive AJAX requests on the either change the variable names or pass an extra value with AJAX request but it looks like I can't pass my data with ajax because my PHP code  get value from jquery ajax success. When I use output outside of ajax call I get nothing. 1 solution. How to pass session value in jquery and ajax.

Comments
  • What do you mean by true AJAX?
  • receive data from php with ajax
  • I have answered. Hope this works....
  • What is save.php doing ? What output does save.php throwing back to this page ?
  • Put a semicolon at the end of the line echo 'Data was not captured'. Any change?
  • It's okay, but I do not see a success message from php to result DIV
  • Your ajax data is not correct $.ajax({ url: "save.php", type: "POST", dataType: 'json', data: { name:name, hobby:hobby}
  • In ajax your data is not in right syntax, Data should be in the pair of key value
  • Ok i will check and tell you