[英]Ajax how to pass form name/id to php
更新 :我不想添加隱藏字段,因為它可能被用戶(檢查元素)篡改?
請參考此問題的以下代碼。
// this is the id of the form
$("#idForm").submit(function(e) {
$.ajax({
type: "POST",
url: 'validate/action.php',
data: $("#idForm").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
在我的action.php
,我怎么知道#idForm
的$_POST
數據? 我可能將不同的表單發布到action.php
。 例如:
$("#FormAdd").submit(function(e) {
$.ajax({
type: "POST",
url: 'validate/action.php',
data: $(this).serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // data successfully added
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
$("#FormEdit").submit(function(e) {
$.ajax({
type: "POST",
url: 'validate/action.php',
data: $(this).serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // data successfully updated
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
$("#FormDelete").submit(function(e) {
$.ajax({
type: "POST",
url: 'validate/action.php',
data: $(this).serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // data successfully deleted
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
在每種形式中,您都可以添加隱藏的輸入,以指定操作的類型。 我看到您的表格基於CRUD動作。 例如,在添加表單中添加一個名為add的隱藏表單,然后在php端中,設置一個開關箱來確定您所處的表單
以添加形式為例,
<input type='hidden' value='add' name='action'/>
然后在PHP
if(isset($_POST['action'])){
switch($_POST['action']){
case 'add':
//do add
break;
}
}
如果您不想傳遞隱藏字段,則可以使用htaccess方式進行。 在Ajax URL中,在php文件中傳遞要訪問的函數的名稱。 然后檢查該功能並調用。
范例:
url:'validate.php/saveForm', //where saveForm is the function in the php file to save
否則,您可以為每個CRUD操作使用單獨的URL
如果您對隱藏的界面問題感到疑惑,請執行此操作
在php文件中,列出了可接受的CRUD類型:
$valid =[];
$valid = ['add', 'edit', 'delete'];
if(in_array((string) $_POST['action'],$valid,true) === false){
die('invalid CRUD action');
}
您必須在所有表單中傳遞一個額外的隱藏值,其中包含要提交的表單的值。 然后檢查php中的隱藏值
示例(以html格式)
<form id ="FormAdd">
<!--Your other fields -->
<input type="hidden" name="form_action" value="add"/>
</form>
<form id ="FormEdit">
<!--Your other fields -->
<input type="hidden" name="form_action" value="edit/>
</form>
<form id ="FormDelete">
<!--Your other fields -->
<input type="hidden" name="form_action" value="delete"/>
</form>
然后在action.php
$action = isset($_POST['form_action'])?$_POST['form_action']:"";
if($action == "add")
{
//#FormAdd submitted
}
elseif($action == "edit")
{
//#FormEdit submitted
}
elseif($action == "delete")
{
//#FormDelete submitted
}
編輯如果您不想包括隱藏字段。 您可以在javascript中傳遞值
$("#idForm").submit(function(e) {
//$data = $("#idForm").serialize();
$.ajax({
type: "POST",
url: 'validate/action.php',
data: $("#idForm").serialize() + '&form_action=' + "add", // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
對編輯和刪除執行相同的操作
首先是您可以加密/解密隱藏字段ID,然后顯示“這是最佳安全性方法”。 這是在另一個頁面中發送ID的Ajax方法調用此功能按鈕提交
function getCity(val) {
if(val){
$.ajax({
type:'POST',
url:'xyz.php',
data:'Ste_id='+val,
success:function(html){
// This I am Used For Bind Data To Html Input You Can Use anything display alert
$('#htmlcontrllname').html(html);
}
});
}else{
// This I am Used For Bind Data To Html Input You Can Use anything display alert
$('#htmlcontrllname').html('Your msg');
}
}
在xyz php頁面中,您可以使用此代碼
if (!empty($_POST['Ste_id']) && isset($_POST["Ste_id"])) {
try {
// create connection object you use your own
$db = new Cl_DBclass();
$con = $db->con;
$row = mysqli_query( $con, "select id, name from table_name
WHERE Ste_id= '" . $_POST["Ste_id"] . "' ORDER BY id ASC");
$rowcount = mysqli_num_rows( $row );
if( !empty($rowcount) ){
while ( $result = mysqli_fetch_assoc($row) ) {
$results[] = $result;
}
exit;
}
} catch (Exception $e) {
$error = $e->getMessage();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.