JQuery 提交点击确认对话框 PHP
JQuery confirmation dialog on submit click in PHP
我写了一个 php 代码,当我点击提交按钮时,组合框中的一些项目将被删除。现在我想要确认,我在下面写了不起作用的代码。
php代码:
$DeleteButton=$_REQUEST['DeleteButton'];
if ($DeleteButton=="delete") :
if ($DeleteComboBox=="PickOne") :
$DeleteButton = "" ;
else :
$query = "DELETE FROM `items` WHERE `id` = $DeleteComboBox LIMIT 1";
$result = mysql_query($query)
or die("Database deletion failed");
$DeleteButton = "" ;
endif ;
endif ;
echo "<BR><BR><FORM NAME=\"EditFORM\" ACTION=\"./index.php\" METHOD=POST>\n";
$sql_select = "SELECT * FROM items WHERE id>0 order by name" ;
$sql_result = mysql_query($sql_select)
or die ("Couldn't execute SQL query on db table.") ;
echo "<SELECT ID=\"DeleteComboBox\" NAME=\"DeleteComboBox\">";
echo "<OPTION VALUE=\"PickOne\" SELECTED>select item</OPTION>";
while ($row = mysql_fetch_array($sql_result)) {
echo "<OPTION VALUE=\"$row[0]\">" . $row[2] . " " . $row[1] . "</OPTION>";
}
echo "</SELECT>";
echo "<BR><BR><INPUT TYPE=SUBMIT NAME=\"DeleteButton\" VALUE=\"delete\" ID=\"DeleteButton\">\n" ;
echo "</FORM>\n";
JQuery 部分:
<script type="text/javascript">
$(document).ready(function() {
$("#dialog").dialog({
autoOpen: false,
modal: true
});
});
$("#DeleteButton").click(function(e) {
e.preventDefault();
currentForm = $(this).closest('form');
$("#dialog").dialog({
dialogClass: "no-close",
buttons : {
"yes" : function() {
currentForm.submit();
},
"no" : function() {
$(this).dialog("close");
}
}
});
$("#dialog").dialog("open");
});
</script>
问题是这段代码不起作用。如果我不添加 jquery 部分,代码可以正常工作,但是在添加 jquery 部分后,当我单击提交按钮时,会出现 jquery 对话框,但在单击是按钮后,将提交表单而不删除所选项目。
提交按钮的值仅在单击时提交,但您捕获了此事件,并执行了 e.preventDefault()
。之后,currentForm.submit()
不再记得点击了哪个按钮。
您可以向表单动态添加隐藏输入:
currentForm.append('<input type="hidden" name="action" value="delete" />');
currentForm.submit()
您可以在 PHP:
中检查此隐藏输入的值,而不是检查 $_REQUEST['DeleteButton']
$action = $_REQUEST['action'];
if ($action == 'delete'):
// ... delete the item
endif;
我写了一个 php 代码,当我点击提交按钮时,组合框中的一些项目将被删除。现在我想要确认,我在下面写了不起作用的代码。 php代码:
$DeleteButton=$_REQUEST['DeleteButton'];
if ($DeleteButton=="delete") :
if ($DeleteComboBox=="PickOne") :
$DeleteButton = "" ;
else :
$query = "DELETE FROM `items` WHERE `id` = $DeleteComboBox LIMIT 1";
$result = mysql_query($query)
or die("Database deletion failed");
$DeleteButton = "" ;
endif ;
endif ;
echo "<BR><BR><FORM NAME=\"EditFORM\" ACTION=\"./index.php\" METHOD=POST>\n";
$sql_select = "SELECT * FROM items WHERE id>0 order by name" ;
$sql_result = mysql_query($sql_select)
or die ("Couldn't execute SQL query on db table.") ;
echo "<SELECT ID=\"DeleteComboBox\" NAME=\"DeleteComboBox\">";
echo "<OPTION VALUE=\"PickOne\" SELECTED>select item</OPTION>";
while ($row = mysql_fetch_array($sql_result)) {
echo "<OPTION VALUE=\"$row[0]\">" . $row[2] . " " . $row[1] . "</OPTION>";
}
echo "</SELECT>";
echo "<BR><BR><INPUT TYPE=SUBMIT NAME=\"DeleteButton\" VALUE=\"delete\" ID=\"DeleteButton\">\n" ;
echo "</FORM>\n";
JQuery 部分:
<script type="text/javascript">
$(document).ready(function() {
$("#dialog").dialog({
autoOpen: false,
modal: true
});
});
$("#DeleteButton").click(function(e) {
e.preventDefault();
currentForm = $(this).closest('form');
$("#dialog").dialog({
dialogClass: "no-close",
buttons : {
"yes" : function() {
currentForm.submit();
},
"no" : function() {
$(this).dialog("close");
}
}
});
$("#dialog").dialog("open");
});
</script>
问题是这段代码不起作用。如果我不添加 jquery 部分,代码可以正常工作,但是在添加 jquery 部分后,当我单击提交按钮时,会出现 jquery 对话框,但在单击是按钮后,将提交表单而不删除所选项目。
提交按钮的值仅在单击时提交,但您捕获了此事件,并执行了 e.preventDefault()
。之后,currentForm.submit()
不再记得点击了哪个按钮。
您可以向表单动态添加隐藏输入:
currentForm.append('<input type="hidden" name="action" value="delete" />');
currentForm.submit()
您可以在 PHP:
中检查此隐藏输入的值,而不是检查$_REQUEST['DeleteButton']
$action = $_REQUEST['action'];
if ($action == 'delete'):
// ... delete the item
endif;