PHP jQuery encoding glitch

Hello everyone!

The essence of the problem is as follows. There is a script that sends the previously collected form data to a php script using the jQuery.post()method. Here is the js script:

jQuery.post(
url = '/wp-content/themes/panorama/query.php', data = dataForm, function (data) {
    alert(data);
});

And here is the same php script (in my case, query.php). query.php – simply sends an email with the form data passed to jQuery.post(). Here is the query.php

code

if (isset($_POST['name'])) {
    $name = $_POST['name'];
} else {
    echo "error name";
}
if (isset($_POST['email'])) {
    $email = $_POST['email'];
} else {
    echo "error email";
}
if (isset($_POST['subject'])) {
    $subject = $_POST['subject'];
} else {
    echo "error subject";
}
if (isset($_POST['message'])) {
    $message = $_POST['message'];
} else {
    echo "error message";
}
$to         = "[email protected]";
$message    = $message . "<br/> : " . $name . " - " . $email . "";
$resultSend = mail($to, $subject, $message);
$success = "   !";
$defeat  = "  ";
$success = iconv('windows-1251', 'UTF-8', $success);
$defeat  = iconv('windows-1251', 'UTF-8', $defeat);
if ($resultSend == TRUE) {
    echo $success;
} else {
    echo $defeat;
}

Everything works, the letter is sent, etc., but the response about the php script comes in an incomprehensible encoding, and the alert(data);function gives krakozyabry. I don’t understand why iconv()doesn’t help.

Advise what to do? Thanks in advance!


Answer 1, authority 100%

What is the encoding of the php file itself?

This can be replaced

$resultSend = mail($to, $subject, $message);
$success = "   !";
$defeat  = "  ";
$success = iconv('windows-1251', 'UTF-8', $success);
$defeat  = iconv('windows-1251', 'UTF-8', $defeat);
if ($resultSend == TRUE) {
    echo $success;
} else {
    echo $defeat;
}

to

if (mail($to, $subject, $message))
    echo iconv('windows-1251', 'UTF-8', "   !");
else
    echo iconv('windows-1251', 'UTF-8', "  ");

It will be shorter.

And the fact that crooked letters are an encoding. Most likely, it is different everywhere.

The page may not be UTF-8, but windows-1251, then iconv will return nonsense, as well as on the server side, if the file is in utf-8, and you iconv(‘windows-1251’, ‘UTF-8’would also be nonsense.

The only solution is to do the encoding correctly everywhere!


Answer 2

Use the PHP function iconv()or use functions from the more or less known MB_STRING extension. mb_internal_encoding(), for example.

By the way, here’s this snippet of code:

  if(isset($_POST['name'])){$name = $_POST['name'];} else {echo "error name";}
   if(isset($_POST['email'])){$email = $_POST['email'];} else {echo "error email";}
   if(isset($_POST['subject'])){$subject = $_POST['subject'];} else {echo "error  
   subject";}
   if(isset($_POST['message'])){$message = $_POST['message'];} else {echo "error 
   message";}

just awful! It hurts to look at him! There is absolutely no filtering of the entered data here. And more or less smart users will understand what’s what and take advantage of this hole…

Use at least strip_tagshere)

Leave a reply

Please enter your comment!
Please enter your name here