dousu8456 2013-09-06 14:06
浏览 90

如何使用jquery post将php获取的数据用于html

Hi i have a function in jquery and using $.Post to send data on a php file where my query is working fine and sending data back

js

function send_agenda_data(cidade_data){
        var data = {'cidade_data':cidade_data};
        $.post('includes/agenda_data.php',data,function(info){

        });
    }

This function works fine and when i alert the data coming back that also works fine

here is php

<?php
    include_once("connection.php");
    $cidade_data = $_POST['cidade_data'];

    if (isset($cidade_data)) {
        $sql = mysql_query("select * from agenda where cidade = '$cidade_data'", $con) or die(mysql_error());
        if (mysql_num_rows($sql) > 0) {
            while ($data = mysql_fetch_object($sql))
            {
                $date = $data->data;
                $cidade = htmlentities($data->cidade); 
                $estado = htmlentities($data->estado); 
                $local = htmlentities($data->local);            
                $endereco = htmlentities($data->endereco); 
                $site_local = htmlentities($data->site_local);
                $site_ingresso = htmlentities($data->site_ingresso); 
                $endereco = htmlentities($data->endereco); 
            }
        }
        else{
           echo "No Data";       
        }
    }

?>

this works fine if i use directly using php and echo the variables in tags now

    $.post('includes/agenda_data.php',data,function(info){
                //need data here            
});

i want to know how i can get the php returned data in js here and how i assign that data into tags. also want to know there is while loop in php is here will be also loop to populate all rows ?

if i use direct php in my webpage then i can use like this in while loop

<div><?php echo $date; ?></div>
<div><?php echo $cidade; ?></div>
<div><?php echo $estado; ?></div>
<div><?php echo $local; ?></div>
.
.
.

how can i get in $.Post case

  • 写回答

3条回答

  • dongyan8896 2013-09-06 14:11
    关注

    Your loop is somewhat incorrect. You're simply fetching each row's data, and then overwriting the previous row's data in all those variables. You should be building an array of results, which you can then send over to the client-side JS code. e.g. something like

    $data
    while($row = mysql_fetch_assoc($result)) {
         $data[] = $row;
    }
    
    echo json_encode($data);
    

    On the client side, you'll receive an array of objects you can iterate over:

    $.post('includes/agenda_data.php',data,function(data){
        $.each(data, function(idx, row) {
            $('#date').innerHTML = row['date'];
            ...
        });
    });
    

    exactly what you do in that JS loop is up to you.

    As well, note that your PHP code is vulnerable to SQL injection attacks.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?