YourNameHere Posted June 13, 2009 Share Posted June 13, 2009 I have a simple ajax script. when a button is clicked the external .php page is loaded but the php script and javascript wont execute. Do I need to include/require the nessesary scripts on the main page that does the ajax call? or am I missing something here? can I keep the scripts on the external page that is being loaded and do something different with the ajax call? Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/ Share on other sites More sharing options...
dreamwest Posted June 13, 2009 Share Posted June 13, 2009 You can have ajax.js in a different directory http://site.com/ajax/ajax.js and execute from http://site.com/index.php which will go to something like http://site.com/execute.php just by adding a link Are you trying to POST or GET to the execute.php page? POST is a form your posting, GET will be a form of post but in a hyperlink Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-854879 Share on other sites More sharing options...
YourNameHere Posted June 13, 2009 Author Share Posted June 13, 2009 The ajax script itself if executing properly. It loads the page into to element but the page that loads has both php and javascript that do do not execute. the issue is this: when I load pms.php with a post using the $_SESSION['username'] variable it works, it loads the PMs from the database. but when it is called via AJAX it doesnt. it just loads the table that holds the pms. I have tried it with GET because I thought that maybe that was the only way to do it but that did the same thing. I would prefer to use POST as that is was I am most familier with. Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-854895 Share on other sites More sharing options...
dreamwest Posted June 13, 2009 Share Posted June 13, 2009 ajax.js - ive even thrown in a loading div for you var object_busy= false; ShowLoading = 1 ; var MyTimer = null; Myvar = "<table align='center'><tr><td><img src='/templates/images/loading.gif' ></td></tr></table>"; function RequireAjaxData($Request, $Control) { if ($Control == "" || $Control == null) {alert ("No output specified !"); return;} var ai = new AJAXInteraction($Request, GetServerData, $Control ); ai.doGet(); } function GetServerData ($TheData, $Control){ document.getElementById($Control).innerHTML = $TheData; } function AJAXInteraction(url, callback, $Control) { var req = init(); req.onreadystatechange = processRequest; function init() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if (window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else {alert ("Your browser seems to be out of date, Please update!"); return; } } function processRequest () { if (req.readyState == 4) { if (req.status == 200) callback(req.responseText, $Control); } else callback(Myvar , $Control); } this.doGet = function() { req.open("GET", url, true); req.send(null); } this.doPost = function(body) { req.open("POST", url, true); req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); req.send(body); } } index.php <script src="/ajax.js"></script> <div id='MyDIV'> <a href='' onclick='RequireAjaxData("/submit.php?id=id_here", "MyDIV"); return false;'> Test me!</a></div> submit.php //database stuff //Always echo from this page, dont try to display templates from this page echo "Im working"; Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-854900 Share on other sites More sharing options...
YourNameHere Posted June 13, 2009 Author Share Posted June 13, 2009 Thanks for the response Dreamwest. That code is useful, however, it doesnt really answer my question. when loading the external page, it doesnt run any of the scripts on that page, even with get perameters. I tried adding all the code that connects to the database to the main page that houses the ajax call but that did nothing. I am a begginner with ajax so you code fell on deaf ears I am afraid. Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-855105 Share on other sites More sharing options...
dreamwest Posted June 13, 2009 Share Posted June 13, 2009 The code i posted is ready made - just cut n paste it and your away So your saying your trying to execute a page on a remote server? If its a subdomain you can by adding the absolute url /homes/user/site/page.php Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-855167 Share on other sites More sharing options...
jacksonmj Posted June 13, 2009 Share Posted June 13, 2009 By "external", do you mean that it is on a different domain or subdomain? Perhaps post the relevant code? Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-855225 Share on other sites More sharing options...
dreamwest Posted June 13, 2009 Share Posted June 13, 2009 External is remote domain http://remote.com //remote Local is still subdomain http:sub.site.com //local Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-855228 Share on other sites More sharing options...
YourNameHere Posted June 13, 2009 Author Share Posted June 13, 2009 The pages are all on the same server. You say it is ready-made. I will try and cut & paste it. I just feel like I am missing a fundimental rule to AJAX that is causing my problem. Will update as soon as I get your script working. Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-855231 Share on other sites More sharing options...
YourNameHere Posted June 14, 2009 Author Share Posted June 14, 2009 well I cant seem to get it to work. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-855270 Share on other sites More sharing options...
dreamwest Posted June 14, 2009 Share Posted June 14, 2009 It works, ive been using it for 6 months now Check over everthing again, make sure the files are in the root directory for testing http://site.com/index.php http://site.com/ajax.js http://site.com/submit.php Go to index.php > Click the link "Test me!" > It should Remove the link and replace it with the echo from submit.php page - "Im working" Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-855306 Share on other sites More sharing options...
YourNameHere Posted June 14, 2009 Author Share Posted June 14, 2009 It works You're right, I was being an idiot. that works but am still having issues porting it over to accept GET variables, or even POST. I will try more in the morning. thanks again, I am MUCH closer to figuring this out! Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-855485 Share on other sites More sharing options...
dreamwest Posted June 14, 2009 Share Posted June 14, 2009 Youll only need $_POST if you have a form to submit, otherwise use get ....'RequireAjaxData("/submit.php?id=Get_id_here", "MyDIV");...... And in submit.php $id = $_GET['id']; Quote Link to comment https://forums.phpfreaks.com/topic/162013-php-script-not-running-with-ajax/#findComment-855487 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.