2017-07-31 22 views
1

我想通過JS發送一個表單,而不需要HTML,我有一個用戶信息表,並且有按鈕被自動添加使用JS,而我無法將該信息傳遞給HTML(JS表覆蓋刪除所有表單的HTML表),那麼我可以使用JS提交查詢到PHP嗎?直接從JS(無HTML)提交PHP表單

這是當我點擊JS創建

function banAccount(id, username){ 
    swal({ 
     title: "Do you really want to ban "+username+"?", 
     text: "Enter amount of days, -1 equals to permanent ban.", 
     input: 'number', 
     showCancelButton: true, 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Yes", 
     showLoaderOnConfirm: true, 
     preConfirm: function (duration) { 
      return new Promise(function (resolve, reject) { 
      setTimeout(function() { 
       if (duration === '0') { 
       reject('Please type -1 for permanent ban or a number greater than 1!') 
       } else { 
       resolve() 
       } 
      }, 2000) 
      }) 
     }, 
    allowOutsideClick: false 
    }).then(function(duration){ 
     action_id = id; 
     action_type = "ban"; 
     action_params = duration; 
     alert("id:"+action_id+", type:"+action_type+", params:"+action_params) 
     // Here's where I need to send "action_id", "action_type" and "action_params" to PHP 
    }); 
} 
+0

您標記了ajax,但您是否熟悉它? Ajax正是你需要的 – GrumpyCrouton

回答

2

您需要使用ajax。

$.ajax ({ 
    url: "PHP-PAGE-TO-POST-TO.php", 
    data: { action_id : action_id, 
      action_type : action_type, 
      action_params : action_params }, 
    success: function(result) { 
     alert("Hi, testing"); 
     alert(result); 
     //the variable "result" holds any value the PHP script outputs 
    } 
}); 
0

如果報警按鈕(在SwaI位的「那麼」部分(我猜甜警報))的輸出函數跑正確的數據,爲什麼你不把數據發佈到你的PHP文件?

$.post("YOURFILE.php", { id: action_id, type: action_type, params: action_params });