Jump to content

Multiple buttons on a page.


pthurmond

Recommended Posts

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

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">
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
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>&nbsp;</th>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<th>&nbsp;</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>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<th>&nbsp;</th>
</tr>
<tr>
<td align="center"><input name="back" type="submit" value=" Back " /></td>
<td>&nbsp;</td>
<th>&nbsp;</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.
[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.

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.