Arnausd Arnausd - 1 month ago 4
Ajax Question

AJAX returning data from a PHP page with code inside

I got the following ajax where I want to send data from a < form > to a PHP page, where the data will be stored in a DataBase and then I will return a password generated in the PHP to the AJAX, but seems that I can't succeed

$.ajax({
type:'POST',
url:'register.php',
data:$('#form_register').serialize(),
});


I'm new to AJAX and every answer I saw was just a PHP page with a few lines of code, it's impossible to use the JSON response in a PHP page with more code?

Answer

If you want to return some data from your register.php, you have to output it in this file:

<?php
// register.php

/* Do whatever has to be done here */

echo json_encode(<your data-array here>); // Encode the data you want to return here

In your script where the ajax request is made you have to handle the response from register.php

$.ajax({
   type:'POST',
   url:'register.php',
   data:$('#form_register').serialize()
}).done(function(returnedData) {
   // Process the returnedData
});
Comments