Answer the question
In order to leave comments, you need to log in
Why is the variable not being passed from PHP to JavaScript?
Good day. There is a form handler like this:
$(document).ready(function(){
"use strict";
$("#butF").click(function(){
var name=$("#name").val();
var mail=$("#e-mail").val();
var txt=$("#txt").val();
var em=$("#em").val();
if(name===""){
alert("ВВЕДИТЕ ИМЯ");
return false;}
if(txt===""){
alert("ВВЕДИТЕ СООБЩЕНИЕ");
return false;}
if(em!=="[email protected] (не изменяйте это поле)"){
alert("ОШИБКА ВВОДА");
$("#em").val("[email protected] (не изменяйте это поле)");
return false;}
if(mail.indexOf('@')<0){
alert("ВВЕДИТЕ EMAIL");
return false;}
});
$('#contact').submit(function (e) {
e.preventDefault();
var data=$("#contact").serializeArray();
$.ajax({
type:'post',
url: "php/contact.php",
data: data,
dataType:'json',
succes:function(data){alert(data.name);
$("#name,#e-mail,#txt").val("");},
error:function(){alert("ОШИБКА, ПОПРОБУЙТЕ ЕЩЕ РАЗ");},
complete: function(data){alert(data.name);}
});});
});
<?php
header ("Content-Type: text/html; charset=utf-8");
echo error_reporting(E_ALL);
$name=strip_tags($_POST["name"]);
$mail=strip_tags($_POST["e-mail"]);
$txt=strip_tags($_POST["txt"]);
$mes="$name.\n$mail.\n$txt";
$mes= wordwrap($mes, 70, "\n");
$headers= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "From: Тестовое письмо <[email protected]>\r\n";
$ans='ошибка сервера, поробуйте еще раз';
if(mail('[email protected]',$mail,$mes,$headers))
{$ans='сообщение отправлено';}
echo json_encode(array(
'name' => $ans
));
exit();
?>
Answer the question
In order to leave comments, you need to log in
Replace
with
yes and
should be replaced witherror_reporting(E_ALL);
Open the console in the browser (F12), tab network (network).
Find POST php/contact.php and see what comes in response from the server.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question