用ajax替换div内容 [英] Replace the div content with ajax

查看:114
本文介绍了用ajax替换div内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < html>< p> ; 
< head>
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js>< / script>
< / head>
< body>
< a class =reply_docdata-doc_value =1> 111< / a>< br>
< a href =#class =reply_docdata-doc_value =2> 222< / a>< br>
< a href =#class =reply_docdata-doc_value =3> 333< / a>< br>
< script type =text / javascript>
$(document).ready(function(){
$(。reply_doc)。click(function(){
var a = $(a.reply_doc)。attr (data-doc_value); //< - 添加此
if(a!=){
$ .ajax({
type:POST,
url:sortingajax.php,
data:doc_reply =+ a,
success:function(b){
alert(a +'ok。'+ b)
$('#ResponseDiv')。html(b);
console.log(b);
}
});
}
});
});
< / script>
< div id ='ResponseDiv'>
这是一个div来保存回复。
< / div>
< / body>
< / html>

sortingajax.php

 <?php 
include('connection.php');
$ countsql ='SELECT * FROM XML WHERE';
$ countsql1 = mysql_query($ countsql);
$ numrows = mysql_num_rows($ countsql1);
$ countArray2 = array();

while($ row = mysql_fetch_array($ countsql1)){
//追加到数组
$ countArray2 [] = $ row;
// echo $ row ['PID']。< BR />;
}

?>
<?php
foreach($ countArray2 as $ array)
{
?>
< div class =search>

我在美国。
< / div>
<?php

$ i ++; }?>

在第一页中。当我点击一个链接并在那里它会调用sortingajax.php和ajax call.After后,它应该用sortedajax.php内容替换ResponseDiv内容。



请告诉我我做错了,因为我不知道ajax调用... 您的ajax代码看起来正确,但另一方面php代码看起来有点不对。首先,停止使用已弃用的 mysql _ * api并切换到 PDO mysqli

mysql _ * api已弃用,并将很快从PHP中删除。

  SELECT * FROM XML WHERE 

^这不是一个正确的mysql查询,你需要在 WHERE 关键字之后添加一些内容来检查,或者如果你想要跳过它从 XML 表中选择所有行。

由于此查询失败,您将不会有任何要打印的数据,并且您的<$ c呼叫完成后,$ c> ResponseDiv 应为空。

I am trying to replace the div content dynamically ajax call.Following is the code which i tried.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<a  class="reply_doc" data-doc_value="1">111</a><br>
<a href="#" class="reply_doc" data-doc_value="2">222</a><br>
<a href="#" class="reply_doc" data-doc_value="3">333</a><br>
<script type="text/javascript">
$(document).ready(function(){
  $(".reply_doc").click(function () {
    var a = $("a.reply_doc").attr("data-doc_value"); //<-- Add this
    if (a != "") {
    $.ajax({
        type: "POST",
        url: "sortingajax.php",
        data: "doc_reply=" + a,
        success: function (b) {
            alert(a+' ok. '+b)
            $('#ResponseDiv').html(b);
            console.log(b);
        }
    });
  }
 });
});
</script>
<div id='ResponseDiv'>
        This is a div to hold the response.
</div>
</body>
</html>

sortingajax.php

<?php
include('connection.php');
$countsql='SELECT * FROM XML WHERE';
$countsql1=mysql_query($countsql);
$numrows = mysql_num_rows($countsql1);  
$countArray2=array();

while($row = mysql_fetch_array($countsql1)) {
    // Append to the array
    $countArray2[] = $row;
    //echo $row['PID']."<BR />";
}

?>
<?php
foreach($countArray2 as $array)
{
?>
        <div class="search">

        I am in US.
        </div>
        <?php

 $i++; } ?>

In the first page.when i click on a link and there it will call sortingajax.php with ajax call.After that it should replace ResponseDiv content with sortingajax.php content.

Please tell me where i am doing wrong as i m not that aware of ajax calls...

解决方案

Your ajax code looks correct, the php code on the other hand looks a bit wrong. First off, stop using the deprecated mysql_* api and switch to either PDO or mysqli.
The mysql_* api is deprecated and will be removed from php soon.

SELECT * FROM XML WHERE

^ This is not a correct mysql query, you need to add something to check for after the WHERE keyword, or just skip it if you want to select all the rows from the XML table.
Due to this query failing, you will not have any data to print, and your ResponseDiv should be empty after the call has been done.

这篇关于用ajax替换div内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆