让AJAX使用post与PHP交谈

时间:2013-06-13 19:41:00

标签: php jquery ajax forms

我试图让我的Ajax代码与我的PHP文件对话,然后再返回同一页面。

我希望我的表单可以填写,一旦发送命中,它就会与我的AJAX代码对话,因此在向最终用户返回成功或失败消息之前会与PHP文件进行对话。

Ajax代码是:

$(document).ready(function(){
    $(function() {  
        $(".submit").click(function() {  
            // validate and process form here
            var fname = $("#fname").val() ;
            var lname = $("#lname").val();
            var aemail = $("#aemail").val() ;
            var year = $("#year").val();
            var club = $("#club").val() ;
            var position = $("#position").val();
            var dataString = 'name='+ name + '&fname=' + fname + '&lname=' + lname + '&aemail='      + aemail + '&year=' + year + '&club=' + club + '&position=' + position;  


            $.ajax({  
                type: "POST",  
                url: "acad_process.php",  
                data: dataString,  
                success: function(){      
                    $('#contact_form').html("<div id='message'></div>");  
                    $('#message').html("<h2>Contact Form Submitted!</h2>")
                    .append("<p>Thank you. We will be in touch soon.</p>")  
                };
                return false;

                error: function(){
                    $('#contact_form').html("<div id='message'></div>");  
                    $('#message').html("<h2>Contact Form Failed!</h2>")  
                    .append("<p>Sorry, your form returned an error! Please try again.</p>")  
                    .hide()  
                    .fadeIn(1500, function() {  
                    });
                };
                return false;      
            });  
        });
    });
});
</script>

非常感谢任何帮助。因为我是AJAX的新手。感谢

问题似乎是PHP,因为在进一步调查中,我在PHP页面上收到错误500,并且没有任何内容返回到我的页面。 PHP代码是:

    <?php

   $email_to = "example@domain.co.uk";

   if (!empty($_POST))
   {
   $fname = $_POST["fname"];
   $lname = $_POST["lname"];
   $email_from = $_POST["aemail"];
   $year = $_POST ["year"];
   $club = $_POST ["club"];
   $position = $_POST ["position"];

   $email_subject = "Enquiry";

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: $email_from . \n";
$headers = "Reply-To: $email_from . \n";

$message = "<p> You have received a message!</p>";
    $message .= "<p><strong>First Name:</strong>" . $fname . "</p>";
    $message .= "<p><strong>Surname:</strong>" . $lname . "</p>";
    $message .= "<p><strong>School Year:</strong> " . $year . "</p>";
    $message .= "<p><strong>E-Mail:</strong>" . $aemail ."</p>";
    $message .= "<p><strong>Club:</strong> " . $club . "</p>";
    $message .= "<p><strong>Position:</strong>" . $position . "</p>";

    ini_set("sendmail_from",$email_from);
    mail($email_to, $email_subject, $message, $headers, '-f'.$email_from);
    }

    ?>

3 个答案:

答案 0 :(得分:0)

尝试将url:'acad_process.php'更改为url:'/ acad_process.php'

还要确保acad_process.php中没有任何语法错误

答案 1 :(得分:0)

您正在使用GET方法传递数据。下面是使用POST方法传递数据的示例

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
  }).success(function( msg ) {
    alert( "Data Saved: " + msg );
});

答案 2 :(得分:-1)

试试这个:

$(document).ready(function(){
        $(".submit").click(function() {  
            // validate and process form here
            var fname = $("#fname").val() ;
            var lname = $("#lname").val();
            var aemail = $("#aemail").val() ;
            var year = $("#year").val();
            var club = $("#club").val() ;
            var position = $("#position").val();
            var dataString = 'name='+ name + '&fname=' + fname + '&lname=' + lname + '&aemail='      + aemail + '&year=' + year + '&club=' + club + '&position=' + position;  


            $.ajax({  
                type: "POST",  
                url: "acad_process.php",  
                data: dataString,  
                success: function(){      
                    $('#contact_form').html("<div id='message'></div>");  
                    $('#message').html("<h2>Contact Form Submitted!</h2>")
                    .append("<p>Thank you. We will be in touch soon.</p>")  
                },
                error: function(){
                    $('#contact_form').html("<div id='message'></div>");  
                    $('#message').html("<h2>Contact Form Failed!</h2>")  
                    .append("<p>Sorry, your form returned an error! Please try again.</p>")  
                    .hide()  
                    .fadeIn(1500, function() { });
                }      
            });  // end of ajax
        }); // end of click listener
}); // end of doc ready