yandoo Posted March 29, 2008 Share Posted March 29, 2008 Hello there, I was hoping for a little help with my form. It has 2 submit buttons, each with its own function (delete and insert) I need to distinguish between which button is clicked.... Heres what i got: if(isset($_POST['perform'])) { foreach($_POST['id'] as $id) { // This will loop through the checked checkboxes $query_test="DELETE FROM animalfears WHERE AnimalFearID='$id'"; // Change yourtable and id. $test = mysql_query($query_test) or die("Problem with the query: $query_test<br />" . mysql_error()); }} else if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO animalfears (AnimalFearID, AnimalID, FearID) VALUES (%s, %s, %s)", GetSQLValueString($_POST['animalfearid'], "int"), GetSQLValueString($_POST['animalid'], "int"), GetSQLValueString($_POST['select'], "text")); <form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>"> <input type="submit" name="Submit" value="Submit" /> <input type="submit" name="perform" id="perform" value="Delete" /> } I get wierd results when i run the page, the INSERT submit button works, but the DELETE Button strangley INSERTS a record instead?? So i dont think it is determing which Button is clicked... How can i get this work propery? Thank You Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted March 29, 2008 Share Posted March 29, 2008 use one form for each submit button like... <form method="post" action="whateever"> <input type="hidden" name="id" value="someid"> <input type="hidden" name="action" value="insert"> <input type="submit" value="insert"> </form> <form method="post" action="whateever"> <input type="hidden" name="id" value="someid"> <input type="hidden" name="action" value="delete"> <input type="submit" value="insert"> </form> then grab the action and id(if applicable) from the hidden posted fields and process the request as such. Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 29, 2008 Share Posted March 29, 2008 Use javascript to direct the form action based on which button was pressed. Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted March 29, 2008 Share Posted March 29, 2008 yeah, if for some reason you need both buttons in one set of <form> tags Quote Link to comment Share on other sites More sharing options...
yandoo Posted March 29, 2008 Author Share Posted March 29, 2008 Hi there, Thaks for reply,. Is it possible to do it with just the one form though??? I At them moment the Delete button now works!!! However, it DELETES the record but also INSERTS a record too????? I feel im close, if it is possible to do it in one form?? Thanks Quote Link to comment Share on other sites More sharing options...
benjaminbeazy Posted March 29, 2008 Share Posted March 29, 2008 if you use 1 form, as the other guy said. use javascript and change you submit buttons to regular buttons. on clicking one of those, have javacript change the form action and submit the form... Quote Link to comment Share on other sites More sharing options...
yandoo Posted March 29, 2008 Author Share Posted March 29, 2008 Hi, JavaScript to distinguise between which 2 buttons??? Is that easy to implement?? You know of how or where i can find out how to do it please Thanks Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted March 29, 2008 Share Posted March 29, 2008 You should never rely on javascript for validation or detection... In this case what you need to is very simple - check which button has been clicked... Like the checkbox and radio button the submit button is only set when it is clicked - if it is not clicked then no value for that element is set. So you have two submit buttons give them a name/id and then use that in the processing script.. the html... <input type="submit" name="insert" id="insert" value="insert"> <input type="submit" name="delete" id="delete" value="delete"> the code... <?php if (!empty($_POST['insert'])) { // do insert stuff. } else { // do delete stuff. } ?> Quote Link to comment Share on other sites More sharing options...
yandoo Posted March 29, 2008 Author Share Posted March 29, 2008 Hi there, Thank you every body for replying and offering, help i managed to get it working...I re thought what you orignally said about having 2 seperate forms and have found it to work a charm... Was worried because i was using a query for BOTH forms and thought it may cause problems! Additionally i was confussed about how to integrate the: if(isset($_POST['perform'])) { and the: if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { And i can see how: <?php if (!empty($_POST['insert'])) { // do insert stuff. } else { // do delete stuff. } ?> Will work too!! Ive really learnt a lot here Thaks all for you help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.