Error while passing PHP data through AJAX?

ajax post data to php
php ajax jquery
simple ajax example in php
post data using ajax in php
how to call php function from javascript using ajax
jquery ajax
how to send value from one page to another in php using ajax
jquery ajax data

I'm new to PHP and jQuery, so I have doubt when declaring PHP data in JSON format with AJAX.

I don't know how to receive the JSON data again and place it inside a PHP variable.

Here's my current attempt:

<?php
 $id = $_GET['id'];
  ?>


function load_topic()
 {
  $.ajax({
   url:"fetch_topic.php",
   method:"POST",
   data:{
    tpid :"<?php print $id; ?>"
   },
   dataType:"JSON",
   success:function(data)
   {
    $('#display_topic').html(data);
   }
  })
 }

function load_comment()
 {
  $.ajax({
   url:"fetch_comment.php",
   method:"POST",
  data:{
    "tpid" :"<?php print $id; ?>"
   },
   dataType:"JSON",
   success:function(data)
   {
    $('#display_comment').html(data);
   }
  })
 }

I assume you want to retrieve the tpid which you pass with ajax in your files fetch_comment.php and fetch_topic.php.

To do this, you simply use

$tpid = $_POST['tpid'];

This retrieves the 'tpid' which was send via the ajax post request and stores it in the variable $tpid.

Source: http://php.net/manual/en/reserved.variables.post.php

jQuery.post(), A plain object or string that is sent to the server with the request. See jQuery.​ajax( settings ) for a complete list of all settings. The success callback function is passed the returned data, which will be an XML root element or a The jqXHR​.done() (for success), jqXHR.fail() (for error), and jqXHR.always() (for completion,​  In today’s tutorial, I’m going to show you, how to Pass Data with Ajax to a PHP file and process it. Using this method, you can retrieve data from a database, and run other PHP scripts using the values of the forms and fields. For the sake of this tutorial, we’re going to create a small calculator,that add two numbers together.

check by inspecting in browser under network that it is sending data or not? if it is sending the data using method post then in php file where you are receiving data use print_r($_POST);

if it print something in and it is showing in json try print_r(json_decode($_POST));

How to pass value to another php using ajax..? - PHP, Based on user inputs in six.php and clicks submit this will call the i came to know that the values were being passed to six-cuf.php , but it was not my ajax code enclosed in javascript function and spot the error ive done. Passing data to php script using Ajax – here we will use j query to get data from inputs and then create an Ajax method to pass data to php script (asynchronous request). Ajax is sued to asynchronously send data to a script. By asynchronous we mean that the user can continue doing some other tasks at the front end till the script is being executed.

There's a few issues with your post, here:

1/ It's not clear what JSON data you are returning from fetch_topic.php and fetch_comment.php. Your code is fine, but without knowing what JSON data you are returning from those pages, it's difficult to help.

2/ The function that handles the response is a bit odd in that it will print the raw JSON data to those elements. If you want to process the JSON a bit better, i suggest something like this:

$.ajax({
    url:"fetch_comment.php",
    method:"POST",
    data:{
        "tpid" :"<?php print $id; ?>"
    },
    dataType:"JSON",
    success:function( response )
    {
        if ( response.status == 'success' ) {
            $('#display_comment').html( response.html );
        } else {
            // handle your error here.
        }
    }
});

and your fetch page responds with:

{"status":"success","html":"<p>Here is an HTML response!<\/p>"}

or not use JSON and just return html from your fetch pages:

$.ajax({
    url:"fetch_comment.php",
    method:"POST",
    data:{
        "tpid" :"<?php print $id; ?>"
    },
    dataType:"HTML",
    success:function( response )
    {
        $('#display_comment').html( html );
    }
});

A little more clarity in your request would be super helpful.

PHP script not picking up variables through Ajax - PHP, I have a sign_up.php script pointing to POST variables like so: In my error log, it is supposed to show undefined indexes but it just stays stagnant. where it says I should json encode my form elements before sending. Can anyone tell me why this bit of code isn't working? When I push submit nothing happens. In the receiving php file I'm using $_POST ['time'] and $_POST ['date'] to put the data in a mysql query but it's just not getting the data. I'm assuming it's something to do with the submit button but I can't figure it out.

PHP - AJAX and PHP, In the example above, when a user types a character in the input field, executed when the server response is ready; Send the request off to a PHP file Output "no suggestion" if no hint was found or output correct values Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant  Handling Ajax errors with jQuery. This is a tutorial on how to handle errors when making Ajax requests via the jQuery library . A lot of developers seem to assume that their Ajax requests will always succeed.

AJAX Send an XMLHttpRequest To a Server, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  I usually just put related items in an object (or leave them in their original object), convert the whole object to a JSON string, and send the JSON string in a parm.

jQuery AJAX Call to PHP Script with JSON Return · GitHub, jQuery AJAX Call to PHP Script with JSON Return success: function(data) { "​Favorite beverage: " + data["favorite_beverage"] + "<br />Favorite restaurant: Thank you for wasting an hour of my time trying to get a tutorial with a bug to work​. I'm trying to send an ajax POST to a php file, however the php file sends a notice of "undefined index", and the php file never seems to receive the value i'm trying to send it. I've been searching for the answer to why this isn't working correctly, so hopefully someone can give me some insight.

Comments
  • Can you tell us what the error/issue is that you are seeing?
  • are you using any framework?
  • create a separate file and pass all the data to file,
  • I don't use any frame work and it does not show any error
  • My doubt is whether I declare the json data in the correct manner or not
  • Can I able to declare simply like this in data attribute unlike json
  • yes it print the variable content,but before i include the data option int the ajax field it works properly in the sense it return the fetched data from the database.but i want to fetch some data with the condition with the help of data i want to pass over the ajax.
  • without these two line my code work well ` data:{ "tpid" :"<?php print $id; ?>" }, dataType:"HTML", `