Passing variables from PHP to AJAX success function

My goal is to update a WordPress post using AJAX. My code is:

Script:

$.ajax({
    type: 'POST',
    url: ajax_url,
    data: {
        'action': 'wp_post',
        'ID': post_id,
        'post_title': post_title
},
success: function( data ) {
        $( '.message' )
        .addClass( 'success' )
        .html( data );
},
error: function() {
        $( '.message' )
        .addClass( 'error' )
        .html( data );
    }       
});

PHP:

function wp_post() {

    $post['ID'] = $_POST['ID'];
    $post['post_title'] = $_POST['post_title'];
    $post['post_status'] = 'publish';

    $id = wp_update_post( $post, true );

    if ( $id == 0 ) {
        $error = 'true';
        $response = 'This failed';
        echo $response;
    } else {
        $error = 'false';
        $response = 'This was successful';
        echo $response;
    }

}

As you can see, the variable $responsein my PHP function is passed to the success function in my script, and the value is displayed on the page $response.

I want to change my success function to do something like this:

success: function( data ) {
    if( $error == 'true' ) {
        // do something
    } else {
        // do something else
    }
},

The problem is that I find it hard to pass variables $responseand $errorin my PHP-function features of success in my session.

  • Can anyone tell me how to pass $response and $error in my success function script?
  • Is there a better approach I should take?

I'm new to AJAX, so forgive me if the question is very simple.

+4
6

php script json, :

function wp_post() {

    $post['ID'] = $_POST['ID'];
    $post['post_title'] = $_POST['post_title'];
    $post['post_status'] = 'publish';

    $id = wp_update_post( $post, true );

    $response = array();

    if ( $id == 0 ) {
        $response['status'] = 'error';
        $response['message'] = 'This failed';
    } else {
        $response['status'] = 'success';
        $response['message'] = 'This was successful';
    }

    echo json_encode($response);

}

javascript-:

success: function( data ) {
    if( data.status == 'error' ) {
        // error handling, show data.message or what you want.
    } else {
        // same as above but with success
    }
},
+10

JSON, , :

$arr = array('error' => true, 'something' => 'foo');
echo json_encode($arr);

json , :

success: function( data ) {
    var error = '';
    var something = '';
    for(var i = 0; i < data.length ; i++)
    {
        error = data[i].error;
        something = data[i].something;
    }
    if( error ) {
        // do something
    } else {
        // do something else
    }
},

, PHP JavaScript.

, , .

+2

, , json_encode()

PHP JSON.

:

$dataArray = array( 'message' => 'Error', 'data' => data);
echo json_encode($dataArray);   
+1

PHP JavaScript. , , PHP JavaScript-.

, $response "error" "no_error" - AJAX , var data "error" ().

0

:

echo json_encode($response);

.php, :

var data = $.parseJSON(data);

ajax.

:

function php_ajax_function() {

    // whatever you want to do...

    $response = array();

    if ( $id == 0 ) {
        $response['status'] = 'error';
        $response['message'] = 'This failed';
    } else {
        $response['status'] = 'success';
        $response['message'] = 'This was successful';
    }

    echo json_encode($response);
}

javascript-:

success: function( data ) {

    console.log(data);

    var data = $.parseJSON(data);

    if( data.status == 'error' ) {
        // do something
    } else {
        // do other thing
    }
}
0

javascript echoed $. , , - . , html ajax. : http://api.jquery.com/jquery.ajax/

success: function( data ) {
    if( data == 'This failed' ) {
        // do something
    } else {
        // do something else
    }
},
-2

All Articles