drxp993551 2016-11-26 11:28
浏览 137
已采纳

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.

  • 写回答

1条回答 默认 最新

  • douao7937 2016-11-26 12:17
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?