Echo Ajax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ggobieski
Forum Newbie
Posts: 4
Joined: Sun Aug 07, 2011 9:52 am

Echo Ajax

Post by ggobieski »

Hi all,

Currently I am working on a multi-core internet plugin for php and javascript. When it is done it will be able to run functions asynchronously on different computers. The issue is the all-important result ajax call, which temporary saves the result of a function to an xml file. Below is the code:

echo '<script type="application/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>';

echo"<script>var result = eval('".$code."');</script>"; //evaluates the function and stores it in var result

echo'<script>$.get("fxn.php", { r: ""+result, id:""+'.$id.' } );</script>'; //sends the data to the php file to be saved

Note: $id is the identifier of the result

Here is the code from the file that the data is sent to:

$xml = simplexml_load_file("results.xml");
$doc = new SimpleXMLElement($xml->asXML());
$object = $doc->addChild('result');
$object->addAttribute("id",$_GET['id']);
$object->addAttribute("result",$_GET['r']);
$doc->asXML('results.xml');

The problem is it never sends the data to that file. Any help would be much appreciated. Thanks.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Echo Ajax

Post by Jonah Bron »

ggobieski wrote:Currently I am working on a multi-core internet plugin for php and javascript.
What does "multi-core" mean?

Looking at your code, it appears that the XML isn't being saved back to the file. Did you omit that part? By the way, please post your code in tags (the "PHP Code" button), it makes it much easier to read.
ggobieski
Forum Newbie
Posts: 4
Joined: Sun Aug 07, 2011 9:52 am

Re: Echo Ajax

Post by ggobieski »

Sorry for the incorrect syntax. Multi-core basically means that multiple computers will be doing the work at the same time. To answer your second question, the code to save the xml is correct – I did not omit anything. The real problem is getting it to send the information to that file. Here I will provide all of the code including the debugging statements for that file:

Code: Select all

echo '<script>alert("FXN")</script>'; //should pop up with an alert right?
		$xml = simplexml_load_file("results.xml");
		$doc = new SimpleXMLElement($xml->asXML());
		$object = $doc->addChild('result');
		$object->addAttribute("id",$_GET['id']);
		$object->addAttribute("result",$_GET['r']);
		$doc->asXML('results.xml'); //this statement saves the file
And once again the code for the echo ajax:

Code: Select all

echo"<script>var result = eval('".$code."');</script>";
				echo "<script>alert('Mid');</script>";
				echo'<script>$.get("fxn.php", { r: ""+result, id:""+'.$id.' } );</script>';
				echo "<script>alert('end');</script>";
Thanks.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Echo Ajax

Post by Jonah Bron »

I see, yeah. No, that won't pop up an alert. You need to attach an event to the AJAX call with success().

Code: Select all

echo'<script>$.get("fxn.php", { r: ""+result, id:""+'.$id.' } ).success(function() { alert("FXN"); });</script>';
Did you check the XML file to see if the contents are there?
ggobieski
Forum Newbie
Posts: 4
Joined: Sun Aug 07, 2011 9:52 am

Re: Echo Ajax

Post by ggobieski »

Yes I checked the xml file and it is unchanged – not updated. I also went back to the call and used your code with the success alert and sure enough no alert. I now know where the problem is, but I don't see any errors. Here is the code that you gave me:

Code: Select all

echo'<script>$.get("fxn.php", { r: ""+result, id:""+'.$id.' } ).success(function() { alert("FXN"); });</script>';
Thanks for the response!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Echo Ajax

Post by Jonah Bron »

Then you need to do some debugging. Are you sure you typed the file name right? Try adding an error event as shown in the documentation here:

http://api.jquery.com/jQuery.get/#jqxhr-object
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Echo Ajax

Post by twinedev »

A good tool for trouble shooting AJAX calls is a program for windows called Fiddler (at fiddler2.com). You turn it on, and all browser requests (and results) are passed through it, so you can see the raw data sent to an ajax request and the raw results from it. Can save a lot of time adding debug code to track what is going being sent and received. This is especially true when you are doing AJAX (and form troubleshooting) that uses POST instead of GET, as you can see ALL post AND GET data being sent with the request. If you have a page that is making calls wrong, you can quickly find it.

-Greg

PS, I forget if it auto sets it up or not with current versions, but there is an Add-On for firefox (gives you an icon to turn on/off sending traffice through fiddler, I usually leave it set to "Automatic", so that if the program is running, it uses it.) Oh another hint, don't have facbook open while using it or streaming something like Netflix. fills up the logs like crazy LOL
ggobieski
Forum Newbie
Posts: 4
Joined: Sun Aug 07, 2011 9:52 am

Re: Echo Ajax

Post by ggobieski »

So after much tinkering and using the code from the Jquery site, I finally got it to work! Thanks much. :D :D
viramkhand
Forum Newbie
Posts: 1
Joined: Sun Aug 07, 2011 10:30 pm

Re: Echo Ajax

Post by viramkhand »

hello sir ,
i am from INDIA ,and i want to know the best forum development platform,other than phpBB,in addition i also want to know that in which platform the following site forum is formed:

http://www.pagalguy.com/forum/?ref=mm
http://forum.santabanta.com/

please reply as early as possible
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Echo Ajax

Post by Jonah Bron »

@viramkhand: I don't know that there is necessarily a "best". I haven't personally used any of them. Both of those sites use vBulletin.

In future, if you have a question please create a new thread for it, it's best to keep threads on-topic.
Post Reply