如何使用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个回答



你的循环有点不正确。 您只需获取每一行的数据,然后覆盖所有这些变量中的前一行数据。 您应该构建一个结果数组,然后您可以将其发送到客户端JS代码。 例如 类似</ p>

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

echo json_encode( $ data);
</ code> </ pre>

在客户端,您将收到一系列可以迭代的对象:</ p>

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

你在JS循环中所做的完全取决于你 。</ p>

同样,请注意您的PHP代码容易受到 SQL注入攻击</ a >。</ p>
</ div>

展开原文

原文

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.



$ .post是一个AJAX调用,因此你需要在PHP中回显结果,以确保你可以使用结果。 / p>

尝试使用</ p>

  echo json_encode($ your_data)
</ code> </ pre>

要使用客户端(浏览器)中的数据,您可以使用
http://api.jquery。 com / jQuery.parseJSON / </ p>

使用了parseJSON,因此您可以更轻松地使用数据。</ p>
</ div>

展开原文

原文

$.post is an AJAX call, thus you need to echo the result in PHP, to make sure you can use the results.

Try using

echo json_encode($your_data)

And to use the data in client side (browser), you can use http://api.jquery.com/jQuery.parseJSON/

That parseJSON is used so you can use the data easier.

Why not use jQuery to fill the contents of the div by assigning a class as in this example:

<div class="date"><?php echo $date; ?></ div>
<div class="cidade"><?php echo $cidade; ?></ div>
<div class="estado"><?php echo $estado; ?></ div>
<div class="local"><?php echo $local; ?></ div>

and with jQuery, the success of $.post retrieve the information returned and integrate like this.

$('.date').html(info[0]['date']);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐