2013-04-11 55 views
0

我有聯繫表單,它使用Javascript和PHP。Javascript沒有將變量傳遞給PHP與HTML表格

我的問題是,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 protected]"; 
$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(); 
?> 
+0

在PHP腳本中,當您的AJAX調用爲POSTING數據時,您必須從'$ _POST'檢索數據。在你的PHP中,你直接使用你的變量。誰已經初始化他們。? – MatRt

回答

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 
+1

我會使用循環來遍歷$ _POST,除此之外,+1 – Digitalis

+0

非常感謝,它通過用$ _POST ['name']替換$ name等來解決我的問題! –

0

Tr Y本:

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; 
} 
0

使用$ _ POST訪問POST變量:

if (!isset($_POST["my_post_variable"]) { 
die('No post arguments passed.'); 
} 
0

它因爲JavaScript客戶端腳本語言,PHP是一個服務器端。所以不能通過PHP直接訪問javascript的變量。

您可以訪問發佈的HTML表單的值作爲

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

您也可以嘗試在你的PHP腳本的起點這一行代碼,但是你需要確保該變量值不重新分配在您的PHP代碼中。

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

使用上述代碼,您將獲得變量中的HTML頁面值 - $ Name,$ Email,$ Message。

而且,你需要給消息字段命名。這是一個很好的做法,只寫小寫字母的名稱