Charset change after POST Request - php

I'm using a meta tag on my page for setting the encoding for the page:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Everything works fine if it's a GET Request, but when I submit a form (like a search form) and do a POST Request, all of the unicode characters convert to weird letters.
Why this is happening?

I had the same problem and solution i very simple. Put <body> </body> and this will solve the problem.

Related

How to prevent non-ascii character turn weird from html form input

When user submit his name as
Thorsten Löhrlein
I am getting this
Thorsten Lï¿œhrlein
It is not happen on my computer thought when I submitting my name as Löhrlein, but is there any technique to prevent the character turn weird in all case ?
Try updating this and check it in your HTML page:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Simple HTML form and PHP throwing error

I am just calling a simple PHP script through a HTML form.
An error is thrown everytime : "The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol"
I have defined the encoding both in PHP as well as HTML as UTF-8 (please refer to code below). I am unable to solve this problem despite searching all over the web.
HTML code :
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<meta content="UTF-8" http-equiv="encoding"/>
<title>Test</title>
</head>
<body>
<div align="center">
<form action= "google1.php" method="get" accept-charset="UTF-8" >
Enter Your Name:
<INPUT TYPE = "text" NAME = "student"> <BR>
<!--input name="q" type="text"-->
<br/ >
<input name="btnG" type="submit" value ="test">
</form>
</div>
</body>
PHP Code
header("Content-type: text/html; charset=UTF-8");
print "<pre>";
print_r($_GET);
print "</pre>";
The result after submitting the button (along with the error) is :
"; print_r($_GET); print ""; ?>
I am using XAMPP. I tried to edit .htaccess (added : AddType 'text/html; charset=UTF-8' html) as suggested in some of the solutions over internet but that also did not help.
I found a site where there is a simple form which calls a PHP script again. http://www.tjhsst.edu/~dhyatt/superap/form1.html . When I try to submit value in the form, I get the same error.
So I thought, this could be a browser problem and I changed the default encoding of my browser to UTF-8. But this also did not help.
I am a novice in web programming and trying to learn. Appreciate if any one can help.
Thanks,
Ashutosh
It looks like you have some issue with opening/closing tags.
Ensure that you have php code wrapped with <?php and ?> in your process1.php3 (Some details: http://php.net/manual/en/language.basic-syntax.phpmode.php)
Like here:
<?php
header("Content-type: text/html; charset=UTF-8");
print "<pre>";
print_r($_GET);
print "</pre>";
?>
UPD:
After a long session of question/answer finally appeared that OP were opening file with a form using file:// protocol. Like file:///C:/xampp/htdocs/example/form.html and form were submitted to file:///C:/xampp/htdocs/example/google1.php?... As apache works with HTTP protocol only, PHP were not executed actually.
Your Code:
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
Correct code:
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
You have forget to put slash at the end of meta-tag. You have not closed the meta tag.
Though, its not very crucial, as you have tried everything. Try this one too. It might work for you.

PHP Japanese echo string becomes question marks

I'm working on a php site that needs both Japanese and English. In most places I have no trouble getting the Japanese to display correctly, but I can't get the characters to display if I use a function like this:
echo.php:
<?php
function draw(){
echo "日本語";
}
draw();
?>
I get "日本語"
but if I try this :
index.php:
<?php
some stuff
include "echo.php";
draw();
?>
I get "???".
Is there any way for me to get my echo file to send the data to the first file in a way it can read it?
EDIT:
The website works, and shows Japanese characters correctly, except when it tries to pull a function from a php file
Here's the code:
<html lang="ja">
<head>
<title>Running Projects</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<div id="header">
<? php include "layout.php" ?>
</div>
</body>
</html>
Where layout.php is just a file with a list of links, I have this on each page so the links are on every page.
For a different part, I have to use functions.php to get some data from a database and write it to the page, so, I tried putting layout.php into the functions.php and calling it: The English links appeared, but the Japanese links appeared as question marks.
You should change your file encoding to UTF-8 and set the header on the website to UTF-8.
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
in HTML5 you should use:
<meta charset="utf-8" />
or in php
header('content-type: text/html; charset=utf-8');
You problem definitely has something to do with the character encoding. Try to check the following:
All your strings in all PHP scripts are Unicode (UTF-8 is a very common choice). Use utf8_encode() and/or utf8_decode() to force UTF-8 on your strings where necessary.
Your server sends PHP output as Unicode (UTF-8 and preferably, but not necessarily, gzipped data)
Your browser understands and accepts Unicode (UTF-8). Typically browser would send Accept-Charset: UTF-8,*;q=0.5 in the GET request to hint it's Unicode capability.
Finally, I have checked your code with PHP version 5.3.6 and Firefox, Chrome and IE 9. Firefox and Chrome prints the Japanese characters as expected. It's only IE 9 which doesn't print it correctly. Snooping on the GET request from IE reveals, it is indeed not sending any Accept-Charset: UTF-8,*;q=0.5. I am not entirely sure how to force IE to send that because in my case, clearly my server (Apache) together with PHP was definitely sending UTF-8 string back.
One more hint - you may try converting your strings into HTML entities. For example - echo "©"; would print ©. But, I am not 100% sure how to convert all strings into HTML entities using PHP. I have unsuccessfully attempted with htmlentities() and htmlspecialchars(). But, it didn't change anything for IE.
i have same problem. But i handle it.
convert "echo.php"'s encoding to EUC-JP or SHIFT-JIS.
<?php
function draw(){
echo mb_convert_encoding("日本語", 'UTF-8', array('EUC-JP', 'SHIFT-JIS', 'AUTO'));
}
draw();
?>
This works for me :)
That happens when charset is not defined or is incorrect.
You can use meta tags to define the charsets. Place the following meta tags as needed on the head section of the page, and It will be rendered correctly.
HTML 4
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
HTML 5
<meta charset="utf-8" />
add below meta tags to header
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Language" content="ja" />
MySQL and PHP need to be handling the same character set.
Try adding ..
mysqli_set_charset("utf8");
.. after connecting to your database. Always works for me!
If you are using php and mssql you will also need to specify characterset in the mssql connection string.
Like this:
$connectionInfo = array("Database"=>"db name", "UID"=>"username", "PWD"=>"pw","CharacterSet" => "UTF-8");
$serverName = "server";
$conn = sqlsrv_connect($serverName, $connectionInfo);
Please note you still need to do the things suggested above
declare your html charset like this HTML 5:
<meta charset="utf-8" />
php charset like this:
header('content-type: text/html; charset=utf-8');

Preserve my character encoding

i bought the domain xn--79n.ws which resolves to ∠.ws. (Still counts as one character on twitter)
I built a custom image upload service and i am trying to echo back the link. Instead of echoing back http://xn--79n.ws/image.jpg, i want to echo back http://∠.ws/image.jpg.
Help please?
All you need to do us add a proper Header to your HTML output and it would work
HTML > HEAD
<meta charset="UTF-8" />
Or
<meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'>
Thanks
:)

PHP - Escaping em dashes

I am $_POST'ing the following headline from a form:
Google’s New Partner Android Update Initiative: Very Promising — Maybe; We’ll See
And on the handler page, if the first thing I do is
echo "<pre>";
print_r($_POST);
die();
I see:
Google’s New Partner Android Update Initiative: Very Promising — Maybe; We’ll See
I understand that there are functions to convert & escape characters and their HTML equivalents, but how can I ensure that this content is added into the $_POST in the correct encoding?
Cheers,
not sure if that helps, but it seems like the UTF-8 encoding got mixed up (control characters seem somewhat familiar to me ...). Try to output with utf8_encode() or utf8_decode().
Both the page with the form, and the displaying page need to use the same charset. To reproduce the behavior you show, I had to create 2 pages, one a form, with
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
which posted to a page without that.
If I put the meta tag in both pages, it outputs correctly, if I remove it from both pages, it outputs correctly.
If only the form has it, you get what you posted, and if only the receiving page has it, you get the ?'s.
test.php
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<form method="post" action="test2.php">
<input type="text" name="string">
<input type="submit">
</form>
test2.php
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<?php
if(isset($_POST['string']))
{
echo "<pre>";
print_r($_POST['string']);
die();
}
?>
If I paste your string into the input box in test.php, hit submit, I get it back properly in test2.php. If I remove the first line of test2.php, I get the behavior you describe.

Categories