Jump to content

Accents in php forms


narcis

Recommended Posts

I have a very simple php form. The users that fill the form in some countries like France use accents like à or letters like ç. How should be the code to insert and to select in these cases?

 

// INSERT

if (isset($_POST['text_post'])) {

$sql="INSERT INTO blog (titol_post, text_post)

VALUES ('$_POST[titol_post]','$_POST[text_post]')";

if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }

 

// SELECT

$result = mysql_query("SELECT * FROM blog");

while($row = mysql_fetch_array($result))

{

echo $row['titol_post'] . " " . $row['text_post'];

echo "<br />";

}

 

}

else { // FILL THE FORM

echo "

<form action='4.php' method='post'>

Títol: <br/> <input type='text' name='titol_post'/><br/><br/>

Text: <br/> <textarea name='text_post' rows='15' cols='80'></textarea><br/><br/>

<input type='submit' />

</form>

";

}

 

I tried things like: echo htmlentities($row['titol_post']) but it doen't seem to work

Link to comment
Share on other sites

What error are you getting?

 

... Always tell us that.

 

;)

 

Make sure your .php page is set to UTF8:

 

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

 

Or using PHP:

 

header('Content-Type: text/html; charset=utf-8');

 

Make sure this PHP function call is the first thing at the very top of the page or it will not work - headers must be sent first to the web browser.

 

Stefan

Link to comment
Share on other sites

If I fill the form (with the code above and without any special thing like htmlentities) it seems to work fine. For example I fill à and the user can see à. But in the database is stored an estrange character: Ã The wright html character should be à

 

If I write à directly in the database à it does not show for the user.

 

If I write à in the database it shows à for the user.

 

Even when the special characters seem to work well for the user, they are stored in a strange way in the database and could give me problems later.

 

I suppose the wright thing would be that the user could write à, in the database could be stored à and the select should show à for the user. But I don't know how to do that.

 

I attach the example

 

My .php is utf8. The database collation is the default latin1_swedish_ci I tried to change the field titol_post to utf8_general_ci and it makes no change.

 

Thank you

 

Narcís

4.php

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...