Jump to content

php script not running with ajax


YourNameHere

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.