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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?