[英]pass php variable to javascript function using ajax calls and getting response text on same page
I am using php sql database connectivity on a php page [search.php] for fetching data from a database in a variable "$url" and passing it to a javascript function where ajax calls catch that variable and passes it to another php page[function.php], This will execute some functions and send the response text back to search.php in a div whose id="rssoutput" see the code given below 我在php页面[search.php]上使用php sql数据库连接,以从变量“ $ url”中的数据库中获取数据,并将其传递给javascript函数,其中ajax调用捕获该变量并将其传递给另一个php页面[ function.php],这将执行一些功能并将响应文本发送回id为“ rssoutput”的div中的search.php,请参见下面的代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>search</title>
<script>
function showRSS(str) {
if (str.length==0) {
document.getElementById("rssOutput").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("rssOutput").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","function.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("could not connect:".mysql_error());
}
mysql_select_db("dbname",$con);
$query="SELECT * FROM table ";
$result=mysql_query($query);
while($runrows = mysql_fetch_assoc($result))
{
$url = $runrows ['URL'];
echo "<input type='button' class='myButton' onclick='showRSS($url);' name='artbttn' >
<div class='container' id='rssOutput' ></div>";
}
mysql_close($con);
?>
</body>
</html>
So , What i am trying to achieve is getting data from another page[function.php] and displaying it on same page [search.php] in a div"rssOutput" when i click on a button . 所以,我要实现的是从另一个页面获取数据,并在单击按钮时将其显示在div“ rssOutput”中的同一页面[search.php]上。 Can anyone tell me how is this possible. 谁能告诉我这怎么可能。
You might want to use urlencode before sending it to the next page, 您可能需要先使用urlencode,然后再将其发送到下一页,
$url = urlencode($runrows ['URL']);
and then enclose $url with quotes like this: onclick='showRSS(\\"$url\\");'
然后将$ url用这样的引号引起来: onclick='showRSS(\\"$url\\");'
echo "<input type='button' class='myButton' onclick='showRSS(\"$url\");' name='artbttn' >
<div class='container' id='rssOutput' ></div>";
This should work. 这应该工作。
Edit: rewrite mysql query with mysqli 编辑:用mysqli重写mysql查询
<?php
$conn = new mysqli('localhost', 'user', 'password', 'dbname');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql="SELECT * FROM mytbl";
$result=$conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$url = urlencode($row['url']);
echo "<input type='button' class='myButton' onclick='showRSS(\"$url\");' name='artbttn' ><div class='container' id='rssOutput'></div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I woner why i didn't get this before.There is a problem with url passing i think . 我不知道为什么以前没有得到这个。我认为url传递存在问题。 What u need to do is to create a session as i have shown . 您需要做的就是创建一个如我所显示的会话。
while($runrows = mysql_fetch_assoc($result))
{
$url = urlencode($runrows ['URL']);
$_SESSION['u']=$url;
<input type='button' onclick='showRSS()' class='myButton' name='artbttn' >
<div class='container' id='rssOutput' ></div>
}
mysql_close($con);
?>
<?php
echo<<<script;
<script>
function showRSS() {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("rssOutput").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","function.php",true);
xmlhttp.send();
}
</script>
script;
?>
</html>
add the following lines to function.php 将以下行添加到function.php
<?php
session_start();
if(isset($_SESSION['u']){
$url=$_SESSION['u'];
}
?>
and use this url on your page. 并在您的页面上使用此网址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.