E
E
Egor Chebotarev2016-04-28 19:06:43
PHP
Egor Chebotarev, 2016-04-28 19:06:43

I don’t understand where I’m losing JSON when transferring to the server, let’s look together?

I send the string to the server:

var msg = {},
                str;
            msg.name = name.value;
            msg.mail = mail.value;
            msg.phone = phone.value;
            msg.select = select;
            msg.comment = comment;

            str = JSON.stringify(msg);

            //msg = serialize(document.querySelector('form'));

            var xhr = new XMLHttpRequest();

            // 2. Конфигурируем его: POST-запрос на URL 'mail.php'
            xhr.open('POST', 'mail.php', true);

            xhr.addEventListener('load', function() {
                if (xhr.status != 200) {
                    // обработать ошибку
                    alert( xhr.status + ': ' + xhr.statusText ); // пример вывода: 404: Not Found
                } else {
                    document.querySelector('.form__content').style.display = 'none';
                    document.querySelector('.form-successful').style.display = 'block';
                }
            });

            xhr.send(str);
            console.log(str);

I receive, decode and send to the mail:
$obj = json_decode($_POST, true);

    $to = "[email protected]";
    $subject = "Заявка";
    $from = "[email protected]";
    $headers = "From: $from";
    mail($to,$subject,$obj[name],$headers);
    echo "Mail Sent.";

Any idea why there is nothing in the letter except for the header?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2016-04-28
@Egche

instead of $_POST use:

offtopic picture
0_c03f9_ada4c32a_orig.jpg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question