Jump to content


PHP/Ajax include problem


7 replies to this topic

#1 umbra

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 10 July 2008 - 12:27 PM

Hello, i've been working with ajax for a few weeks now and i have a simple question. I want to keep this somewhat simple but tell me if i need to give more.

I have an ajax method that is being called, lets say....
function listClient(str)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="/lib/clients.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=generateHTML
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
When it opens up /lib/clients.php and runs everything works fine and dandy.

Athough as my script has progressed in complexity, i turns out i need to add an include file in that clients.php

whenever i add the include file ---- require_once('file.php'); The script stops working. Let me be more clear the clients.php script still works, but the ajax doesn't.

(The Entire process is this... going from html and using Jquery to tell if someone clicked something.. jquery called the ajaxs method and that opens up the connection to the php script. PHP runs and then generates xml in which ajax/jquery reads and then sends back to html. Again the entire process works if i don't add that include (the include file is only one line right now) so i think its something wrong with ajax getting confused)

Please help

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 July 2008 - 05:55 PM

You are putting the require() line inside of clients.php right (not the JS)?

Inside of your generateHTML() function, check the responseText to make sure PHP isn't giving you any errors.

Edited by rc69, 11 July 2008 - 05:55 PM.


#3 umbra

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 14 July 2008 - 10:02 AM

I think we're getting further.. if i change it to responseText it works with that include line. However i don't know how to parse the data if its in Text mode. As i said before if i remove the require_once line the xml works but i need to require that file.

Any hints?

here is the top of my clients.php script
<?php
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

require_once('storecommands.php');

$q=$_GET["q"];
$id=$_GET["id"];

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 July 2008 - 12:50 PM

What's in storecommands.php (the entire file if possible)?

#5 umbra

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 14 July 2008 - 03:17 PM

storecommands.php
<?php
/*
Used to Store Common Mysql Commands
Author:
Created: 7-10-2008
Last Modified:

Usage: $class['section']['field']
Defined Classes: $mgnt $clprt $prj $sale
*/

$mgnt['client']['list'] = 'SELECT c.CustID,cc.Fname,cc.Lname,c.CompanyName,cc.E$
?>

and here is a function from my ajax script

function generateHTML()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
xmlDoc=xmlHttp.responseXML;
var count=0;
while (count < 2)
{
var company=xmlDoc.getElementsByTagName("company")[count].childNodes[0].nodeValue;
var address=xmlDoc.getElementsByTagName("address")[count].childNodes[0].nodeValue;
var city=xmlDoc.getElementsByTagName("city")[count].childNodes[0].nodeValue;
var state=xmlDoc.getElementsByTagName("state")[count].childNodes[0].nodeValue;
var zip=xmlDoc.getElementsByTagName("zip")[count].childNodes[0].nodeValue;
company="<div id='clientID' name='clientID' class="+company+">"+company+"</div>";
addData("content","<tr><td>"+company+"</td><td>"+address+"</td><td>"+city+"</td><td>"+state+"</td><td>"+zip+"</td></tr>");
count++;
}
$("div[@name='clientID']").click(function(event) {
var id=$(this).attr('class');
listClient('info',id);
});
}
}
function addData(field,html)
{
$('#'+field+'').append(html);
}

#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 14 July 2008 - 08:43 PM

This is why you should debug with responseTxt
<?php
/*
Used to Store Common Mysql Commands
Author: 
Created: 7-10-2008
Last Modified:

Usage: $class['section']['field']
Defined Classes: $mgnt $clprt $prj $sale
*/

$mgnt['client']['list'] = 'SELECT c.CustID,cc.Fname,cc.Lname,c.CompanyName,cc.E$';
?>
Forgot the quote on the end of the string. I'm not going to mention the semi-colon because i have mixed feelings about it. And i have no idea why the '$' is there...

#7 umbra

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 15 July 2008 - 08:40 AM

my bad, that isn't the whole storecommands.php

it does continue and close off at the end... its just that i was copying from nano and it was cut off.

it appears this is just a problem with XML.. i am going to figureout a way to just used response text

#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 15 July 2008 - 11:21 AM

The only way i can see this being a problem is if storecommands was outputting something that wasn't valid XML, that's where your issue is. You don't need to find a way to use responseText, just use it temporarily to see if anything is happening that would mess with the XML.
i.e.
function generateHTML()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		window.alert(xmlHttp.responseText);
		return;
		xmlDoc=xmlHttp.responseXML;
		var count=0;
		//...
That will show what is in the response and you can then determine whether or not storecommands is actually messing things up.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users