View Full Version : Ajax script does nothing

12-18-2007, 07:08 PM
Below you can see the code i'm using.
I'm using it to send an id number to vote.php. Here this id should be saved in a mysql database. When i go to vote.php?id=3 directly in my browser it saves the id (3) in my mysql database so the vote.php page is totally correct and working.
But for some reasing, when i use the link, it doesn't save the id. I do however get the alert messages that i have voted so there's nothing really wrong with the javascirptcode. Why isn't it saving the id when i click on the link?

<?php include("header.php");?>

<!--AJAX REFRESH (hoho, (c) AdJ )-->
<script language="javascript" type="text/javascript">
//Browser Support Code
function ajaxFunction(id,lied){
var ajaxRequest;

// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Er is een probleem met (de instellingen van) uw browser, u kan niet stemmen!");
return false;
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
// if(ajaxRequest.readyState == 4){
// document.myForm.time.value = ajaxRequest.responseText;
// }
//var id = document.getElementById('id').value;
this.lied= lied;
this.id= id;

var queryString = "?id=" + id;

ajaxRequest.open("GET", "http://www.webmasterhelp.biz/pilotlight/vote.php" + queryString, true);
alert('Merci! Je stemde voor ' + lied + '!');

<!--einde ajaxbedoening en een half uur denkwerk-->
<div style="width:710px; height:554px; overflow:auto;
background-image:url(images/index_r4_c2.gif);"><img src="http://www.webmasterhelp.biz/pilotlight/Titles/Home%20Title.gif">

//DB Connectie
$host = "localhost";
$user = "x";
$pass = "x";
$db = "x";
mysql_connect($host,$user,$pass) or die(mysql_error());

$query = mysql_query("Select lied,artiest,id From proposals WHERE approved='1' ORDER BY ID DESC");

<p align="left"><u>De ingestuurde voorstellen!</u>
<br><br>Hieronder staan alle liedjes die zijn voorgesteld op onze site. De uitzonderlijk schrale, grappige en ondoenbare voorstellen hebben we hier wel al uit geschrapt. Dit wil natuurlijk niet zeggen dat we ze ook echt spelen. Als je toch heel graag een nummer van de lijst door ons gecoverd ziet, kan je op het liedje klikken! Zo weten wij wat jullie graag zouden horen...<br>
<table width="90%" border="1" bordercolor="#3D515A">
<tr bgcolor="#047391">
<td width="35%"><b>Artiest</b></td>
while($obj = mysql_fetch_object($query)){
<tr bgcolor="<?php echo"$c";?>">
<td width="35%"><?php echo " $obj->artiest"; ?></td>
<td><A style="text-decoration: none;" HREF="#" onClick="ajaxFunction(<?php echo"$obj->id";?>,'<?php echo"$obj->lied";?>')"> <?php echo " $obj->lied"; ?></a></td>
if($i=='2'){$i=0;} //I RESETTEN}

<?php include("footer.php");?>

12-18-2007, 08:30 PM
Have you verified that your queryString is correct? Try to alert() it before sending the request to your page.

You pass id in as a parameter, then you have id commented out, then you have this.id=id.

Following that line of thought, make sure this call has what you expect in it:

onClick="ajaxFunction(<?php echo"$obj->id";?>,'<?php echo"$obj->lied";?>')"> <?php echo " $obj->lied"; ?>

That should probably be in your while loop.

12-19-2007, 03:35 PM
i'll check the query with an alert, just a moment

12-19-2007, 03:41 PM
The query is correct, if i alert the var querystring, the return box returns to me for example: ?id=54 so the squerystring is correct, then what is the problem?

12-19-2007, 03:45 PM
wtf! i changed nothing and now it's working! That's akward! Anyway, thanks a lot for trying to help!


Thank you! Bye!