0

お問い合わせフォームがあり、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>';  
                    }
                }
            }
            ?>

送信機能の改善についても指摘してください。

どんな助けでも本当に感謝しています。

フォームを見る必要がある場合はお知らせください。編集して挿入します

4

2 に答える 2

0

次のようにコードを変更できます。

以下は、まさにあなたが要求したことのスタンドアロンの例 (テストされていません) です。

2 つのコード ブロックを 2 つのファイルにコピーして貼り付けるだけです。

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>';  
    }
?>
于 2013-09-21T00:08:09.847 に答える