V
V
Vasily Sakovich2015-11-17 19:54:25
PHP
Vasily Sakovich, 2015-11-17 19:54:25

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);}
    });});
});

and PHP script:
<?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();
?>

almost everything works)
why does the "error:" function work?
function "complete: function(data){alert(data.name);" returns the value "undefined".
People help!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
max0ua, 2015-11-17
@wasa20081980

Replace
with
yes and
should be replaced with
error_reporting(E_ALL);

S
Stalker_RED, 2015-11-17
@Stalker_RED

Open the console in the browser (F12), tab network (network).
Find POST php/contact.php and see what comes in response from the server.

R
ruboss, 2015-11-17
@ruboss

Remove "echo error_reporting(E_ALL);"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question