Page 1 of 1

Problem with ajax script

Posted: Sat Feb 05, 2011 8:31 am
by K-Z
Hi...I am new to ajax and currently developing a website where users are supposed to rate posts. I want to avoid page refreshing when a rate is given. To do that, the php code is

Code: Select all

<form name='rate' >
<?php 
echo "<input type='image' src='actstar.gif'  name='rate1' id='button1' value='1' title='Value rating 1' onclick='ajaxFunction(this.value, $varid)'/>";
      echo "<label for='label2'></label>";
echo "<input type='image' src='actstar.gif'  name='rate2' id='button2' value='2' title='Value rating 2' onclick='ajaxFunction(this.value, $varid)'/>";
echo "<label for='label3'></label>";
echo "<input type='image' src='actstar.gif'  name='rate3' id='button3' value='3' title='Value rating 3' onclick='ajaxFunction(this.value, $varid)'/>";
echo "<label for='label4'></label>";
echo "<input type='image' src='actstar.gif'  name='rate4' id='button4' value='4' title='Value rating 4' onclick='ajaxFunction(this.value, $varid)'/>";
echo "<label for='label5'></label>";
echo "<input type='image' src='actstar.gif'  name='rate5' id='button5' value='5' title='Value rating 5' onclick='ajaxFunction(this.value, $varid)'/>";
echo "<label for='label6'></label>";
?>
</form>
And the ajax code is

Code: Select all

<script type="text/javascript" src="/home/walknsh1/public_html/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>

<script language="javascript" type="text/javascript">



function ajaxFunction(a, b){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}

ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		// document.rate.time.value = ajaxRequest.responseText;
	}
}
 	var queryString = "?val=" + a + "id=" + b;
	ajaxRequest.open("GET", "actrating.php" + queryString, true);
	ajaxRequest.send(null); 
	
}
</script>
Now the problem is that nothing is happening. I have done the coding as per w3schools tutorials but here actrating.php is not being called on hitting the button.
Whats the issue?? M I missing somegthing?? Plzz Help... :(

Re: Problem with ajax script

Posted: Mon Feb 14, 2011 7:01 pm
by Technocrat
Is there a reason you are not using the JQuery ajax function since you are already including it?
http://api.jquery.com/category/ajax/