更新:我不想添加隐藏字段,因为它可能被用户篡改(检查元素)?
请参阅this问题中的以下代码。
// 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
我如何知道来自$_POST
的{{1}}数据?我可能会将不同的表单发布到#idForm
。例如:
action.php
答案 0 :(得分:1)
在每个表单中,您可以添加指定操作类型的隐藏输入。我看到您的表单基于CRUD操作。例如,在添加表单中添加一个名为add的隐藏表单,然后在php端,有一个switch case来确定你在哪个表单
例如,添加表格
<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');
}
答案 1 :(得分:1)
您必须以所有形式传递一个额外的隐藏值,其中包含提交表单的值。然后检查php中的隐藏值
示例(IN 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.
});
对编辑和删除执行相同操作
答案 2 :(得分:0)
首先是您可以加密/解密隐藏字段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();
}
}