2016-11-15 422 views
3

我有輸入型文本這樣如何通過ajax post發送值輸入類型文本?

<input type="text" id="test" value=""> 

,我有AJAX功能後這樣。

$(document).ready(function() { 
    $('#summernote').summernote({ 
     height: 300,     // set editor height 
     minHeight: null,    // set minimum height of editor 
     maxHeight: null,    // set maximum height of editor 
     focus: true,     // set focus to editable area after initializing summernote 
     callbacks: { 
      onImageUpload: function(files) { 
       sendFile(files[0]); 
      } 
     } 
    }); 

    function sendFile(file, editor, welEditable) { 
     document.getElementById("loading_upload_threads_image").style.display = "block"; 
     data = new FormData(); 
     data.append("file", file);//You can append as many data as you want. Check mozilla docs for this 
     $.ajax({ 
      data: data, 
      type: "POST", 
      url: "threads_image_upload.php", 
      cache: false, 
      contentType: false, 
      processData: false, 
      success: function(url) { 
       $('#summernote').summernote('editor.insertImage', url); 
       document.getElementById("loading_upload_threads_image").style.display = "none"; 
      } 
     }); 
    } 
}); 

我想知道如何從id="test"值發送到我的AJAX的帖子?

+1

它應該已經在你的表單數據中,但要確保你使用formData()調用形式:'new formData($('form')[0])''。您是否觀看過瀏覽器開發人員工具中的AJAX請求/響應?你有沒有在項目中包含jQuery庫?是否有任何錯誤報告?你在網絡服務器上運行這個嗎? –

+0

@JayBlanchard OP創建一個空的FormData對象 –

+0

Ack! ()#RoryMcCrossan –

回答

4

您可以使用append()方法將您想要的任何數據添加到FormData對象 - 如您的評論事件所述。如果要發送的所有數據表單,那麼你可以提供form元素到FormData構造

data = new FormData(); 
data.append("file", file); 
data.append('test', $('#test').val()); 

另外,:試試這個。請注意,這些項目將被命名爲input作爲關鍵。

var data = new FormData($('form')[0]); 
+0

以及如何在threads_image_upload.php中使用值'id =「test」'? –

+0

那麼你發送POST請求,所以'$ _POST [「test」]' –

2

你可以這樣說:

HTML代碼:

<input type="text" id="test" value=""> 

JS代碼:

data = new FormData(); 
data.append("file", file); 
data.append("test", $('#test').val()); 

$.ajax({ 
    data: data, 
    type: "POST", 
    url: "threads_image_upload.php", 
    cache: false, 
    contentType: false, 
    processData: false, 
    success: function(url) { 
     $('#summernote').summernote('editor.insertImage', url); 
     document.getElementById("loading_upload_threads_image").style.display = "none"; 
    } 
}); 

,並在PHP中,你可以訪問它爲:

$test = $_POST['test']; 

希望這有助於!

+0

以及如何在threads_image_upload.php中使用值'id =「test」'? –

+0

由於根據原始的php,你可以訪問它:'$ _POST ['test']' –