Invalid char like "&","?" using ajax - php

Ok guys, i've this:
<textarea class="boxCommento1" placeholder="Scrivi un commento.."></textarea>
<input style="width:100%;" type="button" value="Inserisci" onclick="functionThatObtainTextFromTextarea();"/>
When i click on the input button i get the text form the textarea, and i will insert that in my DB using PHP and AJAX, but there's a problem if i write something like this: "What did u do yesterday???" or char like this "&" when i'll get text using php, "?" and "&" won't be recognized of course, because using GET and POST "?" and "&" are used for php url variables... any advice???
PS: Sorry for my english.

Use encodeURIComponent() in the AJAX Javascript.
When you get the variable in PHP with $_GET or $_REQUEST, it'll automatically be decoded, so you don't need to do anything else.
//Javascript
var inserisciValue = encodeURIComponent(theInsertisciValue);

Related

Form action not working with special characters (&)

I have a reservation form and the form action should be:
https://reservations.posthotel.com/smsworld/wc.dll?smsworld~availbox~
But it's not working. When I submit the form, everything from ? is just ignored.
As a solution, I named the first field as
name="smsworld~availbox~&RAD"
It's ALMOST working, the only problem now is that when I submit my form, the "&" is being switched by %26.
The URL that I should get is:
https://reservations.posthotel.com/smsworld/wc.dll?smsworld~availbox~&RAD=10%2F15%2F2014&RDD=10%2F21%2F2014&nights=3&RCA=2
But instead, I'm getting:
https://reservations.posthotel.com/smsworld/wc.dll?smsworld~availbox~%26RAD=10%2F15%2F2014&RDD=10%2F21%2F2014&nights=6&RCA=2
Any suggestions?
Are you using GET or POST (the method attribute on the form) ? If GET, the URL in the action cannot contain query-string parameters. Try using POST instead.
I just made a fiddle for you which has two forms. One using GET (does not work) and the other using POST (Seems to work):
<form action="https://reservations.posthotel.com/smsworld/wc.dll?smsworld~availbox~" method="get">
<input type="submit" value="Submit GET" />
</form>
<form action="https://reservations.posthotel.com/smsworld/wc.dll?smsworld~availbox~" method="post">
<input type="submit" value="Submit POST" />
</form>
http://jsfiddle.net/n80e6Lzv/
What you're observing is URL encoding. Certain characters in URL's are reserved characters, such that you cannot make parts of the URL that contain that character, or else the URL will get misinterpreted. Ampersand is one of those characters.
Consider what a URL with a query string looks like:
http://test.com?name1=value1&name2=value2
As you're probably already aware, the query string variables are separated by an ampersand. URL encoding substitutes a group of characters for a reserved character, to prevent misinterpretations of the URL.
In other words, the URL you're asking for is impossible. Your GET data would be interpreted as:
$_GET = array(
'smsworld~availbox~' => '',
'RAD' => '10/15/2014', //An additional index created by the unencoded ampersand
//etc...
);
If that extra RAD index was your actual intent, that is not possible this way. You should be making use of hidden fields to add RAD as an additional query string variable instead. One form field for each query string variable only.
thanks for all the replies, but I've found a solution for this using a few lines of javascript.
$('.submit-btn').on('click', function(e) {
var checkinDate = $('#checkAvailDate').val(),
guests = parseInt($'#checkAvailAdults').val()) + parseInt($('#checkAvailChildren').val());
var formAction = 'https://reservations.posthotel.com/smsworld/wc.dll?smsworld~availbox~&RAD=10%2F15%2F2014&nights=6&RCA=' + guests;
var win = window.open(formAction, '_blank');
win.focus();
e.preventDefault();
});
I'm not done with formatting all the fields yet, but even then, thank you very much for all the help :)

$_GET variable in php without using a form

i have gallery.php?gid=1 in gallery.php page and i have to submit data to save.php without using a form.
and i want to get the value of gid without using a button..
is that possible?
gallery.php
$gid=$_GET['gid'];
save.php
echo $gid..
header("location:save.php?gid=".$gid);
OR
use a anchor tag with href="save.php?gid=<?php echo $gid;?>"
$_GET will contain all the variables which are in the URL, after the "?" (foo.php?gid=...).
You are not obliged to use a form to fill these variables.
The members of $_GET array is got from query string. In a simple way, a query string is tha part after the ? character of the URL and formatted in this way:
param1=value1&param2=value2&....
So you can pass any pair of key-value in to your PHP script by adding a query string to the end of this PHP file URL.

How to fix these type

I have html data like text including image src.when I alert using jquery it works fine. Entire html is displayed in alert box.And then I want to send that entire html data and text and something's id using query string of JQuery like:
$.post('something.php','socialprofileid='+socialprofileid+
'&txtMsg='+msg+'&postedHtmlMsg='+HtmlMsgresult)
But I need that entire html data including text and id in php page like get html data, text, id in JQuery alert box
I try to use window.btoa function(base64 encoded format) for HtmlMsgresult. After doing base64 encoded format in javascript, when I try to send in php page, it doesn't get any print in php page.
Another thing is there any solution Text and htmldata(html text and img src) are combined and then it is done the base64 encode.And then it is send in php page using query string like:
if(txtmsg='') {
var result=window.btoa(htmldata);
} else {
var content=text+htmldata;
var result=window.btoa(content);
}
$.post('something.php','socialprofileid='+socialprofileid+'&result='+result)
Is it possible to do that?
And then I want to insert into database in something.php and then in another page I want to fetch the htmldata and text which are encoded by base64 using jquery. Then it is converted by base64_decode function.
But within text and htmldata, How to extract text , htmltext, htmlimgsrc differently from the database table.
If u know about these type of problems, please reply me
I need your hand
Thank you
$.post('something.php',
{socialprofileid:socialprofileid,
txtMsg:msg,
postedHtmlMsg:HtmlMsgresult});
jQuery will apply encodeURIComponent by itself. And yes, you need encodeURIComponent applied to the values of the parameters if you want to encode the data by yourself. Read the following topic When are you supposed to use escape instead of encodeURI / encodeURIComponent?
ps: you do not have to do anything in the php script, the data will be decoded automatically by the server. Access $_POST['socialprofileid'], $_POST['txtMsg'] and so on in php.
In php, you can get the post parameters with the $_POST variable.
<?
echo $_POST['socialprofileid'], PHP_EOL;
echo $_POST['txtMsg'], PHP_EOL;
echo $_POST['postedHtmlMsg'], PHP_EOL;
?>

How do I escape '+' in data I’m sending via AJAX in Mootools?

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.
Try using the function encodeURIComponent over your text value. It, well.. uri encodes your text.
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">
Your text most likely either need to be URLEncoded.

php htmlspecialchars() from one form to another

Why can't htmlspecialchars() continually encode characters after each form submission? Take a look at the following example:
<?php $_POST['txt'] = htmlspecialchars($_POST['txt']); ?>
<form method="post">
<input name="txt" value="<?=$_POST['txt'] ?>" />
<input type="submit" name="save" value="test" />
</form>
You can see it at running at http://verticalcms.com/htmlspecialchars.php.
Now do the following
1) Type & into the text field
2) Hit the test button once
3) When the page completes post back, hit the test button again
4) When the page completes post back, view the page source code
In the input box, the value is & amp;
I was expecting & amp; amp;
Why is it not & amp; amp; ???
This simply is HTML entity encoding. When using "&" in an HTML attribute, it should be encoded. And this is what you are doing.
So, the browser reads
<input value="&" />
and translates it to an "textbox widget with value '&'".
The same would be true for other special chars:
<input value="&quot" />
would result in a " character.
When you submit the form, the browser sends these values unencoded, therefore your PHP script receives it like "&", not "&".
The values in $_POST are already html-decoded for convenience. So when your script starts, the following is true:
$_POST['txt'] == '&';
htmlspecialchars('&') == '&'
[edit]
Looks like this needs further explanation
When a form like the one above is submitted to the server by the browser with a single ampersand as the value of 'txt', it puts the following into the body of the request:
txt=&
The value is encoded because the browser would concatenate multiple fields with an ampersand character like
txt=&&user=soulmerge&pass=whatever
PHP takes the transmitted values and decodes them for the convenience of the programmer - it makes an ampersand out of & Now I though this was the reason for the question in the first place - guess I got it wrong. The actual question was answered correctly by Ferdinand.

Categories