views:

49

answers:

3

I was building a simple web based calculator which takes equations from a HTML form, evaluates it on the server using PHP and sends the result back.

I am using Mootools to send the data via the req.send AJAX operation.

But, each time I have a '+' in an equation, it is not seen on the POST data the server gets.

Any ideas why this is happening and how I can work around it?

eg:

10 + 12 in HTML form is seen as 10 12 in the $_POST data.

The Mootools send command I am using is something like this with

<textarea name="equationTextArea">10+12</textarea>

req.send("eqn="+$('equationTextArea').value);

Upon submit, I see $_REQUEST['eqn'] as 10 12.

+3  A: 

Try using the function encodeURIComponent over your text value. It, well.. uri encodes your text.

CharlesLeaf
Why do we have to URI encode POST data?
arbithero
I have seen other websites take equations in POST without any uri encoding
arbithero
We do this for certain characters, like + which is a "space" according to RFC 1738. If other websites don't appear to throw uri encoding over their values, it's likely a framework solves this problem for them. In this/your case it does not seem to happen like that.
CharlesLeaf
bobince
I know, I said "like +", meant as 'for example' ;) But good to point it out for people who are unfamiliar with it!
CharlesLeaf
That solved the problem! Thanks!
arbithero
Your welcome! (can you mark this answer as solved/answered? for future visitors of the question)
CharlesLeaf
Done. Thanks CharlesLeaf.
arbithero
A: 

Your text most likely either need to be URLEncoded.

m.edmondson
Don't let the php tag fool you, the problem in this question is client-side. Despite that, in PHP I personally prefer `rawurlencoding` as it is RFC 1738 compliant.
CharlesLeaf
+1  A: 

Set your form's encoding to multipart/form-data - this is an alternative to the default application/x-www-form-urlencoded and doesn't encode a space into a plus sign +.

Example from the w3.org reference:

<form action="http://example.com/cgi/handle"
   enctype="multipart/form-data"
   method="post">
Piskvor
This is doing an XMLHttpRequest. If you wanted to send `multipart/form-data` you'd have to create that request body manually, which is more effort than for `application/x-www-form-urlencoded`.
bobince