Jump to content

Multiple Submit Actions


verdrm

Recommended Posts

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
}
?>

<?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.

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.
}

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;
}
?>

 

 

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>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.