PHP Form multiple buttons

Go To


I have a form with 2 buttons, that depending on which is selected will either be deleted or edited from the database. Those are each individual pages using SQL statements (questionedit and questiondelete). However, when i press a button, nothing happens...Any Ideas

Here is my javascript

function SelectedButton(button)
    if(button == 'edit')
        document.testedit_questionform.action ="testedit_questionedit.php";
    }else if(button == 'delete'){
        document.testedit_questionform.action ="testedit_questiondelete.php";

Here is my form (being echoed from a loop)

<form name="testedit_questionform" action="SelectedButton" method="POST"> 
    <span class="grid_11 prefix_1" id="" >
Question:<input type="text" name="QuestionText" style="width:588px; margin-left:10px;" value="$row[0]"/>
    <input type="button" value="Edit" name="Operation" onclick="submitForm('edit')" />
    <input type="button" value="Delete" name="Operation" onclick="submitForm('delete')" />
    <input type="hidden" name="QId" value="$row[3]" /><br />
2012-04-04 17:25
by Ken


First of all, your function should be named submitForm

function submitForm(button) {
  if(button == 'edit') {
    document.testedit_questionform.action ="testedit_questionedit.php";
  } else if(button == 'delete') {
    document.testedit_questionform.action ="testedit_questiondelete.php";


And then, call submit method from your form.


An alternative to calling forms is: document.forms['FORM_NAME'].submit()

2012-04-04 17:29
by MatuDuke
That is what i needed, thank you - Ken 2012-04-05 17:54


Look at how you access the form to call submit(). Now look at how you access the form to change the action. One of them is clearly wrong.

2012-04-04 17:27
by Niet the Dark Absol


It would be easier to make one PHP file, and buttons with different names like this:

<form method="post" action="actions.php">
<input type="submit" name="action1" value="Action 1" />
<input type="submit" name="action2" value="Action 2" />

And the file actions.php:

if(isset($_POST["action1"])) {
     // action 1
elseif(isset($_POST["action2"])) {
    // action 2
2012-04-04 17:35
by Dion