Calling AJAX function from within PHP

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sdeleon28
Forum Newbie
Posts: 4
Joined: Tue Mar 30, 2010 6:41 am

Calling AJAX function from within PHP

Post by sdeleon28 »

Hi,
I'm having problems calling a JavaScript from within a PHP app. I'm developing this small system that has a menu on the left and a box where PHP content is outputted according to the option selected. Here's the code for my main page (just in case you can't imagine it):

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"lang="es" xml:lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Adamantoise</title>
<?php 
require_once "templates/addStyle.php"; //adds some stupid css style
?>
<script language="JavaScript" type="text/javascript" src="../js/ajax.js"></script>
</head>
<body>
<div id="menu"> <a href="javascript:fill('./login.php', 'contenidos', '', 'POST');">Login</a></div>
<div id="contenidos">
   <h3>Welcome blah</h3>
</div>
</body>
</html>
The redirect function I call there is an AJAX function that fills the div with id "contenidos" with the output from login.php. The login.php script displays a login thingy inside the right box, and calls fill again on the event onsubmit (back to login.php) but sends some parameters to itself. Then if the login is correct, login.php should call fill again to replace the contents of the box with the real stuff, but as I am only reloading a piece of the page through AJAX the head section of the page has already been echoed, and

Code: Select all

echo "<script type=text/javascript>redirect('./menu.php', 'contenido', '', 'POST');</script>";
is not executed when the page is loaded. The thing is: where should I place the script tag in order for it to execute immediately?

Please let me know if I haven't made myself clear, and sorry if I went crazy on the details.

Santiago
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: Calling AJAX function from within PHP

Post by ell0bo »

You have the javascript in the head, which means that it will get called before the body even loads. You need to put that call in the onload of the body.
sdeleon28
Forum Newbie
Posts: 4
Joined: Tue Mar 30, 2010 6:41 am

Re: Calling AJAX function from within PHP

Post by sdeleon28 »

Yes, I would love to be able to do that but as I said earlier, the only part of the page that I'm am reloading is the content of the div with the id "contenidos" (that is, every echo that comes from the login.php will but printed INSIDE the div component). Again, my question is if there is any way to call the function from within that div component. Thanks for your help!
sdeleon28
Forum Newbie
Posts: 4
Joined: Tue Mar 30, 2010 6:41 am

Re: Calling AJAX function from within PHP

Post by sdeleon28 »

I also found some strange behavior: if I

Code: Select all

echo "<script type='text/javascript'>alert('Hello!')</script>"
after authentication on login.php, the script is outputted in the source of the html page but not executed. The interesting thing is when I open Firebug, choose edit and then change one char from the script and then change it back to "alert('Hello!')" the message does come up! I guess it has to do with the browser not refreshing the whole page when I modify one single piece of it through AJAX. Any ideas?

Note: I changed my js funtion for an alert in order to diagnose the problem'but I'm still trying to do the same thing I was doing at the beggining.
Post Reply