Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查javascript函数中的php变量_Javascript_Php_Jquery - Fatal编程技术网

检查javascript函数中的php变量

检查javascript函数中的php变量,javascript,php,jquery,Javascript,Php,Jquery,我已经下载了一个网页模板 在此模板中,有一个用于发送电子邮件的php脚本。在这个脚本中有一个validate()函数,其中有一个变量$return\u数组。它的一个值是$return_array['success'],可以是'0'或'1'。在另一个文件(javascript文件)中,有一个click()函数,该函数管理php脚本中的“success”值doing if(html.success=='1')…但它不能按预期工作,事实上它总是“0”…我需要检查什么 以下是html表单: <fo

我已经下载了一个网页模板

在此模板中,有一个用于发送电子邮件的php脚本。在这个脚本中有一个validate()函数,其中有一个变量$return\u数组。它的一个值是$return_array['success'],可以是'0'或'1'。在另一个文件(javascript文件)中,有一个click()函数,该函数管理php脚本中的“success”值doing if(html.success=='1')…但它不能按预期工作,事实上它总是“0”…我需要检查什么

以下是html表单:

<form method="POST" action="send_form_email.php" id="contactform">
            <div>
                <label for="name" style="color:white;">Inserisci il tuo nome</label>
                    <input type="text" class="input-field" id="name" name="name" value="">
            </div>
            <div>
                <label for="email" style="color:white;">Inserisci la tua e-mail</label>
                    <input type="text" class="input-field" id="email" name="email" value="">
            </div>
            <div>
                <label style="color:white;">Scrivi il tuo messaggio</label>
                    <textarea id="message" name="message" style="min-height: 160px;"></textarea>
            </div>                  
                <a id="button-send" href="#" title="Send Email" class="button" style="width:100%;">Invia E-Mail</a>
                <div id="success">Il tuo messaggio &egrave stato inviato correttamente!</div>
                <div id="error">Impossibile inviare il messaggio. Riprovare pi&ugrave tardi.</div>
        </form>
编辑:


我已经编辑了php部分,在结尾添加了json_编码和内容类型,但是当出现错误时,如名称丢失或邮件丢失,我希望看到输入表单附近出现一些内容,但它没有…

使用
返回json_编码($return_数组)
而不是
返回$return\u数组

Json encode返回键=>值对数组


在ajax调用中也使用
dataType:“json”
dataType:“jsonp”

首先解析成功返回的数据,然后对返回数组进行匹配
json\u编码
并在ajax成功html=$.parseJSON(html)中使用json;然后使用html['Success']而不是html.Success我需要在哪里添加html=$.parseJSON(html)?我已经在if(html['success'])之前添加了它,但是它给了我一个错误“html为null”。对不起,我需要在返回之前或返回地点使用json_编码?我需要在哪里使用数据类型?它不起作用…控制台记录“一个空字符串”…似乎jquery在php验证之前启动…有可能吗?然后可能会有1未设置,根据您的if-else条件,使用
console.log(html)
查看您得到的回报。如果您通过php获取0,它将显示相同的结果,如果php向您发送1,ajax将显示您1.console.log(html)将为我打印一个空值…如果我在数据类型中使用jsonp而不是json,ajax将出错而不成功
<?php 
// EDIT THE 2 LINES BELOW AS REQUIRED
$send_email_to = "mail.address@email.com";
$email_subject = "Feedback subject";
function send_email($name,$email,$email_message)
{
  global $send_email_to;
  global $email_subject;
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
  $headers .= "From: ".$email. "\r\n";
  $message = "<strong>Email = </strong>".$email."<br>";
  $message .= "<strong>Name = </strong>".$name."<br>";  
  $message .= "<strong>Message = </strong>".$email_message."<br>";
  @mail($send_email_to, $email_subject, $message,$headers);
  return true;
}

function validate($name,$email,$message)
{
  $return_array = array();
  $return_array['success'] = '1';
  $return_array['name_msg'] = '';
  $return_array['email_msg'] = '';
  $return_array['message_msg'] = '';
  if($email == '')
  {
    $return_array['success'] = '0';
    $return_array['email_msg'] = 'email is required';
  }
  else
  {
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email)) {
      $return_array['success'] = '0';
      $return_array['email_msg'] = 'enter valid email.';  
    }
  }
  if($name == '')
  {
    $return_array['success'] = '0';
    $return_array['name_msg'] = 'name is required';
  }
  else
  {
    $string_exp = "/^[A-Za-z .'-]+$/";
    if (!preg_match($string_exp, $name)) {
      $return_array['success'] = '0';
      $return_array['name_msg'] = 'enter valid name.';
    }
  }

  if($message == '')
  {
    $return_array['success'] = '0';
    $return_array['message_msg'] = 'message is required';
  }
  else
  {
    if (strlen($message) < 2) {
      $return_array['success'] = '0';
      $return_array['message_msg'] = 'enter valid message.';
    }
  }
  return $return_array;
}

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];


$return_array = validate($name,$email,$message);

if($return_array['success'] == '1')
{
    send_email($name,$email,$message);

}
header('Content-type: text/json');
echo json_encode($return_array);
die();
?>
$('#button-send').click(function(event){
    $('#button-send').html('Invio in corso...');
    event.preventDefault();

    //$('html, body').scrollTo( $('#contact'), 'fast' );
    $.ajax({
        type: 'POST',
        url: 'send_form_email.php',
        data: $('#contactform').serialize(),
        success: function(html) {
            if(html.success == '1')
            {
                $('#button-send').html('Invia E-Mail');
                $('#success').show();

            }
            else
            {
                $('#button-send').html('Invia E-Mail');
                $('#error').show();
                console.log(html);
            }                   
        },
        error: function(){
            $('#button-send').html('Invia E-Mail');
            $('#error').show();
            console.log("not html.success");
        }
    });

});