Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post special/reserved characters from HTML forms to PHP pages?

I have a form that looks like this:

<form action="/assesment/savelist/" method="post">
    <input type="hidden" name="owner" value="<?php echo $userid ?>" />
    <input type="text" name="title" value="Question List Title" />
    <textarea name="description"></textarea>
    <input type="submit" />
</form>

In the description people will have to be able to use the £ character (among other non-allowed characters).

Is there anyway to convert these characters to something that is allowed before posting them to my PHP page?


Hi All, thanks for your comments so far.

If I do print_r($_POST) on my "savequestion" it outposts the postdata that gets sent to it from that form.

however, if there is a £ in any of the fields then that specific character doesnt get sent. For example if I was to post "sdfsdfs £ adasd" from that form all that would get sent is "sdfsdfs adasd"

the question is how do I convert the £ to something that I can send as post data from a HTML form.

like image 655
Chris Headleand Avatar asked Aug 09 '12 15:08

Chris Headleand


People also ask

What is HTML special characters PHP?

htmlspecialchars() Function: The htmlspecialchars() function is an inbuilt function in PHP which is used to convert all predefined characters to HTML entities. Syntax: string htmlspecialchars( $string, $flags, $encoding, $double_encode ) Parameter value: $string: This parameter is used to hold the input string.

What is encoding and escaping in PHP?

Encoding is transforming data from one format into another format. Escaping is a subset of encoding, where not all characters need to be encoded. Only some characters are encoded (by using an escape character).


2 Answers

WIN!

The solution is to add accept-charset="utf-8" to the form tag.

I didnt have the option to add this to the header of the page but adding it to the form tag solved all my issues. Big shout out to @deceze for posting a link to this website http://kunststube.net/frontback/

like image 135
Chris Headleand Avatar answered Nov 15 '22 08:11

Chris Headleand


Browsers will automatically encode data when it is submitted via the standard form submit mechanism.

PHP will automatically decode data when it populates $_POST/GET/REQUEST.

You don't need to do anything at that stage.

You might need to encode the data before inserting it into a database / some HTML / an email / a URI / some other data format, but that would depend on what you are doing with the data.

like image 34
Quentin Avatar answered Nov 15 '22 07:11

Quentin