2014-01-13 25 views
0

這個想法是,這個腳本將我的HTML表單中輸入的文本的值POST到emailform.php,該值將該數據並將其添加到.txt文件中。我認爲我遇到的麻煩是將PHP中的$email的值設置爲html文本輸入的值。因此,當前腳本被觸發時,我會得到兩個警報(第一個'錯誤',然後'完成',來自.fail和.complete函數),然後重新加載頁面。這就是爲什麼我認爲問題是從PHP返回的信息,但也許我錯了。將數據從AJAX轉移到PHP的問題

<form method="post"> 
    <input type="text" name="email" value="" class="emailSubmitSidebar" placeholder=" Your Email"> 
    <input type="submit" name="submit" value="Add" class="submitButton" id="subscribeButton"> 
</form> 
<script type='text/javascript'> 
    $(document).ready(function() { 
     var subscribeButton = $('#subscribeButton'); 
     subscribeButton.click(function() { 
      $.ajax({ 
       url: 'emailform.php', 
       type: 'POST', 
       dataType: 'text', 
       data: {email: $("input[name=email]").val()}, 
      }) 
      .done(function(data) { 
       alert("Added!"); 
      }) 
      .fail(function() { 
       alert("error"); 
      }) 
      .always(function() { 
       alert("complete"); 
      }) 
     }) 
    }) 
</script> 

下面是PHP,我添加了前兩行來檢查是否有任何錯誤,其中沒有任何錯誤。奇怪的是,當我單獨運行PHP時,echo行在頁面上打印數字3時沒有任何明顯的原因。我評論了變量$email,因爲我被引導認爲它是更好/有必要首先檢查它是否爲isset

<?php 
    ini_set('display_errors', 'On'); 
    error_reporting(E_ALL); 
    $fileHandle = fopen('emailList.txt', 'a') OR die ("Can't open file\n"); 
    $email= isset($_POST['email']) ? $_POST['email'] : ""; 
    // $email=$_POST['email']; 
    $result = fwrite ($fileHandle, "$email; \n"); 
    fclose($fileHandle); 
    echo (!$result) ? "error" : $result; 
    die; 
?> 

回答

0

我覺得有什麼不對您的數據,試圖將其序列化爲JSON數據數組:

var data = $(form).serialize(); 

$.ajax({ 
    url: 'emailform.php', 
    type: 'POST', 
    dataType: 'text', 
    data: data, 
}) 

的值將作爲一個正常的POST請求被髮送出去的電子郵件地址會在$_POST['email'];

從這裏

更新了jQuery .done()和.fail()函數需要HTTP狀態代碼工作,所以你的PHP文件應該返回這些代碼。當一個請求被創建並且成功時,在200範圍內的任何東西都可以。在發生錯誤時,應返回400或500範圍內的值。這裏有一個完整的列表:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

因此,當返回2xx(用.success替換.done)時會觸發ajax .success函數,並且在返回狀態代碼4xx或5xx時調用.error。看下面的php如何實現這個。

數3你得到是fwrite函數的$返回值,根據:http://nl3.php.net/fwrite

的fwrite()返回寫入的字節數,或FALSE的錯誤。

讓你的PHP是這樣的:

<?php 
//ini_set('display_errors', 'On'); 
//error_reporting(E_ALL); 

if($fileHandle = fopen('emailList.txt', 'a')) { 

    if(isset($_POST['email']) && !empty($_POST['email'])) { 
    $email = $_POST['email']; 

    $result = fwrite ($fileHandle, "$email; \n"); 
    fclose($fileHandle); 

    if(isset($result) && !empty($result)) { // Something was written 
     http_response_code(200); 
     echo "File written"; 
    } else { // Writing failed 
     http_response_code(500); 
     echo "Writing of file failed"; 
    } 
    } else { // Empty email 
    http_response_code(500); 
    echo "Email was empty"; 
    } 
} else { // File could not be opened 
    http_response_code(500); 
    echo "File could not be opened for writing"; 
} 
?> 
+0

運行時沒有錯誤,但電子郵件沒有寫入文本文件,頁面在單擊(並且輸入顯示在URL中作爲參數)後重新加載。我該怎麼辦@ Neograph734? – trop

+0

請用你現在擁有的東西來更新你的問題。我看到你更新了它,但錯誤的'data:('input').val()'仍然存在。然後我們可以再看看它。重新加載沒有任何意義......同時刪除'OR DIE',而是用正確的http_response_code代替錯誤。你可能想用'fwrite($ fileHandle,「$ email; \ n」);'.txt文件不明白
;) – Neograph734

+0

我已經更新了這個問題!感謝您一直以來的幫助 :)。我已經改變了'data:'。我不知道如何用你的建議來代替「死亡」。是的,我發現
問題。查看問題中的新描述。基本上PHP腳本回應數字3,我認爲這觸發了警報。 @ Neograph734 – trop

0

(簡單)

更改HTML到:

<input type="text" name="email" value="" class="emailSubmitSidebar" placeholder=" Your Email"> 

更改JS到:

 $.ajax({ 
     url: 'emailform.php', 
     type: 'POST', 
     dataType: 'text', 
     data: {email: $("input[name=email]").val()} 
    }) 
    .done(function(data) { 
     // there checking 
     alert("success!") 
    }) 

將PHP更改爲:

$fileHandle = fopen('emailList.txt', 'a') OR die ("Can't open file\n"); 
    $email=$_POST["email"]; 
    $result = fwrite ($fileHandle, "$email; <br/>"); 
    fclose($fileHandle); 
    echo (!$result)? "error" : $result; 
    die; 
+0

我收到一個警告'錯誤'。它由.fail(我選中)啓動。我認爲問題在於PHP中的$ email變量,因爲文本文件正在接收換行符而不是電子郵件。 @ voodoo417 – trop

+0

@ user3163188在fwrite之前檢查'var_dump($ email)'。 – voodoo417

+0

@ user3163188或嘗試在'$ .ajax'中更改爲'data:{email:$(「input [name = email]」)。val()}' – voodoo417