Jump to content

JavaScript functions not executing after AJAX request


robs99

Recommended Posts

Hello,

i'm trying to load a php file through AJAX which accesses a mysql db. the php file loads fine, but for some reason i can't run any javascript functions inside the file. here's the code of the php file:

 

  <?php

include ('sql.php');

if (!$_GET[start]) { $_GET[start] = 0; }

$result = mysql_query("SELECT * FROM `posts` WHERE `post_content` NOT LIKE '' AND `post_status` NOT LIKE 'draft' AND `post_status` NOT LIKE 'inherit' $sqlsearch ORDER BY `post_date` DESC , `ID` DESC LIMIT $_GET[start] , 1");

if (mysql_num_rows($result) == 0) {
echo "Sorry, no posts found. Please search for something else.<br>";
}

while($row = mysql_fetch_array($result))
{
echo "$row[post_title]<br><br>";
if ($row[genre3]) { echo "$row[genre1] - $row[genre2] - $row[genre3]"; }
elseif ($row[genre2]) { echo "$row[genre1] - $row[genre2]"; }
else { echo $row[genre1]; }

echo "<br><br><br><a href=\"javascript:runme();\">Run Me</a>";
}
?>
<script type="text/javascript">
function runme() {
alert ('test');
}
</script> 

 

any ideas why the runme function doesnt run? if i access the file directly the function works fine. stuff like <a href="javascript:alert('test');"> works aswell.

You can view the script in action here: http://it-leaked.com/v4test/

 

thanks for the help if you need any other files let me know :)

yup...how are you doing your ajax though? manually with XMLHttpRequest or with a library like jQuery/prototype? If you are doing it manually, you need to parse and eval() the javascript. I would just use jQuery to do the AJAX calls for you

hey,

i'm doing it manually. how exactly would the eval() work? ive tried to eval the responseText but then the loading state gets stuck at 2.

here is my js code:

 

function ShowNewReleasesAjax(start,divnumber) {
var ajaxRequest;

try{
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			alert("Your browser broke!");
			return false;
		}
	}
}
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		var ajaxDisplay = document.getElementById("DivNewReleases" + divnumber);
		ajaxDisplay.innerHTML = ajaxRequest.responseText;
	}
		else {
		var ajaxDisplay = document.getElementById("DivNewReleases" + divnumber);
		ajaxDisplay.innerHTML = '<br><br><br><br><br><br><image src=\"php/ajax-loader.gif\">';
	}
}
ajaxRequest.open("GET", "php/releases.php?start=" + start + "&divnumber=" + divnumber, true);
ajaxRequest.send(null);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.