无法使用变量

时间:2017-05-04 19:10:59

标签: javascript jquery ajax

我试图在用户点击"上一个按钮"时检索以前的数据。但是,我无法使用变量data检索数组prev中的值。换句话说,数据[上一页] 不起作用。任何人都可以建议怎么做?

使用Javascript:

$("#canvas-item .previous-button").click(function(e){
  //Get id of which previous button(s) when clicked on. Ex: #PS, #VP,etc
  var prev = $(this).attr('id');  
  previous_content(prev);
});

function previous_content(prev){
  var project_id = $(" #info #project_id").text();
    return $.ajax({
      type: "POST",
      url: ajaxobject.ajaxurl,
      data: {
        action:'get_previous',
        prev:prev,
        project_id:project_id,
      },
      success: function(data) {
        var data = JSON.parse(data);
        console.log(prev+"= button id");
        console.log(data[prev]);
        $("#canvas-item #" + prev + " #content-wrap #item-content").html(data[prev]);
      },
      error: function(xhr, ajaxOptions, thrownError) {
        console.log(xhr.responseText);
      }
    })
}

数据数组如下所示:

data['PS'] = 'mppp Lmmmm';
data['VP'] = 'Value';
data['C'] = 'Customers';
data['TT'] = 'Time';

PHP:

function get_previous()
{
  global $mydb;

  $project_id = $_POST['project_id'];
  $canvas = $mydb->get_results('SELECT * FROM canvases WHERE project_id=' . $_POST['project_id'] . ' ORDER BY date_submitted DESC limit 2');

    switch($_POST['prev']){

        case 'PS':
        $response['PS'] = $canvas[1]-> problem_statement;
        echo json_encode($response);
        break;

        case "VP":
        $response['VP'] = $canvas[1]-> value_proposition;
        echo json_encode($response);
        break;

        case "C":
        $response['C'] = $canvas[1]->customer; 
        echo json_encode($response);
        break;

        case "TT":
        $response['TT'] = $canvas[1]->target;
        echo json_encode($response);
        break;

        default:
        break;
    }

    wp_die();

}
add_action("wp_ajax_get_previous", "get_previous");
add_action("wp_ajax_nopriv_get_previous", "get_previous");

0 个答案:

没有答案