dsfykqq3403
2012-12-21 05:04
浏览 156
已采纳

如何将PHP url变量传递给AJAX?

How could I pass PHP URL variables to AJAX so I can get the page content load in the same page ?? Here is an example of what I am trying to to do .. I am trying for example to get the "profile.php?id= to AJAX so that page content be be loaded .. However , I first started using a loop .. I dont know if that's the right way to go about it ..

Below is the code

<div id="myDiv"><h2>Let AJAX change this text</h2></div>


<?php
require('../madscore/database/connect.php');
database_connect();
$query = "select * from Entertainers";
$result = $connection->query($query);
$row_count =$result->num_rows;

for($i = 1; $i <= $row_count; $i++)
  {
   $row = $result->fetch_assoc();
    //echo $i. "<br />";
   // echo $row['Name']."<br />";
   // echo $row['Profession']."<br />";
   // echo $row['Score']."<br />";

?>

<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","profile.php?id="<?php echo $row['ID'] ?>, true");
xmlhttp.send();
}
</script>




<?php  echo "<a href='/profile.php?id=".$row['ID']."' onclick='loadXMLDoc()'><img src ='../".$row['Picture']."' width='100' height='100' /> </a>"; } ?>

</body>
</html>

图片转代码服务由CSDN问答提供 功能建议

我如何将PHP URL变量传递给AJAX,以便我可以在同一页面中加载页面内容? 这是我想要做的一个例子。我试图让“profile.php?id =到AJAX,以便加载页面内容..但是,我首先开始使用循环.. 我不知道这是不是正确的方法..

下面是代码

 &lt; div id =“myDiv  “&gt;&lt; h2&gt;让AJAX更改此文字&lt; / h2&gt;&lt; / div&gt; 
 
 
&lt;?php 
require('.. / madscore / database / connect.php'); 
database_connect(  ); 
 $ query =“select * from Entertainers”; 
 $ result = $ connection-&gt; query($ query); 
 $ row_count = $ result-&gt; num_rows; 
 
for($ i =  1; $ i&lt; = $ row_count; $ i ++)
 {
 $ row = $ result-&gt; fetch_assoc(); 
 // echo $ i。“&lt; br /&gt;”; 
 /  / echo $ row ['Name']。“&lt; br /&gt;”; 
 // echo $ row ['Profession']。“&lt; br /&gt;”; 
 // echo $ row [' 分数']。“&lt; br /&gt;”; 
 
?&gt; 
 
&lt;!DOCTYPE html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; script&gt; 
function loadXMLDoc()\  n {
var xmlhttp; 
if(window.XMLHttpRequest)
 {// IE7 +,Firefox,Chrome的代码 ,Opera,Safari 
 xmlhttp = new XMLHttpRequest(); 
} 
else 
 {// IE6的代码,IE5 
 xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”); 
} 
 
xmlhttp.onreadystatechange =  function()
 {
 if if(xmlhttp.readyState == 4&amp;&amp;  xmlhttp.status == 200)
 {
 document.getElementById(“myDiv”)。innerHTML = xmlhttp.responseText; 
} 
} 
 
xmlhttp.open(“GET”,“profile.php?id =”  &lt;?php echo $ row ['ID']?&gt;,true“); 
xmlhttp.send(); 
} 
&lt; / script&gt; 
 
 
 
 
&lt;?php echo  “&lt; a href ='/ profile.php?id =”。$ row ['ID']。“'onclick ='loadXMLDoc()'&gt;&lt; img src ='.. /”。$ row [' 图片']。“'width ='100'height ='100'/&gt;  &lt; / a&gt;“;}?&gt; 
 
&lt; / body&gt; 
&lt; / html&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanhuang2150 2012-12-21 05:06
    已采纳

    Change this --

    xmlhttp.open("GET","profile.php?id="<?php echo $row['ID'] ?>, true");
    

    To this --

    xmlhttp.open("GET","profile.php?id=<?php echo $row['ID'] ?>", true);
    

    EDIT as per comments - Try this-

    myid = <?php echo $row['ID'] ?>; 
    
    //myid = "<?php echo $row['ID'] ?>";  //Or this if its a string type 
    
    
    xmlhttp.open("GET","profile.php?id="+myid, true);
    

    After such a long chat --

    <?php  echo "<a href='/profile.php?id=".$row['ID']."' onclick='loadXMLDoc(".$row['ID'].")'><img src ='../".$row['Picture']."' width='100' height='100' /> </a>"; } ?>
    

    Pass $row['ID'] to your loadXMLDoc() method.

    Final Code ---

    <div id="myDiv"><h2>Let AJAX change this text</h2></div>
    
    
    <?php
    require('../madscore/database/connect.php');
    database_connect();
    $query = "select * from Entertainers";
    $result = $connection->query($query);
    $row_count =$result->num_rows;
    
    for($i = 1; $i <= $row_count; $i++)
      {
       $row = $result->fetch_assoc();      
    ?>
    
    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function loadXMLDoc( myid )
    {
    var xmlhttp;
    var myloveid = id;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","profile.php?id="+myloveid, true");
    xmlhttp.send();
    }
    </script>
    
    
    
    
    <?php  echo "<a href='/profile.php?id=".$row['ID']."' onclick='loadXMLDoc(".$row['ID'].")'><img src ='../".$row['Picture']."' width='100' height='100' /> </a>"; } ?>
    
    </body>
    </html>
    
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题