繁体   English   中英

如何将PHP网址变量传递给AJAX?

[英]How to pass PHP url variables to AJAX?

我如何将PHP URL变量传递给AJAX,以便可以在同一页面中加载页面内容? 这是我要执行的操作的示例。例如,我尝试将“ profile.php?id =”转换为AJAX,以便加载页面内容。.但是,我首先开始使用循环..我不知道这是否是正确的方法..

下面是代码

<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>

改变这个-

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

为此-

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

根据评论编辑-试试这个-

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);

经过这么长时间的聊天-

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

$row['ID']传递给您的loadXMLDoc()方法。

最终代码-

<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>

取代这个

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

有了这个

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

; 非常重要。
如果不写,则不会回显该值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM