JQuery确认对话框提交点击PHP

问题描述:

I wrote a php code in which when I click on submit button some item in combobox will be deleted. Now I want confirmation and I wrote below code which is not working. php code:

$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>
";
$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\">
" ;
echo "</FORM>
";

JQuery part:

<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>

The problem is this code is not working. If I don't add jquery part the code is perfectly working but after adding jquery part when I click submit button the jquery dialog appears but after clicking yes button the form will be submitted without deleting selected item.

我写了一个PHP代码,当我点击提交按钮时,组合框中的某些项目将被删除。 现在我想要确认,我在下面编写了无法正常工作的代码。 nphp代码: p>

  $ DeleteButton = $ _ REQUEST ['DeleteButton']; 
if($ DeleteButton ==  “删除”):
 if($ DeleteComboBox ==“PickOne”):
 $ DeleteButton =“”; 
 else:
 $ query =“DELETE FROM`itements` WHERE`id` = $ DeleteComboBox LIMIT 1  “; 
 $ result = mysql_query($ query)
或die(”数据库删除失败“); 
 $ DeleteButton =”“; 
 endif; 
endif; 
 
echo”&lt; BR&gt;&lt;  BR&gt;&lt; FORM NAME = \“EditFORM \”ACTION = \“./ index.php \”METHOD = POST&gt; 
“; 
 $ sql_select =”SELECT * FROM items WHERE id&gt; 0 order by name“;  
 $ sql_result = mysql_query($ sql_select)
ot die(“无法在db表上执行SQL查询。”); 
echo“&lt; SELECT ID = \”DeleteComboBox \“NAME = \”DeleteComboBox \“&gt;  “; 
echo”&lt; OPTION VALUE = \“PickOne \”SELECTED&gt; select item&lt; / OPTION&gt;“; 
while($ row = mysql_fetch_array($ sql_result)){
 echo”&lt; OPTION VALUE = \“$ 行[0] \ “&gt;” 中 。  $ row [2]。  “”。  $ row [1]。  “&lt; / OPTION&gt;”; 
} 
echo“&lt; / SELECT&gt;”; 
echo“&lt; BR&gt;&lt; BR&gt;&lt; INPUT TYPE = SUBMIT NAME = \”DeleteButton \“VALUE = \”delete  \“ID = \”DeleteButton \“&gt; 
”; 
echo“&lt; / FORM&gt; 
”; 
  code>  pre> 
 
 

JQuery部分: p >

 &lt; script type =“text / javascript”&gt; 
 $(document).ready(function(){
 $(“#dialog”)。dialog({\  n 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”); 
}); 
&lt; / script&gt; 
  code>  pre> 
 
 

问题是这段代码 不管用。 如果我不添加jquery部分代码完全正常工作但是在我单击提交按钮后添加jquery部分时会出现jquery对话框,但是在单击yes按钮后,将提交表单而不删除所选项目。 p> DIV>

The submit button's value is submitted only when it is clicked, but you catch this event, and do a e.preventDefault(). After it, currentForm.submit() do not remember anymore which button was clicked.

You could dynamically add a hidden input to the form:

currentForm.append('<input type="hidden" name="action" value="delete" />');
currentForm.submit()

And instead of checking $_REQUEST['DeleteButton'], you can check this hidden input's value in your PHP:

$action = $_REQUEST['action'];
if ($action == 'delete'):
    // ... delete the item
endif;