Jump to content

Strange problem with <form> using Japanese characters


eugeniu

Recommended Posts

I wrote a script that retrieves a word from a database made up of Japanese characters, adds two more Japanese characters to the script, and has the user enter the new word, in Japanese. For the purpose of debugging, I've simplified the script. The script is supposed to take the submitted word and check if it matches the word from the database, and return "Success" or "Fail". However, no matter how many times I enter the correct word, it determines it's incorrect. The code to the script is shown below:

 

<?php
session_start();

$title = "Test Quiz";
$autoselect = "quiz.answer";
include ("header.php");
echo "<h1>Test Quiz</h1><div id=\"quiz\">";

$entry = mysql_query("SELECT * FROM verbs LIMIT 1") or die(mysql_error());
$entry = mysql_fetch_array($entry);

$masu = "&#12414;&#12377;";

echo "Answer: $entry[ans_kana] + $masu<br />";
if (isset($_POST['form_submit'])) {
$answer = $_POST['answer'];
$rightanswer = $entry['ans_kana'];
$rightanswer .= $masu;
if ($answer == $rightanswer) {
	echo "Sucess.<br />$answer = $rightanswer<br />";
} else {
	echo "Fail.<br />$answer =/ $rightanswer<br />";
}
}
?>

<form name="test" action="test.php" method="post">
<input id="answer" type="text" name="answer" size="10" maxlength="60" />
<input type="hidden" name="form_submit" value="submitted" />
<input type="submit" name="submit" value=">>" />
</form>

<?php
echo "</div>";
include("footer.php");
?>

The above script can be run at http://kanaquest.com/dev/test.php . The right answer is supposed to be たべます, which is a combination of the string from the database, and $masu. But it seems that when a string from the database is combined with a UTF-8 string, nothing I enter matches the formed string. However, when $rightanswer is only a word from the database, or only a UTF-8 string set in the script, I have much better luck with the script matching up my submitted word with the word in the script. What am I doing wrong?

Link to comment
Share on other sites

What happens when you var_dump() both the input value and the value from the database?

 

Do they both report being the same length?

 

 

As a matter of fact, you're appending html character entities as apposed to regular characters. This is probably why they don't match.

 

Try converting the input with htmlentities().

Link to comment
Share on other sites

string(12) "たべます"
string(22) "たべます" 

Guess not.

 

I tried using htmlentities on the input string, then both strings, but I still get the same problem.

 

Answer: たべ + ます

string(12) "たべます"
string(22) "たべます"
htmlentities()...
string(12) "たべます"
string(22) "たべます"
Fail.
たべます =/ たべます

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.