How to display a JSON response on separate lines using jQuery and PHP

后端 未结 4 915
离开以前
离开以前 2021-01-16 07:54

I am using AJAX to submit a form, and would like to display a summary of the information on the page so that the user can confirm the information before submitting. I am hav

4条回答
  •  情话喂你
    2021-01-16 08:22

    Generally you want to use json_encode on arrays. So your PHP array would look like:

    $arr = array('foo' => 1, 'bar' => 2, 'baz' => 'test');
    // you can probably just use $_POST here, i.e. $arr = $_POST
    
    echo json_encode($arr);
    

    And your JavaScript dealing with that data variable would look like this:

    $('#id').html(data.foo + "
    " + data.bar + "
    " + data.baz);

    This lets you deal with your data piecewise, rather than in one lump, so it's easier to manipulate and parse.

提交回复
热议问题