Trying to retrieve a response from another page, specifically named ajaxresponse.php
, through an AJAX request. I also aim to execute some JavaScript actions on that ajaxresponse.php
page, but the JavaScript code is not functioning as expected. Although I am receiving the response from the page, the JavaScript code on that page is not working. The main query is why the AJAX code on my second page is not operational?
function ajaxinput(tinnerid)
{
var xmllhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("articledive").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajaxresponse.php?q="+tinnerid,true);
xmlhttp.send();
}
Below is the PHP code for the first page:
<?php
$con1=mysqli_connect("127.0.0.1","root","root","databasetry");
$result=mysqli_query($con1,"select title,articleid from article");
$tinnerid=0;
$articledivid=0;
while($row = mysqli_fetch_array($result))
{
$tinnerid=$row['articleid'];
echo "<div class='tinner' id='$tinnerid' onclick='ajaxinput($tinnerid)' style='cursor:pointer;'>";
echo"<span class='fontdiv'>";
echo $row['title']."<br>";
echo "</span>";
echo"</div>";
$tinnerid++;
$articledivid++;
}
mysqli_close($con1);
?>
Now moving onto the JavaScript code for the second page:
function run()
{
alert("example");
}
Last but not least, here is the PHP code of the second page:
<?php
$id=$_GET['q'];
$result=mysqli_query($con1,"select user.username,article.articleid,article.title,article.artiletext from article inner join user on article.user_id=user.user_id where article.articleid=$id");
$divid=0;
while($row = mysqli_fetch_array($result))
{
$id=0;
echo"<div class='inner' id=$divid >";
$id=$row['articleid'];
echo "<font class='ftitle'>"."Title : "."<a href='#'onclick='ajaxinput($id)'>".$row['title']."</a>"."</font>"."<br>"."<font class='ftext'>".$row['artiletext']."</font>"."<br>"."<font class='flast'>"."Posted By : ".$row['username']."</br>"."</font>"."<br>"."<br>";
echo "<button type='button' onclick='con($id)' class='button'><font size='1'>Delete</font></button>";
echo"</div>";
$divid++;
echo"<script>run()</script>";
}
mysqli_close($con1);
?>