duancan1950 2011-04-27 19:53
浏览 33
已采纳

如何使用jQuery和PHP在单独的行上显示JSON响应

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 having trouble getting the results to display on multiple lines. Any suggestions on the best way to do this?

Here is the PHP:

$return['message'] = 'First Entry: ' . $_POST['Name1'] . ' '  . 'Second Entry: ' .    $_POST['Name2'] . ' ' . 'Third Entry: ' . $_POST['Name3'];  

echo json_encode($return);

Here is the jQuery:

$("#mark-form").validate({
    submitHandler: function(form) {
            $(form).ajaxSubmit({                  
                type: "POST",
                data: {
                    "Name1" : $('#Name1').val(),
                    "Name2" : $('#Name2').val(),
                    "Name3" : $('#Name3').val()
                       },
                dataType: 'json',
                url: './includes/ajaxtest3.php',
                error: function() {alert("Error");},
                success: 
                function(data) {
                    $('<div id="output2"></div>').insertAfter($('#agreement-information'));
                    $('#output2').html(data.message).show(500);
                    $('#ouput2').append(data);  
                },       
  });

return false;   
   },
        errorPlacement: function(error,element) {
                        return true;
                },
        rules: {
            "Name1": {
                required: true,
                }
        }       
});
  • 写回答

4条回答 默认 最新

  • doubian0284 2011-04-27 20:00
    关注

    You can transfer JSON encoded HTML newlines like this:

    $return['message'] = 'First Entry: ' . $_POST['Name1'] . '<br /> '  . 'Second Entry: ' .    $_POST['Name2'] . '<br /> ' . 'Third Entry: ' . $_POST['Name3'];  
    echo json_encode($return);
    

    Or convert newlines ( ) into newline tags.

    The nicer way though would be to leave that kind of processing to the client by e.g. returning an array of messages:

    $return['message'] = array();
    $return['message'][] = 'First Entry: ' . $_POST['Name1'];
    $return['message'][] = 'Second Entry: ' . $_POST['Name2'];
    $return['message'][] = 'Third Entry: ' . $_POST['Name3'];
    echo json_encode($return);
    

    And leave the processing to the client (maybe the client wants the messages wrapped in a paragraph tag later on):

    function(data)
    {
        $('<div id="output2"></div>').insertAfter($('#agreement-information'));
        $('#output2').html(data.message.join('<br />')).show(500);
        $('#ouput2').append(data);  
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)