verdrm Posted March 21, 2008 Share Posted March 21, 2008 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? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 21, 2008 Share Posted March 21, 2008 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 } ?> Quote Link to comment Share on other sites More sharing options...
phpretard Posted March 21, 2008 Share Posted March 21, 2008 if (isset($_POST['submit1'])){$result = mysql_query("INSERT INTO ...1");} if (isset($_POST['submit2'])){$result = mysql_query("INSERT INTO ...2");} Quote Link to comment Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 What is the form action I need to use cooldude832? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 21, 2008 Share Posted March 21, 2008 what do u mean form action? You dont' need to do much just have 1 submit button and add in a radio section similar to what I shown. Quote Link to comment Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 That doesn't work for me; are you sure that code is correct? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 21, 2008 Share Posted March 21, 2008 I didn't give you an exact "code" I gave you an example on how to do it Quote Link to comment Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 I need to submit the form in two different ways...(i.e.) one way needs to go to insert.php and another needs to go to insert2.php. Can you provide a code that will do that? Quote Link to comment Share on other sites More sharing options...
thomashw Posted March 21, 2008 Share Posted March 21, 2008 Do you mean you need it to go to two different pages simultaneously, or one of two pages depending on what a user selects? Quote Link to comment Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 One of two pages depending on what the user selects. Quote Link to comment Share on other sites More sharing options...
thomashw Posted March 21, 2008 Share Posted March 21, 2008 Okay. For the switch statement above he's saying put the code for BOTH pages on one submit page and have the form submit to this page. Then for each case you'd have the code depending on which they selected. Does that make sense? Quote Link to comment Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 It makes sense but it doesn't work. Quote Link to comment Share on other sites More sharing options...
thomashw Posted March 21, 2008 Share Posted March 21, 2008 Can you post the code you made using the switch statement? Quote Link to comment Share on other sites More sharing options...
verdrm Posted March 21, 2008 Author Share Posted March 21, 2008 <?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. Quote Link to comment Share on other sites More sharing options...
thomashw Posted March 21, 2008 Share Posted March 21, 2008 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. } Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 21, 2008 Share Posted March 21, 2008 what I am saying is you don't need to make 2 processor pages combine them into 1 page and the switch handles th alternative tasking of it. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 21, 2008 Share Posted March 21, 2008 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; } ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 21, 2008 Share Posted March 21, 2008 For a strict doc I thought that a form can have 0-1 inputs defined the type of "submit"? Quote Link to comment Share on other sites More sharing options...
Barand Posted March 22, 2008 Share Posted March 22, 2008 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> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 22, 2008 Share Posted March 22, 2008 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.