Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one form have multiple actions?

I am wanting to submit a form to different places based on selections made in the form. I had originally been planning to to send all to a central file/location and have it determine where to go next. However, I would like to do without this extra step if I could.

If the user wants to create/edit/delete elements go to page 1.
If the user wants to group/attach elements go to page 3.

I am trying to write a form builder. You can create/edit/delete forms, questions, and answers. I have everything for creating, editing, and deleting done. Those functions are performed without leaving the page, but now I am looking to assign answers to specific questions. The questions page and the answers page are separate. I am wanting to select a group of answers and submit an array of answer Ids (selected check boxes) to the question page where those Ids will then be assigned to a question. So basically the create, edit, and delete functions are on without leaving the page, but the assign function would be performed on a different page.

if(empty($delRowID) || empty(updateRowID) || empty($groupRows)) {
    qInsert();
}else{
    if(!empty($delRowID)) qDelete($delRowID);
    if(!empty(updateRowID)) qUpdate($updateRowID);
    if(!empty($groupRows)) {
        submit $groupRows to Question.php;
    }
}
like image 758
Brook Julias Avatar asked Jun 08 '10 11:06

Brook Julias


People also ask

Can a form have more than one action?

Let's learn the steps of performing multiple actions with multiple buttons in a single HTML form: Create a form with method 'post' and set the value of the action attribute to a default URL where you want to send the form data. Create the input fields inside the as per your concern. Create a button with type submit.

Can one form have multiple submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below.

Does a form require an action?

Is the action Attribute Required? Back in HTML4, the answer would be yes. Nowadays with HTML5, you are not required to specify an action attribute. If you have a form tag without an action attribute then the data will be sent to its own page.

What is the action of a form?

The action attribute specifies where to send the form-data when a form is submitted.


2 Answers

No, a form has only one action.

But you have options:

Javascript

However, you may change the action attribute with javascript:

<input type="submit" value="Edit" onclick="editform();return true;">
<input type="submit" value="Delete" onclick="deleteform();return true;">

together with a little javascript:

function editform() {
    document.myform.action = '/edit';
}
function deleteform() {
    document.myform.action = '/delete';
}

See also

  • How to set form action through JavaScript?
  • Different form ACTION depending on button pressed
  • jquery, changing form action

Multiple forms

If javascript is not an option for you, you may consider multiple forms in your page.

Multiple forms = multiple actions

No problems with javascript disabled clients, and standards compliant.

Multiple submit buttons - server side

Or you may handle the distinction of editing or deleting on the server side. No javascript needed.

Add multiple submit buttons to your form and give them the same name but a different value:

<input type="submit" name="btSubmit" value="Edit">
<input type="submit" name="btSubmit" value="Delete">

You then can retrieve the value of the button which has been clicked. In php, the following should do the job:

$_POST['btSubmit']

See http://www.chami.com/tips/internet/042599I.html for an example with classic asp.

like image 134
marapet Avatar answered Sep 29 '22 21:09

marapet


It is possible using JavaScript, but it's not recommended because some people turn JS off by default because of trojans and noisy behavior of some sites. It is considered polite to have your site working both with JS enabled and disabled.

Actually you don't need many form actions because every operation can be done using branching in the single form handler script.
Here is the very simple CRUD application example, performing displaying, editing, adding - all in one body and utilizing templates:

index.php

<?  
mysql_connect(); 
mysql_select_db("new"); 
$table = "test"; 
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part: 
  $name = mysql_real_escape_string($_POST['name']); 
  if ($id = intval($_POST['id'])) { 
    $query="UPDATE $table SET name='$name' WHERE id=$id"; 
  } else { 
    $query="INSERT INTO $table SET name='$name'"; 
  } 
  mysql_query($query) or trigger_error(mysql_error()." in ".$query); 
  header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);  
  exit;  
}  
if (!isset($_GET['id'])) { //listing part: 
  $LIST=array(); 
  $query="SELECT * FROM $table";  
  $res=mysql_query($query); 
  while($row=mysql_fetch_assoc($res)) $LIST[]=$row; 
  include 'list.php'; 
} else { // form displaying part: 
  if ($id=intval($_GET['id'])) { 
    $query="SELECT * FROM $table WHERE id=$id";  
    $res=mysql_query($query); 
    $row=mysql_fetch_assoc($res); 
    foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v); 
  } else { 
    $row['name']=''; 
    $row['id']=0; 
  } 
  include 'form.php'; 
}  
?>

form.php

<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>

list.php

<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>
like image 29
Your Common Sense Avatar answered Sep 29 '22 21:09

Your Common Sense