繁体   English   中英

通过ajax传递字符串变量

[英]Passing a string variable through ajax

该变量通过ajax传递,它是一个数据库名称。 单击链接后,应该从数据库中检索数据。 链接和变量在同一页面上。 这是链接的代码:

$x = strval($_GET['x']);    

echo '<a href="#" onclick="showInformation('.$x.')">'.$seatid.'</a>';

$ x变量包含数据库的表名。 这是ajax的代码:

function showInformation(str)
{
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("txtInfo").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getinfo.php?x="+str,true);
xmlhttp.send();
}

这是getinfo.php:

<?php
session_start();
$_SESSION['login']="1";
$x = strval($_GET['x']);

$con = mysql_connect('localhost','root','Newpass123#','seatmapping');    
if (!$con){
    die('Could not connect: ' . mysql_error());
}

mysql_select_db('seatmapping');
$sql="SELECT name, seatid FROM $x WHERE seatid = 1";
$result = mysql_query($sql) or die("Query Error " . mysql_error());
...
...
?>

当我单击链接时,它无法显示表格中的数据,因此无法正常工作。 请帮我。 任何帮助都将受到赞赏。 提前致谢..

您没有传递期望元素的ID

echo '<a href="#" id="txtInfo" onclick="showInformation('.$x.')">'.$seatid.'</a>';

替换此行

echo '<a href="#" onclick="showInformation('.$x.')">'.$seatid.'</a>';

您能尝试一下吗:

更换线

 echo '<a href="#" onclick="showInformation('.$x.')">'.$seatid.'</a>';

随着以下:

echo '<a href="#" onclick="showInformation(\''.$x.'\')">'.$seatid.'</a>';

这样可以解决您的问题。

暂无
暂无

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

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