WordPress.org

Ready to get started?Download WordPress

Forums

return data from ajax call (5 posts)

  1. christian_gnoth
    Member
    Posted 3 years ago #

    i am using an ajax call in my plugin to create a new page with wp_insert_post and I would like to show this new post to the front-end user.

    my javascript function is this:

    function show_players_page(id)
    {
      jQuery()
      {
        var data = {
                 action: 'bbnuke_players_page',
                 bbnuke_player_id: id
           };
    
        // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
        jQuery.post( ajaxurl, data,
           function( response )
           {
             alert(response);
           }
        );
      };
    }

    my php function which is called is:

    function  bbnuke_show_players_page()
    {
      global $wpdb;
    
      $player_id = $_POST['bbnuke_player_id'];
    
      $user_id = get_current_user_id();
      if (!$user_id)
        $user_id = 1;
    
      //  create the page content
      $bbnuke_post_content =  bbnuke_widget_playerstats( $player_id, false );
    
      $bbnuke_post = array(
        'menu_order'     => 0,
        'comment_status' => 'closed',
        'ping_status'    => 'closed',
        'post_author'    => $user_id,
        'post_content'   => $bbnuke_post_content,
        'post_name'      => 'baseballnuke-players-page',
        'post_status'    => 'publish',
        'post_title'     => 'baseballNuke - Players Page' . $player_id,
        'post_type'      => 'page'
        );  
    
      $post_id = wp_insert_post( $bbnuke_post );
    
      sleep(3);
    
      $url = get_bloginfo('url') . '/?p=' . $post_id;
    
      bbnuke_update_option( 'bbnuke_ajax_post_url' , $url );
    
      exit;
    }

    in my plugin file is this:

    add_action( 'wp_ajax_bbnuke_players_page', 'bbnuke_show_players_page');
    add_action( 'wp_ajax_nopriv_bbnuke_players_page', 'bbnuke_show_players_page');

    how I can send the create post_id back to the calling javascript function?

    the response only contains "success: true".

    how i can set the responsetext or receive data from the server?

  2. christian_gnoth
    Member
    Posted 3 years ago #

    has anyone an idea how to user the WP_Ajax_Response class? there is no documentation about this class.

  3. christian_gnoth
    Member
    Posted 3 years ago #

    I am getting now always a response code of 0 from the server - no other info - how i can find out what is the problem?

  4. christian_gnoth
    Member
    Posted 3 years ago #

    the ajax call is now working, but still the return of data is not working.

    I have tried different versions of:

    $x = new WP_Ajax_Response( array(
                  'what' => 'post_id',
                  'id'   => $post_id,
                  'data' => $url,
                  'supplemental' => $url
              ) );
    //  $x->send();
    
      die($url);

    but I get an error message back that mysql_real_escape_string expects one parameter and on the end of that the parameter of die is appended.

  5. Zane Matthew
    Member
    Posted 3 years ago #

    put die() at the end of your php script

Topic Closed

This topic has been closed to new replies.

About this Topic