0

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>\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 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 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

could you please elaborate more. I just want to delete the item on button click. How can I use this hidden field to accomplish this?
Thanks. That answer saved me ^_^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.