繁体   English   中英

如何使此php脚本改为发送Ajax

[英]How to make this php script send in ajax instead

我有一个联系表格,它只通过php atm发送,我想用ajax发送它? 最简单的方法是什么?

            <?php
            //If the form is submitted
            if(isset($_POST['submit'])) {

                //Check to make sure that the name field is not empty
                if(trim($_POST['contactname']) == '') {
                    $hasError = true;
                } else {
                    $name = trim($_POST['contactname']);
                }




                //Check to make sure that the subject field is not empty
                if(trim($_POST['subject']) == '') {
                    $hasError = true;
                } else {
                    $subject = trim($_POST['subject']);
                }

                //Check to make sure sure that a valid email address is submitted
                if(trim($_POST['email']) == '')  {
                    $hasError = true;
                } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
                    $hasError = true;
                } else {
                    $email = trim($_POST['email']);
                }

                //Check to make sure comments were entered
                if(trim($_POST['message']) == '') {
                    $hasError = true;
                } else {
                    if(function_exists('stripslashes')) {
                        $comments = stripslashes(trim($_POST['message']));
                    } else {
                        $comments = trim($_POST['message']);
                    }
                }

                //If there is no error, send the email
                if(!$hasError) {
                    $emailTo = '123@gmail.com'; // Put your own email address here
                    $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
                    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

                    if(wp_mail($emailTo, $subject, $body, $headers)) {
                        $emailSent = true;
                    }else{
                        echo '<p class="alert-message error">Error sending mail.</p>';  
                    }
                }
            }
            ?>

请也许在改进发送功能方面给出指示。

任何帮助真的很感激。

如果您需要查看表格让我知道,我将进行编辑并将其放入

您需要在客户端执行此操作

只需使用此插件http://jquery.malsup.com/form/

您可以按以下方式修改代码。

以下是您所要求的独立示例(未经试用)。

只需将两个代码块复制/粘贴到两个文件中:

contacttest.php (或任何您想调用的名称)
yourphpprocessor.php(如果改名,还必须改变它在AJAX代码块)

注意:
1.现在每个表单元素都有一个ID属性
2. <form>功能完全不再使用,实际上已通过e.preventDefault() 阻止了


HTML: contacttest.php

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />

        <style>
        </style>

        <script type="text/javascript">
            $(document).ready(function() {

                //If you want the output from php side in a lightbox...
                $('#alertmsg').dialog({
                    autoOpen: false,
                    modal: true,
                    width: 400,
                    buttons: {
                        Thanks: function() {
                            $(this).dialog('close');
                        }
                    }
                });

                $('#contactform').submit(function(e) {
                    e.preventDefault();
                    var frm = $('#cname').val();
                    var eml = $('#cemail').val();
                    var sub = $('#subj').val();
                    var msg = $('#msg').val();

                    //validation goes here, for eg.
                    if (frm == '' || eml==''){
                        alert('All fields must be completed');
                        return false;
                    }

                    $.ajax({
                        type: "POST",
                        url: "yourphpprocessor.php",
                        data: 'f=' +frm+ '&e=' +eml+ '&s=' +sub+ '&m=' +msg,
                        success: function(recd) {
                            $('#alertmsg').html(recd);
                            $('#alertmsg').dialog('open'); //Uses lightbox to display message
                        }
                    });
                }); //END AJAX


            }); //END $(document).ready()

        </script>
    </head>
<body>

    <form id="contactform" action="" method="POST">
        From: <input type="text" name="contactname" id="cname"><br />
        Email: <input type="text" name="email" id="cemail"><br />
        Subject: <input type="text" name="subject" id="subj"><br />
        Message: <textarea name="message" id="msg"></textarea><br /><br />
        <input type="submit" id="mysubmit" value="Submit">
    </form>

    <div id="alertmsg"></div>

</body>
</html>

PHP方面: yourphpprocessor.php

<?php
    $name = $_POST['f'];
    $email = $_POST['e'];
    $subject = $_POST['s'];
    $comments = $_POST['m'];

    $emailTo = '123@gmail.com'; // Put your own email address here
    $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
    $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

    if(wp_mail($emailTo, $subject, $body, $headers)) {
        $emailSent = true;
    }else{
        echo '<p class="alert-message error">Error sending mail.</p>';  
    }
?>

暂无
暂无

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

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