duanmo7075 2012-09-24 18:05
浏览 25
已采纳

获取表的所有内容并使用带有jquery的ajax将其附加

I made a simple sample on how to insert using AJAX and retrieving it then append it in a <div> after getting it. But I am having trouble on getting all the content of the table, it's returning a null values.

<div id="wrap-body">
  <form action method="post">
    <input type="text" name="username" id="username">
    <input type="text" name="msg" id="msg">
    <input type="button" id="submit" value="Send">
  </form>
  <div id="info">
  </div>
</div>

jQuery:

<script>
    $(document).ready(function (){
        $('#submit').click(function (){
            var username = $('#username').val();
            var msg = $('#msg').val();

            $.ajax({
                type: 'POST',
                url: 'get.php',
                dataType: 'json',
                data:'username='+username+'&msg='+msg,
                success: function (data){
                    $('#info').append("<p> you are:"+data.username+"</p> <p> your message  is:"+data.mesg);
                }
            });
        });
    });
</script>

PHP:

<?php 
$host='localhost';
$username='root';
$password='12345';
$db = 'feeds';

$connect = mysql_connect($host,$username,$password) or die("cant connect");
mysql_select_db($db) or die("cant select the".$db);

$username = $_POST['username'];
$msg = $_POST['msg'];

$insert = "INSERT INTO info(user_name,message) VALUES('$username','$msg')";

if(@!mysql_query($insert)){
    die('error insertion'.mysql_error());
}

$get = "SELECT * FROM info ";

$result=mysql_query($get)or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
    $return  = $row['user_name'];
    $return = $row['message'];
}
echo json_encode($return);
?>
  • 写回答

2条回答 默认 最新

  • dongzhu6900 2012-09-24 18:10
    关注

    Your while should create array and then do json_encode

    Try below code

    $data=array();
    while ($row = mysql_fetch_array($result))
    {
    $data[] = array(
       'username'=>$row['user_name'],
       'mesg'=>$row['message']
    );
    }
    
    echo json_encode($data);
    exit
    

    Now write your javascript success handler as below

    $.ajax({
                    type: 'POST',
                    url: 'get.php',
                    dataType: 'json',
                    data:'username='+username+'&msg='+msg,
                    success: function (data){
                       $.each(data, function(i, item) {
                         $('#info').append("<p> you are:"+data[i].username+"</p> <p> your message  is:"+data[i].mesg);
                       });​
    
                    }
                });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP