PDA

View Full Version : AJAX: XMLHttpRequest and POST to PHP


CDarklock
06-15-2005, 10:30 AM
There's been some talk about AJAX here, so I thought this might be helpful to a few people.

Every single example of AJAX using XMLHttpRequest I have ever seen uses the GET method to retrieve the URI. (I see the occasional HEAD example, but it's essentially the same as GET.) However, there are three cases where it is generally agreed that you SHOULD send a POST request:

- When the request includes sensitive data
- When the request URI would exceed 512 bytes in length
- When the request will alter or create resources on the server

Since these situations occur frequently, any effective AJAX implementation must be able to send POST requests as well as GET requests. It seems straightforward from the documentation: use the POST method in the open() call, and pass the data in the send() call. Unfortunately, PHP does not populate the $_POST variable when you do that, because you need to set the appropriate MIME type on your request:


// assume xmlhttp is your XMLHttpRequest object
xmlhttp.open("POST",myURI,true);
xmlhttp.onreadystatechange=myCallback;
xmlhttp.setRequestHeader(
'Content-Type',
'application/x-www-form-urlencoded'
);
xmlhttp.send(myFormData);


If you don't set the right MIME type, $_POST contains nothing. Once you established this, you would probably Google the problem and crawl through lots of false positives to find a few people advising you to parse $HTTP_RAW_POST_DATA manually. This is really bad advice, so I figured I should post *good* advice somewhere.

Deb
06-15-2005, 08:04 PM
:yeah: Thanks! :yeah:

Always nice to see good info posted for future reference :)

CDarklock
06-16-2005, 03:30 PM
Always nice to see good info posted for future reference :)

I originally ended up here because I was trying to solve a hairy SSL problem related to IE's flawed version of HTTP 1.1, and FQ was the *only* place that actually had a set of usable httpd.conf entries to fix the problem. Nobody else had squat.

And that's what gets people like me to post in places like this. I mostly want to be where there are people who can help me get things done in a crunch, but as you build experience in this industry (I've been building web sites since 1991), it gets much harder to find people who are smart enough to help you at all -- let alone when you need a solution twenty minutes ago and your client is hemorrhaging five figures an hour.

Joseph
06-16-2005, 07:43 PM
Another reason to use POST is if you're trying to pass content that has line breaks or spaces/etc in it.

For example, I was recently trying to do a vB-Style text-edit and it would just randomly lose spaces or line breaks... I converted over to POST and it's working perfectly now. :smile:

So yea, it's really important to know how to use both GET & POST. :smile:

phppete
06-17-2005, 07:37 PM
I've already posted a working example of using POST weeks ago and every Ajax example I have seen uses POST.

CDarklock
06-20-2005, 02:59 PM
I've already posted a working example of using POST weeks ago and every Ajax example I have seen uses POST.

If you posted it weeks ago, why couldn't I find it when I needed it? Clearly, I was smart enough to figure it out on my own. Shouldn't it have been *easier* to find your example? I mean, if the example is harder to get than the solution, it sort of defeats the purpose.

phppete
06-20-2005, 03:06 PM
Is there something wrong with you? Use the bloody search feature and you *did not* work the problem out yourself you copied the code so please stop the nonsense. I've seen your type before, you like to play the 'big I am' the 'wannabe' personality.


The Wannabe

Motivation: craves respect for being competent and professional despite lacking in competence and professionalism
Mindset: deceptive
Malice: low to medium; when held accountable, medium to high

similar to the attention-seeker
is one of life's chronic underperformers and is best described as ineffectual in everything
craves undeserved respect and attention and will go to considerable lengths to acquire them
hangs around the fringes of a profession
not professionally qualified but claims they are a professional because they sit next to a professional or work alongside or near or in the midst of professionals, or provide services to professionals
lacks the ability, competence and professionalism to be a qualified professional
wants so much to be seen as competent professional person but is unable and unwilling to put in the work to achieve this
is unable and unwilling to apply knowledge gained from experience but instead devotes time and effort to improving skills of deception, manipulation, false claim, denial and projection
may have been rejected by their chosen profession for lack of competence
is spiteful towards and despises anyone who is qualified in the profession from which the bully has been excluded by virtue of lack of competence
is likely to be vilifying the profession they want to belong to or which they're claiming to be part of or which they are claiming to represent
displays a deep-seated envy and jealousy of the professionals that he or she works alongside or claims to serve
harbours a bitter resentment, grudge, distaste and contempt for the professionals that he or she works alongside or claims to serve
is likely to be criticising, condemning, disadvantaging and causing detriment to the professionals he or she works alongside or claims to serve
may seek positions of power over the professionals he or she works alongside or claims to serve, perhaps to facilitate a compulsion to criticise, condemn, disadvantage and cause detriment
is irresistibly drawn to organisations, roles and positions which offer the wannabe power and control over the professionals s/he despises (eg inspection regimes, approval roles, regulatory bodies, ticksheet compliance schemes, political correctness police, trade union official, etc) - and is often described as a talentless jobsworth
when in a position of power associates with and makes alliances with or surrounds him or herself with clones, drones, minions, fellow wannabes, sycophants and brown-nosers
instinctively objects to any suggestion of change, reform, improvement, progress or evolution, but has no viable or positive alternatives of their own
opposes every idea, suggestion, opinion, contribution or reform on principle but has no original, positive, constructive ideas or contributions of his or her own
is likely to plagiarise and steal others' ideas which are then put forward as their own
may place undue emphasis or reliance on an old, minor or irrelevant qualification to bolster their claim of belonging to or deserving to belong to a profession
may claim ambiguous or misleading or bogus or fraudulent qualifications, associations and experience
displays a superior sense of entitlement because they associate with or serve higher performers
emotionally immature
controlling
easily provoked
when challenged is adept at rewriting history to portray themselves as competent, professional and successful, regardless of multiple witnesses and overwhelming evidence to the contrary
quickly and loudly feigns victimhood when exposed and held accountable, often repeatedly and loudly accusing the person holding them accountable of being a bully
when held accountable makes conflicting and contradictory threats and demands (eg demands apology but orders the other person not to communicate with them)
when held accountable makes lots of loud but empty threats (eg of legal action such as libel, slander, defamation etc)
only carries out threats of legal action when in the presence of a superior serial bully, especially a sociopath type
may indulge their jealousy and envy of professionals or those they claim to serve by pursuing vindictive vendettas, sometimes with the help of a superior serial bully, especially a sociopath type
is easily manipulated and controlled by a superior serial bully
female wannabes may be arch bullies (some people might call them puppetmasters or queen bees)
may surround herself with drones of the opposite sex
may exploit some perceived vulnerability in self to ensure drone loyalty
gives the appearance of loyalty to drones but will discard them when they've served their purpose
is likely to have affairs to gain power, status or position

CDarklock
06-20-2005, 04:19 PM
Use the bloody search feature

Been there, done that. Try searching for "ajax php post", and show me where you say something about MIME types. Was that not a reasonable search term?

and you *did not* work the problem out yourself you copied the code so please stop the nonsense.

It's four method calls, three of which have to occur in the same order given. Do the math.

You do know *how* to do the math... right?

Deb
06-20-2005, 05:36 PM
Shouldn't it have been *easier* to find your example? I mean, if the example is harder to get than the solution, it sort of defeats the purpose. If the discussion surrounding the information is hostile it makes it harder for anyone to learn from the information, expand on the information, or refer others to the information.

Please keep the attacks off of the forums.... the more info on the forums (even if duplicated) the better as it allows those trying to accomplish the task multiple ways of finding ways to do it. The next goal is to avoid scaring them away as it just might be them that has the answer to one of your own questions later on.

Deb
- Don't make me separate you two :P

CDarklock
06-21-2005, 03:03 PM
Please keep the attacks off of the forums

And why exactly are you quoting *me* in that? I didn't accuse Pete of anything. I simply stated the facts: I could not find his example when I needed it. On what planet does that give him the right to accuse me of the single *worst* breach of ethics a developer can commit? And what sensible person would even *consider* denying me the right to defend myself against that accusation?

Sure, fights are distasteful, but there's a word for people who don't fight back: "victim".

phppete
06-21-2005, 03:21 PM
I simply stated the facts: I could not find his example when I needed it.

You didn't know the correct syntax for innerHTML either, you then attacked me and told me to grow up when I pointed out that this is very basic information. In my 'Ajax' thread I listed urls for further reference, but you pretended you plucked the info out of thin air rather than giving links to resources.

I think you have some serious issues to deal with, I would suggest you put your head in a food blender to see if that helps rearrange your DNA so you can then behave like a normal human instead of a retarded snail... apart from that I think you are a great guy :)

phppete
06-21-2005, 03:47 PM
To throw something a little more constructive into the thread here is a nice article on the back button issue with xmlhttp aka Ajax, it uses a nice iframe solution as a workaround http://www.contentwithstyle.co.uk/Articles/38/fixing-the-back-button-and-enabling-bookmarking-for-ajax-apps

CDarklock
06-21-2005, 07:21 PM
You didn't know the correct syntax for innerHTML either

Actually, I didn't think innerHTML was a consistent spelling. I illustrated this by pointing out that I misspelled it, it didn't work, and two other developers looked at it but couldn't see the problem.

you then attacked me and told me to grow up when I pointed out that this is very basic information.

Well, you said we clearly didn't have any experience with DHTML, which was a pretty childish thing to say. What I said in response was: "Grow up. This is pointless." Six words. Where's the attack?

But since that post was deleted by a moderator, you can *claim* it said whatever you want, and I'll bet most people would just take your word for it.

In my 'Ajax' thread I listed urls for further reference, but you pretended you plucked the info out of thin air rather than giving links to resources.

I compared the results of print_r($_SERVER) between a form submission and an AJAX request. Feel free to confirm for yourself that this does, indeed, pluck the info out of thin air.

Terra
06-21-2005, 08:00 PM
From here, it is best to take this conversation to private email...

Shame, because this could have become a really good source of information regarding AJAX...

>>Thread Closed<<

--
Terra
--I have a broom and it has my name on it--
FutureQuest