pthurmond Posted November 29, 2006 Share Posted November 29, 2006 Hello, I having trouble with using multiple buttons on my forms. I of course have the submit button for all of my forms, but I sometimes have a "Back" button or a "Delete Image" button and would like each button to do something different, but even though I am using separate names for each input button they still seem to jump into the normal submit area of the code.Any ideas?Thanks,Patrick Link to comment https://forums.phpfreaks.com/topic/28889-multiple-buttons-on-a-page/ Share on other sites More sharing options...
Psycho Posted November 29, 2006 Share Posted November 29, 2006 If you create mutiple "submit" buttons, then they will all attempt to submit. You need to create "regular" buttons.Submit button<input type="[b]submit[/b]" name="submit form" value="somevalue">"regular" button<input type="[b]button[/b]" name="Remove image" value="somevalue"> Link to comment https://forums.phpfreaks.com/topic/28889-multiple-buttons-on-a-page/#findComment-132282 Share on other sites More sharing options...
kenrbnsn Posted November 29, 2006 Share Posted November 29, 2006 No, you can have multiple submit buttons on a form. With in your processing script you need to test for which button was pressed and perform the appropriate action.For the OP, please post the source for your form and processing script.Ken Link to comment https://forums.phpfreaks.com/topic/28889-multiple-buttons-on-a-page/#findComment-132287 Share on other sites More sharing options...
pthurmond Posted November 29, 2006 Author Share Posted November 29, 2006 Here is the form:[code] <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div id="header"></div> <!-- user_navi layer--> <div id="user_navi" style="font-style:normal; color:#4E4FC6; font-weight:bold"> </div> <!-- Content2 layer--> <div id="content2" style="font-style:normal; color:#4E4FC6; font-weight:bold" align="center"> <br>PARTICIPANT LOGIN - Step 2 of 3 - <p> </p> </div> <!-- Continue4 layer--> <div id="continue4" style="font-style:normal; color:#4E4FC6; font-weight:bold" align="center"> <p> <br> <table width="350" border="0" id="table_c"> <tr align="left"> <th>First Name</th> <th>Last Name</th> <th>Date of Birth</th> <th> </th> </tr> <tr> <td> </td> <td> </td> <td> </td> <th> </th> </tr> <?php # Main login script, login.php $page_title = 'Login'; if ($fn && $ln) //If all the fields are ok then continue { //Current query status $query = "SELECT First_Name, Last_Name, Date_Of_Birth, Participant_ID FROM Participants WHERE First_Name LIKE '%$fn%' && Last_Name LIKE '%$ln%' ORDER BY First_Name"; //Run the query $result = mysql_query($query) or die('Query failed: ' . mysql_error()); if($result) { while($row = mysql_fetch_array($result, MYSQL_NUM)) { echo '<tr align="left">'; echo '<td>'; echo $row[0]; echo '</td>'; echo '<td>'; echo $row[1]; echo '</td>'; echo '<td>'; echo $row[2]; echo '</td>'; echo '<td align="center">'; echo '<input type="radio" name="pid" value="'; echo $row[3]; echo '" /></td>'; echo '</tr>'; } } else { $message .= '<p>Your search could not be completed due to a system error.</p><p>' . mysql_error() . '</p>'; } // Free resultset mysql_free_result($result); mysql_close($link); } else { $message .= '<p>Please try again!</p>'; } ?> <tr> <td> </td> <td> </td> <td> </td> <th> </th> </tr> <tr> <td align="center"><input name="back" type="submit" value=" Back " /></td> <td> </td> <th> </th> <td align="center"><input name="pfind" type="submit" value=" Continue " /></td> </tr> </table> </p> </div> <br clear="all" /> </form>[/code]And Here is the processing script:[code]<?php require_once('includes/config.php'); //Connect to the db session_start(); $fn = $_SESSION['plogin_fname']; $ln = $_SESSION['plogin_lname']; if (isset($_POST['pfind'])) { $message = NULL; //Creates an empty new variable $error = false; //Check first name if (empty($_POST['pid'])) { $id = FALSE; $error = true; $message .= '<p>You forgot to select a student!</p>'; } else { $id = $_POST['pid']; $_SESSION['plogin_pid'] = $id; header("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/ssi.php"); exit(); } } if(isset($_POST['back'])) { header("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/plogin.php"); exit(); }?>[/code]As you can see I have set the script to process the buttons differently, but it doesn't work. Link to comment https://forums.phpfreaks.com/topic/28889-multiple-buttons-on-a-page/#findComment-132295 Share on other sites More sharing options...
kenrbnsn Posted November 29, 2006 Share Posted November 29, 2006 Name all the buttons the same, for example "sbmt", then in the script do something like:[code]<?phpif (isset($_POST['sbmt']) && $_POST['sbmt'] == " Back ") // do your "back" processing?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/28889-multiple-buttons-on-a-page/#findComment-132340 Share on other sites More sharing options...
Psycho Posted November 29, 2006 Share Posted November 29, 2006 [quote author=kenrbnsn link=topic=116743.msg475832#msg475832 date=1164831008]No, you can have multiple submit buttons on a form. With in your processing script you need to test for which button was pressed and perform the appropriate action.[/quote]I didn't say you couldn't have multiple submit buttons on a page. I just said that a submit button will try to submit the form. The Back botton action, for ex., would be best handled by a client side function and would not require a submit button. Link to comment https://forums.phpfreaks.com/topic/28889-multiple-buttons-on-a-page/#findComment-132388 Share on other sites More sharing options...
kenrbnsn Posted November 29, 2006 Share Posted November 29, 2006 It all really depends on what the OP wants to do with the buttons. The "back" function may or may not be the same as the "back" button on the browser.Ken Link to comment https://forums.phpfreaks.com/topic/28889-multiple-buttons-on-a-page/#findComment-132432 Share on other sites More sharing options...
pthurmond Posted November 30, 2006 Author Share Posted November 30, 2006 I used the idea of using the same name for all the submit buttons and then checking the contents and that worked. Thanks! Link to comment https://forums.phpfreaks.com/topic/28889-multiple-buttons-on-a-page/#findComment-132551 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.