Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
newbie3

Chapter 5, Add_Employee, Ajax Status 200 & 304 Failed

Recommended Posts

Hi there

 

Just wonder why unable to make the if statement below "TRUE".

 

if ((ajax.status == 200) || (ajax.status == 304) ) {

 

Insert alert statements before and after the if statement above, got alert after "if (ajax.readyState == 4)".

 

Much appreciated in advance for assistance.

 

 

Regards

Share this post


Link to post
Share on other sites

Hi there

 

Ajax Status 200 & 304 is now successful and record has been added to the database.

 

However, there is no message on the screen that say record has been added.

 

Regards

 

newbie3

Share this post


Link to post
Share on other sites

Like you did before, try "echoing" the message onto the screen using the alert function.

After you have confirmed that there is indeed the message you want being produced, then probably the easiest way to put it on the screen with JavaScript is the innerHTML property.

 

For example:

 

<div id="message"></div>

<script>

 document.getElementById('message').innerHTML = ajax.responseText;

</script>

  • Upvote 1

Share this post


Link to post
Share on other sites

Hi there

 

Thanks for the trouble shooting steps recommended (.innerHTML etc) which highlighted that date.time zone was not set correctly.

 

Corrected the date.time zone via php.ini, it works fine only when ajax.responseText is in place. If we remove the trouble shooting statements (<div id=message_xml></div> ; <div id=message_text></div> ; the ajax.responseXML and ajax.responseText) and purely rely on the codes as written in the book (var data = ajax.responseXML; etc), we get no message at all.

 

Trouble shooting statements:

1. add the following in "add_employee.html"

<div id=message_xml></div>

<div id=message_text></div>

 

2. add the following in "add_employee.js"

document.getElementById('message_xml').innerHTML = ajax.responseXML;

 

document.getElementById('message_text').innerHTML = ajax.responseText;

 

 

Results :

 

[object XMLDocument] (ajax.responseXML)

last_name email phone_ext Please correct problems with the highlighted field(s) below. (ajax.responseText) . This is as expected as we key in the first name only.

 

 

 

Regards

Share this post


Link to post
Share on other sites

newbie3, I know English is not your first language, so it's okay but I am having some trouble understanding you.

 

I am assuming that your problem is with the responseXML Ajax property. Just to provide a basic explanation, there are two ways to return responses from Ajax requests: responseText and responseXML. Personally, I almost always use responseText, as it's easier to deal with just plain text.

 

When you use responseXML though, you have to realize what you're getting. You're essentially getting an XML document. If you don't know what XML is, I highly recommend you study up on XML a bit so that you know what responseXML is giving you in the first place.

 

As for the code in the book, I haven't looked at it for a while, nor do I recall exactly what Larry does, but I think the key to understand is that XML data cannot be treated like text data. XML data requires the use of special methods, etc. to manipulate it. The advantage of using responseXML is that you can use DOM methods and whatnot to navigate and edit it.

 

Anyway, the book specifically uses the responseXML property to show you how to use it. However, if you don't like it, you can achieve the same result by manipulating the text returned by the responseText property.

Share this post


Link to post
Share on other sites

Thank you HartleySan

 

The book uses responseXML property and show no message. I expect to see some messages as I add an employee.

 

I insert responseText property into the codes and I got messages back because of the responseText property.

 

The question I have is that how come the codes in the book (which uses responseXML) did not work for me? I made no changes to the codes in the book.

 

Regards

Share this post


Link to post
Share on other sites

It's been a while since I went through the code in the book, but I remember having the same problem with that section... until I found the error in my code. Using the responseXML property can get verbose, and it's very easy to make even the slightest error, which will stop EVERYTHING from working.

 

So, all I can suggest is, as frustrating as it is (I know), keep looking for the mistake you made in your code. I'm almost positive that the code in the book is okay (as I was able to finally get it working myself).

 

If you're having trouble understanding the responseXML property conceptually, you may want to check out other sites that explain it, and then try to create your own, simple example that shows how to use it.

 

Good luck.

Share this post


Link to post
Share on other sites

Thank You HartleySan, much appreciated.

 

Resolved.

 

Running "http://localhost/ajax/add_employee_xml.php" gave me a clue where the problem was. The problem was in "mysql.inc.php" which contains scripts to connect to MySQL. Never suspected that the problem could be in that script. The mistake I made was that I included basic HTML tags in the "mysql.inc.php" script. The file "mysql.inc.php" should contain only PHP scripts. Refer page 18 for more details.

 

These HTML tags in the "mysql.inc.php" work fine in all my other testings until this ResponseXML property comes into picture. As you said, slight error will stop EVERYTHING from working.

 

Thanks again.

  • Upvote 1

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...