简体   繁体   English

使用jquery和PHP发布表单值

[英]Post form values using jquery and PHP

I'm trying to send a simple form to an email using PHP , but cannot get the actual ajax function to work. 我正在尝试使用PHP将简单的表单发送到电子邮件,但是无法使实际的ajax函数正常工作。

This is the form: 形式如下:

<form id="quick-booking-form" method="post" action="#">
                    <input type="text" name="mail" id="mail">
                    <textarea name="message" id="message">
                    </textarea>
                    <input type="submit" value="Send the Message" id="SendTheForm">
                </form>

This is then sent to a separate Javascript function, which holds the ajax: 然后将其发送到一个单独的Javascript函数,该函数包含ajax:

$(function() {  
            //alert("page loaded");
            $('#quick-booking-form').submit(function(event) {
                event.preventDefault();
                alert("Start of function");
                var mail = $("#mail").val();
                var message = $("#message").val();
                alert("Mail: " + mail + " Message: " + message);

                $.ajax({
                    type: "POST",
                    url:"process.php",
                    data:{ 
                        "mail": mail,
                        "message": message, 
                        },
                    dataType: "text"
                }).success(function(result){
                    alert(result);
                    //next line: end of function
                    }) ;

                //next line: end of ajax
            });
        //next line: end of script
        }); 

which should send the data to this PHP file (process.php): 应该将数据发送到此PHP文件(process.php):

$msg = $_POST['message'];
                $UserName = $_POST['mail'];

                $contact = "Booking Manager";
                $reply = "web-bookings@thesite.net";

                //Subject is always...
                $subject = "A message from ".$UserName;
                $from = $reply;

                //test data
                //$contactDetails = "test@test.net";


                // send message
                $message = "Message reads: ".$msg;
                //$to = "notify@test.com";
                $to = $_POST['mail'];
                //$headers = "From: " . $contact."\r\n"."Reply-To:".$reply;
                $headers = "From: " . $contact;
                mail($to, $subject, $message, $headers);
                echo "Mail Sent.";

What should happen is that the form is submitted. 应该发生的是该表单已提交。 It then gets validated (code omitted) in validate.js . 然后在validate.js中对其进行验证(省略代码)。 If it passes, the form data is sent to process.php. 如果通过,则表单数据将发送到process.php。 This in turn sends a notification message to notify@test.com . 反过来,这会将通知消息发送到notify@test.com。

What currently happens is that the alert("Start of Function") fires, and so does the alert showing the correct values. 当前发生的情况是发出alert("Start of Function") ,并且alert("Start of Function")也会显示正确的值。

And this is as far as it gets. 这是它所能做到的。 The mail function does not seem to fire, and the last alert does not show either. 邮件功能似乎没有启动,并且最后一个警报也未显示。

You are right but some changes, try this 您说得对,但有些更改,请尝试

$(function() {  
//alert("page loaded");
$('#quick-booking-form').submit(function(event) {
    event.preventDefault();
    alert("Start of function");
    var mail = $("#mail").val();
    var message = $("#message").val();
    alert("Mail: " + mail + "Message: " + message);

    $.ajax({
        type: "POST",
        url:"process.php",
        data:{ 
            "mail": mail,
            "message": message, 
            },
        dataType: "text",
        success: function(html) {
           alert(html);
        }
    });
    return false;

    //next line: end of ajax
    });
    //next line: end of script
  }); 

Use preventDefault function and success for ajax instead of done 对Ajax使用preventDefault函数和成功而不是完成

try this Include JQUERY Library 试试这个Include JQUERY Library

HTML : HTML:

<form id="quick-booking-form" method="post" action="javascript:return false">
email:
<input type="text" name="mail" id="mail">
<br/>
message: <textarea name="message" id="message">
</textarea><br/>
<input type="submit" value="Send the Message" id="SendTheForm">
</from>

SCRIPT : 脚本:

$(function() {  
    //alert("page loaded");
    $('#quick-booking-form').submit(function(event) {

        var mail = $("#mail").val();
        var message = $("#message").val();
        if(mail==""){
            alert("enter Mail"); return false;
         }
        if(message==""){
            alert("enter message"); return false;
         }
        $.ajax({
            type: "POST",
            url:"process.php",
            data:{ 
                "mail": mail,
                "message": message, 
                },success:function(result){
                    alert(result);
                 }

        });

    //next line: end of ajax
    });
//next line: end of script
}); 

process.php : process.php:

$to =$_POST['mail'];
$subject = "Test mail";
$message =$_POST['mesage'];
$from = "bookings@test.net";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

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

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