笑故挽风 2015-06-24 12:55 采纳率: 100%
浏览 17

PHP Ajax表单发送

I am posting form data to a PHP script using a jQuery serizalize function.

My script is running ok and the PHP is executing although I am not receiving the posted values from my form in my PHP.

Here is my code

HTML:

<form id="contact_form" action="process.php" method="POST">
                        <div id="name-group" class="form-group">
                            <label for="name">Name</label>
                            <input type="text" class="form-control" name="name" placeholder="Ringo Starr" form="contact_form">
                        </div>
                        <div id="email-group" class="form-group">
                            <label for="email">Email</label>
                            <input type="text" class="form-control" name="email" placeholder="john@beatles.com" form="contact_form">
                        </div>
                        <div id="email-group" class="form-group">
                            <label for="message">Message</label>
                            <textarea rows="4" class="form-control" id="formMessageArea" name="message" placeholder="Your message here..." form="contact_form"></textarea>
                            <span class="countdown"></span>
                        </div>
                        <div class="result"></div>
                        <button id="submitButton" type="submit" class="btn btn-success">Submit</button>
                    </form>

Ajax Post:

$.ajax({
        url: $form.attr('action'),
        type: 'POST',
        datatype : 'html',
        data: data,
        success: function(response) {
            if (response.status === "success") {
                $('.result').text("Success!!");
            } else if (response.status === "error") {
                $('.result').text("Error!!");
            }
        }
    });

Serialized Data:

name=Jamie+Berke&email=jamie_b25%40hotmail.com&message=testing+123

Here is my PHP:

<?php
$autoResponse = true; //if set to true auto response email will be sent, if you don't want autoresponse set it to false
$autoResponseSubject = "Demo Contact Form"; 
$autoResponseMessage = "Hi, thank you testing the JQuery Contact Form Demo.";
$autoResponseHeaders = "From: email_from@yourWebsite.com";  

//we need to get our variables first
$email_to =   'jamie_b25@hotmail.com';
$subject  =   'A enquiry for The Retros!';
$name     =   $_POST['name'];
$email    =   $_POST['email'];
$message  =   $_POST['message'];

$body = "From: $name 
Message: 
$message";

$headers  = "From: $email
";
$headers .= "CC: test@test.com
";
$headers .= "Reply-To: $email
";


if(mail($email_to, $subject, $body, $headers)){
    if($autoResponse === true){
        mail($email, $autoResponseSubject, $autoResponseMessage, $autoResponseHeaders);
    }
    echo 'success'; 
}else{
    echo 'error';/
}

?>

Here are the errors my PHP is returning:

Notice: Undefined index: name in C:\Users\Jberke\Documents\Projects\theRetros\src\process.php on line 10

Call Stack: 0.0010 235032 1. {main}() C:\Users\Jberke\Documents\Projects\theRetros\src\process.php:0

Notice: Undefined index: email in C:\Users\Jberke\Documents\Projects\theRetros\src\process.php on line 11

Call Stack: 0.0010 235032 1. {main}() C:\Users\Jberke\Documents\Projects\theRetros\src\process.php:0

Notice: Undefined index: message in C:\Users\Jberke\Documents\Projects\theRetros\src\process.php on line 12

Call Stack: 0.0010 235032 1. {main}() C:\Users\Jberke\Documents\Projects\theRetros\src\process.php:0

If i add a if ( isset( $_POST['submit'] ) ) { } around the PHP block nothing is returned to the jQuery-ajax success function and my form doesn't return any result although the script seems to execute fine.

  • 写回答

3条回答 默认 最新

  • weixin_33728268 2015-06-24 13:21
    关注
            <script>
                    $('#submitButton').click(function(){
                        var data = $('#contact_form').serialize(); 
                            $.ajax({
                            url: $('#contact_form').attr('action'),
                            type: 'POST',
                            datatype : 'html',
                            data: data,
                            success: function(response) {
                                console.log(response);
                                if (response.status === "success") {
                                    $('.result').text("Success!!");
                                } else if (response.status === "error") {
                                    $('.result').text("Error!!");
                                }
                            }
                        });
                    });
            </script>
    

    Try this

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog