Thank you for your reply but I don't if your solution will work
Code: Select all
function init(){
var link1 = document.getElementById("link_to_click");
link1.onclick = showAlert();
}
This means all the links will have the same name which is "link_to_click". Remember, my links will look like as follows:
Code: Select all
<a href="news.php?q=1"> news title 1 </a><br/><a href="news.php?q=2"> news title 2 </a><br/><a href="news.php?q=3"> news title 3 </a>
So if I use your function all the time the value will be same.
I thought of this solution:
Code: Select all
<a href="javascript : process( <? echo $row['id']; ?> );" > echo $row['title']; </a><br />
<a href="javascript : process( <? echo $row['id']; ?> );" > echo $row['title']; </a><br />
So it will look like this
Code: Select all
<a href="javascript : process(1);" > news title 1 </a><br />
<a href="javascript : process(2);" > news title 2</a><br />
Relevant section from my Javascript file looks liek this (test.js)
Code: Select all
function process(name)
{
// proceed only if the xmlHttp object isn't busy
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
{
// execute the test.php page from the server
xmlHttp.open("GET", "test.php?name=" + name, true);
// define the method to handle server responses
xmlHttp.onreadystatechange = handleServerResponse;
// make the server request
xmlHttp.send(null);
}
else
// if the connection is busy, try again after one second
setTimeout('process()', 1000);
}
Relevant PHP bit (test.php)
Code: Select all
<?php
// retrieve the user name
$name = $_GET['name'];
//include('includes/DbConnector.php');
mysql_connect ("localhost", "root", "")or mysql_error();
mysql_select_db("test");
//$connector= new DbConnector();
$check = "Select * FROM news where newsId = '".$name."' ";
echo $check;
$result = mysql_query($check);
$row= mysql_fetch_array($result);
if(mysql_num_rows($result)>0){
while($row = mysql_fetch_array($result))
{
echo "<span class=style1>".$row['title']."</span><br>";
echo "<span class=style1>".$row['description']."</span><br>";
}
}
else{
echo "<span class='infoTxt'>Sorry no match found. Please try again. </span>";
}
?>
So, now when I click on the first link it shows the sql statement correctly. I.e something like this
[sql] SELECT * FROM news WHERE newsId = 1 [/sql]
it waits 2/3 seconds then prints
[sql] SELECT * FROM news WHERE newsId = undefined [/sql]
And displays no result at all.
I have copied the first statement ( Select * FROM news where newsId = 1 )and ran it in my phpmyadmin and it gives me row. So sql command is fine but for some reason it is not working. since I can see the value being changed in my sql means the js and php is working fine as long as parameter passing is concerned.
I will try out <body onload = process(); > but not sure why do you have to use it?
Any help appreciated.