Jump to content



Photo

Chapter 5, Add_Employee, Ajax Status 200 & 304 Failed


  • Please log in to reply
11 replies to this topic

#1 newbie3

newbie3

    Newbie

  • Members
  • Pip
  • 16 posts

Posted 28 January 2012 - 6:13 PM

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
  • 0

#2 newbie3

newbie3

    Newbie

  • Members
  • Pip
  • 16 posts

Posted 28 January 2012 - 8:28 PM

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
  • 0

#3 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,946 posts
  • LocationColumbus, OH USA

Posted 29 January 2012 - 7:53 AM

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>

  • 1

#4 newbie3

newbie3

    Newbie

  • Members
  • Pip
  • 16 posts

Posted 30 January 2012 - 5:27 AM

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
  • 0

#5 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,915 posts
  • LocationState College, PA (USA)

Posted 30 January 2012 - 10:05 PM

So, it's not clear to me: is everything okay now or not okay?
  • 0

#6 newbie3

newbie3

    Newbie

  • Members
  • Pip
  • 16 posts

Posted 31 January 2012 - 3:30 AM

Hi Larry

The problem has not been resolved yet. When we add an employee, there is no message return.

Regards
  • 0

#7 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,946 posts
  • LocationColumbus, OH USA

Posted 31 January 2012 - 3:43 AM

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

#8 newbie3

newbie3

    Newbie

  • Members
  • Pip
  • 16 posts

Posted 31 January 2012 - 7:08 PM

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
  • 0

#9 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,946 posts
  • LocationColumbus, OH USA

Posted 1 February 2012 - 1:49 AM

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

#10 newbie3

newbie3

    Newbie

  • Members
  • Pip
  • 16 posts

Posted 3 February 2012 - 4:13 AM

Thank You HartleySan, much appreciated.

Resolved.

Running "http://localhost/aja...ployee_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.
  • 1

#11 HartleySan

HartleySan

    Advanced Member

  • Members
  • PipPipPip
  • 2,946 posts
  • LocationColumbus, OH USA

Posted 3 February 2012 - 8:44 AM

Would have never guessed that that was the problem, but good job solving it.
  • 0

#12 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,915 posts
  • LocationState College, PA (USA)

Posted 3 February 2012 - 8:58 AM

Kudos for figuring that out and thanks for sharing the solution.
  • 0