robs99 Posted December 25, 2008 Share Posted December 25, 2008 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 25, 2008 Share Posted December 25, 2008 when loading JS through AJAX, you need to assign your JS a little differently. try this: <script type="text/javascript"> runme = function () { alert ('test'); } </script> Quote Link to comment Share on other sites More sharing options...
robs99 Posted December 25, 2008 Author Share Posted December 25, 2008 hey, thanks for the fast reply. i tried it but it didnt work either i still have to call the function through javascript:runme(); right? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 26, 2008 Share Posted December 26, 2008 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 Quote Link to comment Share on other sites More sharing options...
robs99 Posted December 26, 2008 Author Share Posted December 26, 2008 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); } Quote Link to comment Share on other sites More sharing options...
rhodesa Posted December 26, 2008 Share Posted December 26, 2008 you have to use regex to pull out just the part between <script> tags. seriously...save yourself the trouble and use jQuery: http://docs.jquery.com/Ajax/jQuery.get Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.