简体   繁体   English

无法在PHP中使用AJAX存储变量的值

[英]Unable to store value of a variable using AJAX in PHP

This is my html Form. 这是我的html表格。 From here I am passing 3 values to my ajax 从这里我将3个值传递给我的ajax

<div class="form-container">
     <h3>Drop me a line</h3>
     <p id="response"></p>
     <form role="form" id="form">
<div class="form-group">
     <input type="text" class="form-control" id="name" name="name" placeholder="Name">
</div>
<div class="form-group">
    <input type="email" class="form-control" id="email" name="email" placeholder="Email Address">
</div>
<div class="form-group">
    <textarea rows="4" class="form-control" cols="50" id="message1" name="message" placeholder="Message"></textarea>
 </div>
 <button type="submit" id="email-button" class="btn btn-danger btn-contact">Submit</button>
 </form>
 </div>   

Here I used Ajax to send email. 在这里,我使用Ajax发送电子邮件。 From here I can successfully pass all these 3 parameter. 从这里我可以成功传递所有这三个参数。 I checked in chrome via debugging. 我通过调试检查了chrome。 I am getting all 3 values. 我正在获取所有3个值。

THIS IS MY AJAX CALL. 这是我的AJAX通话。

$(document).ready(function(){
     $('#email-button').click(function() {
           name = $('#name').val();
           email = $('#email').val();
           message2 = $('#message1').val();
            $.ajax({
                    type: "POST",
                    url: "email.php",
                    data: "name=" + name + "email=" + email + "message3=" + message2,

                     success: function (html) {
                     if (html == "true") {
                       $('#form').remove();
                       $('#response').html("<p>Thank you for your message. I'll get back to you ASAP </p>");
                     }
                     else {
                       $('#form').remove();
                       $('#response').html("<p> Something Wrong </p>");

                     }
                     }
                    });
                       return false;

                    });
                    });

But in my PHP code I am not able to store anything in $message variable. 但是在我的PHP代码中,我无法在$message变量中存储任何内容。 It's not storing any value in this variable. 它没有在此变量中存储任何值。 I tried with echo $message but it's showing me empty string. 我试着用echo $message但是显示的是空字符串。

THIS IS MY email.php FILE 这是我的email.php文件

 <?php
    if(isset($_POST['name'])) {
      $name = addslashes(strip_tags($_POST['name']));
    }
    if(isset($_POST['email'])) {
      $email = addslashes(strip_tags($_POST['email']));
    }
    if(isset($_POST['message3'])) {
      $message = addslashes(strip_tags($_POST['message3']));
    }

      $header = "Name:" . $name . "Email:" . $email ;
      $to = "xyz@gmail.com";
      $subject = "From My website";

      mail($to, $subject, $message, $header);
      echo "true";
   ?>

Even in mail I am getting only Subject. 即使在邮件中,我也只收到主题。 Not header nor Message.I am not able to find my mistake. 没有标题也没有消息。我找不到我的错误。 I am getting Name and Email via echo $header; 我通过echo $ header获取名称和电子邮件; but with same syntax I am not able to see $message. 但是使用相同的语法,我看不到$ message。 It's giving me empty string only. 它只给我空字符串。

这样尝试

data:{"name":  name,  "email" : email,  "message3" : message2}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM