Single PHP form with 2 buttons (edit and delete)

I am trying to create a form with two buttons - "DELETE", which runs on the delete.php page and "EDIT", which runs on edit.php.

So I need another action, depending on which button they pressed ...

Here is my code.

$con = mysql_connect("localhost","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $result = mysql_query("SELECT * FROM myTable"); // Need to change the action depending on what button is clicked echo "<form name='wer' id='wer' action='delete.php' method='post' >"; echo "<table border='1'>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['page_title'] . "</td>"; echo "<td><input type='radio' name='test1' value='" . $row['id'] . "' /></td>"; echo "</tr>"; } echo "<tr>"; echo "<td>&nbsp;</td>"; echo "<td>&nbsp;</td>"; echo "<td><input type='submit' name='delete' value='Delete' /> <input type='submit' name='edit' value='Edit' /></td>"; echo "</tr>"; echo "</table>"; echo "</form>"; mysql_close($con); 
+4
source share
2 answers

You can do this using JavaScript:

 <input type='button' value='Delete' onclick=' this.form.action = "delete.php"; this.form.submit(); ' /> 

Or you can do it with PHP, having a form action, for example action.php , which will contain:

 if (!empty($_REQUEST['delete'])) { require_once dirname(__FILE__) . '/delete.php'; else if (!empty($_REQUEST['edit'])) { require_once dirname(__FILE__) . '/edit.php'; } 
+5
source

Check isset($_POST['delete']) and isset($_POST['edit']) .

However, the idea of bad is to press the delete button first. When you press the ENTER key in browsers, the first button is usually used - and pressing the input in the form should not delete files.

+5
source

All Articles