0

Javascript と PHP を使用するお問い合わせフォームがあります。

私の問題は、javascript がその変数を PHP スクリプトに渡さないことです。私は次のことを発見しました:

変数は、javascript 内の変数チェックに合格し、変数を表示するアラートを使用して、javascript に設定されます。

PHP スクリプトに移動すると、変数は存在しなくなります。PHPスクリプトのチェックをコメントアウトしようとしましたが、メールは正常に送信されましたが、PHPスクリプトの静的コンテンツを除いてメールは空でした。

http.send() 関数から変数を直接渡すなど、さまざまな方法を試しました。

関連するコードは次のとおりです (これらはすべて、Web サーバー上の 3 つの異なるファイルです)。

HTML:

div class="contact_form">
            <h4>Get in touch</h4>
            <form method="post">
                <input type="text" name="Name" id="name" value="Name" onfocus="this.value = this.value=='Name'?'':this.value;" onblur="this.value = this.value==''?'Name':this.value;" />
                <input type="text" name="Email" id="email" value="Email" onfocus="this.value = this.value=='Email'?'':this.value;" onblur="this.value = this.value==''?'Email':this.value;" />
                <input type="text" value="Subject (Hosting, Requests, Appeal, Report, etc.)" id="subject" onfocus="this.value = this.value=='Subject (Hosting, Requests, Appeal, Report, etc.)'?'':this.value;" onblur="this.value = this.value==''?'Subject (Hosting, Requests, Appeal, Report, etc.)':this.value;" />
                <textarea name="Message" id="body" onfocus="this.value = this.value=='Message'?'':this.value;" onblur="this.value = this.value==''?'Message':this.value;">Message</textarea>
                <input type="submit" name="submit" id="submit" value="send" class="submit-button" onClick="return check_values();" />
            </form>
            <div id="confirmation" style="display:none; position: relative; z-index: 600; font-family: 'Open Sans', sans-serif; font-weight: 300; font-size: 16px; color: #4e4e4e;"></div>
        </div> <!-- end .contact_form -->

Javascript:

var http = createRequestObject();
var areal = Math.random() + "";
var real = areal.substring(2,6);

function createRequestObject() {
    var xmlhttp;
    try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
  catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
    catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
    xmlhttp=new XMLHttpRequest();
  }
    return  xmlhttp;
}

function sendRequest() {
    var rnd = Math.random();
    var name = escape(document.getElementById("name").value);
    var email = escape(document.getElementById("email").value);
    var subject = escape(document.getElementById("subject").value);
    var body = escape(document.getElementById("body").value);

    try{
        http.open('POST','pform.php');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.send('name='+name+'&email='+email+'&subject='+subject+'&body='+body+'&rnd='+rnd);
        http.onreadystatechange = handleResponse;
    }
    catch(e){}
    finally{}
}

function check_values() {
    var valid = '';

    var name = document.getElementById("name").value;
    var email = document.getElementById("email").value;
    var subject = document.getElementById("subject").value;
    var body = document.getElementById("body").value;
    if(trim(name) == "" ||
        trim(email) == "" ||
        trim(subject) == "" ||
        trim(body) == "") {
            alert("Please complete all fields");
    } else {
        if(isEmail(email)) {
            document.getElementById("submit").disabled=true;
            document.getElementById("submit").value='Please Wait..';
            sendRequest();
        } else {
            alert("Email appears to be invalid\nPlease check and try again");
            document.getElementById("email").focus();
            document.getElementById("email").select();
        }
    }
}

function handleResponse() {
    try{
    if((http.readyState == 4)&&(http.status == 200)){
        var response = http.responseText;
      document.getElementById("confirmation").innerHTML = response;
      document.getElementById("confirmation").style.display ="";
        }
  }
    catch(e){}
    finally{}
}

function isUndefined(a) {
   return typeof a == 'undefined';
}

function trim(a) {
    return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

function isEmail(a) {
   return (a.indexOf(".") > 0) && (a.indexOf("@") > 0);
}

PHP:

<?php
error_reporting(0);

$page_title = "Contact Us Form";
$email_it_to = "email@example.com";
$error_message = "Please complete the form first";
$confirmation = "Thank you, your message has been successfully sent.";

if(!isset($rnd) || !isset($name) || !isset($email) || !isset($subject) || !isset($body)) {
    echo $error_message;
    die();
}

    $email_from = $email;
    $email_subject = "Contact Form: ".stripslashes($subject);
    $email_message = "Please find below a message submitted by '".stripslashes($name);
    $email_message .="' on ".date("d/m/Y")." at ".date("H:i")."\n\n";
    $email_message .= stripslashes($body);

    $headers = 'From: '.$email_from."\r\n" .
   'Reply-To: '.$email_from."\r\n" .
   'X-Mailer: PHP/' . phpversion();

    mail($email_it_to, $email_subject, $email_message, $headers);

    echo "<b>$confirmation</b>";
    die();
?>
4

4 に答える 4

1

変数を使用して何も取得していません$_POST-未設定の変数を使用しています。

すべての呼び出しを使用するように変更します$_POST

if(!isset($rnd) || !isset($name) || !isset($email) || !isset($subject) || !isset($body))

への変更:

if(!isset($_POST['rnd']) || !isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['subject']) || !isset($_POST['body']))

また、未定義の変数を使用している他の場所では、それを変更する必要があります。

または、次のようにすることもできます。

$rnd = mysql_real_escape_string($_POST['rnd']);
$name = mysql_real_escape_string($_POST['name']);
... and so on
于 2013-04-11T22:37:42.313 に答える
0

これは、Javascript がクライアント側のスクリプト言語であり、PHP がサーバー側であるためです。そのため、PHP を介して JavaScript の変数に直接アクセスすることはできません。

HTML フォームによって投稿された値には、次のようにアクセスできます。

<?php
$var_name = $_POST['name_of_the_variable_to_access'];
//then access these variables in your code.
?>

php スクリプトの開始時にこの 1 行のコードを試すこともできますが、php コードで変数値が再割り当てされていないことを確認する必要があります。

<?
extract($_POST);
//your code here.
?>

上記のコードを使用すると、HTML ページの値を変数 ($Name、$Email、$Message) で取得できます。

そして、メッセージ フィールドに名前を付ける必要があります。小文字のみで名前を書くことをお勧めします

于 2013-04-11T22:47:50.843 に答える
0

これを試して:

try{
    http.open('POST','pform.php?name='+name+'&email='+email+'&subject='+subject+'&body='+body+'&rnd='+rnd);
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadystatechange = handleResponse;
}
于 2013-04-11T22:36:58.943 に答える
0

$_POST を使用して POST 変数にアクセスします。

if (!isset($_POST["my_post_variable"]) {
 die('No post arguments passed.');
} 
于 2013-04-11T22:45:12.983 に答える