JQuery get response from php file after publishing

I am sending some data to a php file with jquery. The PHP file simply saves the data and prints the success of the error message. I would like to receive the message generated by the php file and paste it into the div. I cannot figure out how I can return a message from a PHP file.

Here is my code:

JQuery

$(document).ready(function(){

    $('#submit').click(function() {

        $.ajax({
            type : 'POST',
            url : 'post.php',           
            data: {
                email : $('#email').val(),
                url   : $('#url').val(),
                name  : $('#name').val()
            },          
        });     
    });
});

PHP:

<?php
//save data
$message = "saved successfully"
?>
+5
source share
2 answers

Try it -

$(document).ready(function(){

    $('#submit').click(function() {

        $.ajax({
            type : 'POST',
            url : 'post.php',           
            data: {
                email : $('#email').val(),
                url   : $('#url').val(),
                name  : $('#name').val()
            },
            success:function (data) {
                $("#yourdiv").append(data);
            }          
        });     
    });
});

This uses a successfunction callback ajaxto return your data to the page in the parameter data. Then the callback function adds your data to the div on the page.

You will also need -

echo $message

On your php page to give jQuery some data to work with.

+8
source

ajax?

jQuery :

$.ajax({
  url: 'ajax/test.html',
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  }
});

, PHP - ( JSON), script.

+1

All Articles