jQuery AJAX form using mail() PHP script sends email, but POST data from HTML form is undefined

前端 未结 6 1603
青春惊慌失措
青春惊慌失措 2020-11-27 05:07

Thanks for taking the time to look, guys. I\'m creating a pretty basic AJAX contact form using jQuery. The email sends, but upon opening the email there is no POST data, so

相关标签:
6条回答
  • 2020-11-27 05:37

    You are using the wrong parameters name, try:

    if($_POST){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['text'];
    
    //send email
        mail("j.andrew.sears@gmail.com", "51 Deep comment from" .$email, $message);
    }
    
    0 讨论(0)
  • 2020-11-27 05:38

    Your PHP script (external file 'email.php') should look like this:

    <?php
    if($_POST){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['text'];
    
    //send email
        mail("j.andrew.sears@gmail.com", "51 Deep comment from" .$email, $message);
    }
    ?>
    
    0 讨论(0)
  • 2020-11-27 05:44

    You code should be:

       <section id="right">
          <label for="form_msg">Message</label>
          <textarea name="form_msg" id="#msg_text"></textarea>
          <input id="submit" class="button" name="submit" type="submit" value="Send">
       </section>
    

    Js

    var data = {
        name: $("#form_name").val(),
        email: $("#form_email").val(),
        message: $("#msg_text").val()
    };
    $.ajax({
        type: "POST",
        url: "email.php",
        data: data,
        success: function(){
            $('.success').fadeIn(1000);
        }
    });
    

    The PHP:

    <?php
    if($_POST){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['text'];
    
    //send email
        mail("email@gmail.com","My Subject:",$email,$message);
    }
    ?>
    
    0 讨论(0)
  • 2020-11-27 05:46

    There is no need to make a query string. Just put your values in an object and jQuery will take care of the rest for you.

    var data = {
        name: $("#form_name").val(),
        email: $("#form_email").val(),
        message: $("#msg_text").val()
    };
    $.ajax({
        type: "POST",
        url: "email.php",
        data: data,
        success: function(){
            $('.success').fadeIn(1000);
        }
    });
    
    0 讨论(0)
  • 2020-11-27 05:48

    Leave your email.php code the same, but replace this JavaScript code:

     var name = $("#form_name").val();
            var email = $("#form_email").val();
            var text = $("#msg_text").val();
            var dataString = 'name='+ name + '&email=' + email + '&text=' + text;
    
            $.ajax({
                type: "POST",
                url: "email.php",
                data: dataString,
                success: function(){
                $('.success').fadeIn(1000);
                }
            });
    

    with this:

        $.ajax({
            type: "POST",
            url: "email.php",
            data: $(form).serialize(),
            success: function(){
            $('.success').fadeIn(1000);
            }
        });
    

    So that your form input names match up.

    0 讨论(0)
  • 2020-11-27 05:53

    You're using the wrong post parameters:

        var dataString = 'name='+ name + '&email=' + email + '&text=' + text;
                          ^^^^-$_POST['name']
                                           ^^^^--$_POST['name']
                                          etc....
    

    The javascript/html IDs are irrelevant to the actual POST, especially when you're building your own data string and don't use those same IDs.

    0 讨论(0)
提交回复
热议问题