0

OK、これは少し長くなりそうです。javascript にするか、php にするか迷っています。次のようなお問い合わせフォームがあります。

<form action="contact.php" method="POST" id='contact_form'>
            <p><label for="name">Name</label>
            <input type='text' name='name' id='name' /></p>
            <div class="clear"></div>
            <p id='name_error' class='error'>Enter your name</p>

            <p><label for="email">Email</label>
            <input type='text' name='email' id='email' /></p>
            <div class="clear"></div>
            <p id='email_error' class='error'>Enter a valid email address</p>

            <p><label for="telephone">Telephone</label>
            <input type='text' name='telephone' id='telephone' /></p>
            <div class="clear"></div>
            <p id='subject_error' class='error'>Enter your telephone number</p>

            <p><label for="message">Details</label>
            <textarea name='message' id='message' rows="30" cols="30"></textarea></p>
            <div class="clear"></div>
            <p id='message_error' class='error'>Enter your message</p>

            <p id='mail_success' class='success'>Thank you. We will be in contact soon.</p>
            <p id='mail_fail' class='error'>Sorry, an error has occured. Please try again later.</p>

            <div id="button">
                <input type='submit' id='send_message' class="button" value='Submit' />
            </div>
        </form>

Javascript:

$(document).ready(function(){
        $('#send_message').click(function(e){

            //stop the form from being submitted
            e.preventDefault();

            /* declare the variables, var error is the variable that we use on the end
            to determine if there was an error or not */
            var error = false;
            var name = $('#name').val();
            var email = $('#email').val();
            var telephone = $('#telephone').val();
            var message = $('#message').val();


            if(name.length == 0){
                var error = true;
                $('#name_error').fadeIn(500);
            }else{
                $('#name_error').fadeOut(500);
            }
            if(email.length == 0 || email.indexOf('@') == '-1'){
                var error = true;
                $('#email_error').fadeIn(500);
            }else{
                $('#email_error').fadeOut(500);
            }
            if(telephone.length == 0){
                var error = true;
                $('#subject_error').fadeIn(500);
            }else{
                $('#subject_error').fadeOut(500);
            }
            if(message.length == 0){
                var error = true;
                $('#message_error').fadeIn(500);
            }else{
                $('#message_error').fadeOut(500);
            }

            //now when the validation is done we check if the error variable is false (no errors)
            if(error == false){
                //disable the submit button to avoid spamming
                //and change the button text to Sending...
                $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' });

                //we submit it to contact.asp
                $.post("contact.php", $("#contact_form").serialize(),function(result){
                    //and after the ajax request ends we check the text returned
                    if($.trim(result) == 'sent'){
                       $('#button').remove();
                       $('#mail_fail').fadeOut(500);
                       $('#mail_success').fadeIn(500);
                    }else{
                        //show the mail failed div
                        $('#mail_fail').fadeIn(500);
                        //reenable the submit button by removing attribute disabled and change the text back to Send The Message
                        $('#send_message').removeAttr('disabled').attr('value', 'Submit');
                    }
                });
            }
        });    
    });

そしてPHP:

<?php

$EmailFrom = Trim(stripslashes($_POST['email'])); 
$EmailTo = "joe@bloggs.com";
$Subject = "Contact Form Message";
$name = Trim(stripslashes($_POST['name'])); 
$email = Trim(stripslashes($_POST['email']));
$telephone = Trim(stripslashes($_POST['telephone'])); 
$message = Trim(stripslashes($_POST['message'])); 


$validationOK=true;
if (Trim($name)=="") $validationOK=false;
if (Trim($email)=="") $validationOK=false;
if (Trim($telephone)=="") $validationOK=false;
if (Trim($message)=="") $validationOK=false;
if (!$validationOK) {
  print "failed";
  exit;
}


$Body = "";
$Body .= "name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $telephone;
$Body .= "\n";
$Body .= "message: ";
$Body .= $message;
$Body .= "\n";


$success = mail($EmailTo, $Subject, $Body, "From: <$email>");

if ($success){
  print "sent";
}
else{
  print "failed";
}
?>

記入すると、「申し訳ありませんが、エラーが発生しました。後でもう一度やり直してください」というメッセージが表示されます。メッセージが表示されますが、どこが間違っているのかわかりませんか?

助けてくれてありがとう。

4

4 に答える 4

0

phpコードはユーザーに何も返しません。

  1. あなたのメールモジュールはPHPでオンになっていますか?
  2. デバッグのためにメッセージ本文をエコーバックする必要があります。

印刷は単なる構成であり、複数のアイテムを連続して「印刷」すると動作が異なるため、個人的には印刷の使用を避けています。問題ではありません。

于 2011-08-01T15:08:31.290 に答える
0

結果にページの html コード全体が含まれている場合は、スクリプトが適切なポイントで停止していないか、js が間違った URL に投稿されている可能性があります (はい、contact.php を指定したことがわかりますが、htaccess に書き換えルールがある場合)それが可能だ)。

exit(0); を追加してみてください。このスクリプトの一番下にあります。最後のifの下。この php は、電子メールの送信後にサイトの残りの部分を表示する別の php ファイルに含まれているようです。

于 2011-08-01T14:52:11.573 に答える
0

ローカルホストで作業していますか、それとも共有ホスティング サーバーでコードを実行していますか? (つまり、GoDaddy または類似の会社)

ローカルホストを使用している場合、メール サーバーが正しく構成されていない可能性があります。コードがホストされている場合、電子メールの送信元のプロバイダーで電子メール アカウントを設定していますか? あなたのアカウント パッケージは mail() 関数へのアクセスを提供していますか?

PHP エラー ログまたは Apache ログにアクセスできる場合は、それらを確認する必要があります。環境がどのように構成されているかを確認できるように、phpinfo ページも設定する必要があります。

どちらのシナリオでも、ブラウザで POST リクエストを調べて、サーバーが HTTP エラーを返しているかどうかを確認できます。Firebug で Firefox を使用するか、開発ツールを有効にして Chrome を使用します。

于 2013-09-18T23:20:33.843 に答える
-1

PHPはサポートサーバーなしでは正しく機能しないため、PHPをサポートするサーバーにアップロードされますか。

于 2011-08-01T12:12:02.477 に答える