Jump to content

Multiple Submit Actions


verdrm

Recommended Posts

I have two submit buttons with different names, and I want the form to post differently for each. For example:

 

- Submit button 'Submit1' should post form values to insert.php

 

- Submit button 'Submit2' should post form values to insert2.php

 

How do I do this?

Link to comment
Share on other sites

the properly to allow 1 form to handle 2 unique processes is to use a radio button that controls the process not multiple submit buttons.

then use a switch on it

i.e

<input type="radio" name="action" value="insert1" />
<input type="radio" name="action" value="insert2" />

On process page

<?php
switch ($_POST['action']){
case "insert1":
#Do insert type 1
break;
case "insert2":
#Do insert type 2
break;
default:
#Output error
}
?>

Link to comment
Share on other sites

<?php

switch ($_POST['action']){

case "insert1":

$r = $_GET['id'];

header("Location:insert.php?id=$r&s=1");

break;

case "insert2":

$r = $_GET['id'];

header("Location:insert.php?id=$r&s=0");

break;

}

?>

 

What I need this code to do is SUBMIT the page to either insert.php with s=1 or s=0  OR  it could be to two separate pages.

Link to comment
Share on other sites

Put the switch statement on the submitted page.

 

So your page with the form would look like this:

 

<form action="submit.php" ....>
<input type="radio" name="action" value="1"...>
<input type="radio" name="action" value="2"...>

 

and then on submit.php:

 

switch ($_POST['action'])
{
case "1":
// The page for case "1".
break;
case "2":
// The page for case "2".
break;
default:
// Put an error line here.
}

Link to comment
Share on other sites

Give both submit buttons the same name but different values

 

<input type='submit' name='sub' value='A'>
<input type='submit' name='sub' value='B'>

 

then

<?php
switch ($_POST['sub'])
{
    case 'A':
         // do something
         break;
    case 'B':
         // do something else
         break;
}
?>

 

 

Link to comment
Share on other sites

try it

<?php 
           if (isset ($_POST['sub']))
           {
                switch ($_POST['sub'])
                {
                    case 'A':
                        echo 'you clicked A';
                        break;
                    case 'B':
                        echo 'you clicked B';
                        break;
                }
           }
?>
<form method='post'>
<input type='submit' name='sub' value='A'>
<input type='submit' name='sub' value='B'>
</form>

Link to comment
Share on other sites

this was strict valid which I thought it wasn't :)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test</title>
</head>
<body>
<form method='post' action="">
<div>
<input type='submit' name='sub' value='A' />
<input type='submit' name='sub' value='B' />
</div>
</form>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.