1

I have a form and I'm using a PHP file to write its content to a text file. My problem is that whenever I try to write special characters (such as è é à á ä etc...) they are replaced by other strange characters (such as è é Ã).

I have looked around but none of the solutions suggested on different forums solved my issue.

Here you can find the HTML form I'm talking about: http://beginninghtml.altervista.org/InfinitySMS_Web/index.html

The PHP file processing my html form is http://beginninghtml.altervista.org/InfinitySMS_Web/process-form-data.php:

$phonenum = $_POST['phonenum'];
$messagetext = $_POST['messagetext'];
$types=$_POST['types'];
$message=stripslashes($messagetext);
//if no message entered and no mobile num entered print an error 
if (empty($phonenum) && empty($messagetext)){
print "Non sono stati inseriti il testo del messaggio e il numero del destinatario.<br>Inseriscili per poter inviare il tuo messaggio.";
}
//if no message entered send print an error 
elseif (empty($messagetext)){
print "Non è stato inserito alcun testo per il messaggio.<br>Inseriscilo per poter inviare il tuo messaggio.";
}
//if no mobile num entered send print an error 
elseif (empty($phonenum)){
print "Non è stato inserito il numero del destinatario.<br>Inseriscilo per poter inviare il tuo messaggio.";
}
elseif (strlen($phonenum)<11){
    if (strlen($phonenum)>9){
        //if the form has both a phone number and a text message. 
        //write data to the file
        $name = $_POST['phonenum'];
        $email = $_POST['messagetext'];
        $fp = fopen("formdata.txt", "a");
        $savestring = $name . "!$@$!;" . $email . "!$@$!;\n";
        fwrite($fp, $savestring);
        fclose($fp);
        header("location:success.html");
    }
    else
    {
        print "Il numero di cellulare inserito non è corretto: il numero del destinatario deve contenere 10 cifre.";
    }
}

And here you are the URL to the txt file containing the so called "strange" characters: http://beginninghtml.altervista.org/InfinitySMS_Web/formdata.txt

rambodrahmani
  • 99
  • 4
  • 12

1 Answers1

2

Think you missed setting UTF-8 encoding in your header()

Add header('Content-Type: text/html; charset=utf-8'); and all should be OK.

Also, be sure what you formdata.txt file also have utf-8 encoded

  • If manually set encoding in browser for you page: http://beginninghtml.altervista.org/InfinitySMS_Web/formdata.txt when it start to show normal symbols. So, most likely you have non-unicode encoding setted for formdata.txt – Павел Осипов Mar 28 '13 at 14:52
  • I added the code you suggested and (using TextWranger) made sure that the txt file format was UTF-8, but I obtained the same result: my characters still gets replaced by other characters. – rambodrahmani Mar 28 '13 at 15:02
  • Yes you right Man! I just set the encoding manually and my characters appeared. How can I make it permanent? – rambodrahmani Mar 28 '13 at 15:03
  • Ok man now it's fine. – rambodrahmani Mar 28 '13 at 15:11