Page 1 of 1

Multiple buttons to do different actions

Posted: Mon Oct 02, 2006 6:05 pm
by impulse()
I have made a virtual banking system where so far a table displays a unique customer ID, name and balance. I have a text box with a button next to it to update the customers balance and now I want to expand this to add a button to delete a customer. How is it possible to do 1 action if a certain buttons is pressed and another action if another is pressed.
Here's what it looks like so you get a clear idea
http://stesbox.co.uk/php/vbank/vbank.php

The code is:

Code: Select all

$modBalance = $_REQUEST["balanceMod"];
 $boxID = $_REQUEST["userid"];

   if (isset($_REQUEST["balanceMod"])) {
      $insert = mysql_query("UPDATE balance SET balance='$modBalance' WHERE custID='$boxID'");
}

$query = mysql_query("SELECT 
                            b.custID, b.balance, c.id, c.fName, c.sName, c.address
                      FROM
                            balance as b
                      LEFT JOIN  
                            custDetails as c
                      ON   
                            b.custID = c.id");

?>
<center>
<table border="1" bgcolor="black">
  <tr>
    <th><font color="white"> ID </th> <th>><font color="white"> Name </th> <th>><font color="white"> Balance </th> <th>><font color
  </tr>
      <?php


while ($results = mysql_fetch_array($query)) { ?>

<form method="post" action="vbank.php">
<tr>
<td bgcolor="grey"><font color="white"><?php echo "<br><b>", $results['id']; ?></td>
<td bgcolor="grey"><font color="white"><?php echo "</b><br>", $results['fName']; ?>
<font color="white"><?php echo "<br>", $results['sName']; ?></td>
<td bgcolor="grey"><font color="white"><?php echo "<br>\$", $results['balance']; ?></td>
  <input type="hidden" name="userid" value="<?php echo $results['id']; ?>">
  <td bgcolor="grey"><font color="white">$<input type="text" name="balanceMod"></td>
  <td bgcolor="grey"><input type="submit" value="Modify Balance"></td>
  <td bgcolor="grey"><input type="submit" value="Delete Customer"></td>
  </form>
  <?php
}
?>
</table>
<br>
<center><table border="1" bgcolor="black">
<tr><th>
<form method="post" action="vbankAdd.php">
<center><input type="submit" value="Add"><br><font color="white"> a customer
</form>
</th></tr>
</table>
Stephen,

Posted: Mon Oct 02, 2006 6:12 pm
by quackor
I'm not all that experienced so this might be a brute solution but here's what I'd do:

Make your buttons POST forms with some hidden field values and put a buch of if-statements (make your file post to itself) that will check what type of button was set and execute code block corresponding to that button.

Hope that helps :)

Posted: Mon Oct 02, 2006 6:14 pm
by Luke
there's actually a couple options... you could use two different submit buttons and then test for the submit value

Code: Select all

<form method="post" action="#">
<input type="submit" name="submit" value="Modify Balance">
<input type="submit" name="submit" value="Delete Customer">
</form>

Code: Select all

switch($_POST['submit']){
    case "Delete Customer":
        // Delete customer
    default:
        // Modify Balance
}
Another way... and the way I prefer... is to create a new form for that button...

Code: Select all

<form method="post" action="#">
<input type="hidden" name="action" value="modify">
<input type="submit" value="Modify Balance"></form>

<form method="post" action="#">
<input type="hidden" name="action" value="delete">
<input type="submit" value="Delete Customer"></form>

Posted: Mon Oct 02, 2006 6:15 pm
by impulse()
That's a good idea. I was taught to do that earlier.

Posted: Mon Oct 02, 2006 6:18 pm
by impulse()
Oh dear. I've just realised I asked exactly the same question in the Databases forum earlier but regarding the 'Update Balance' button. And I didn't even realise :lol:

Posted: Mon Oct 02, 2006 6:24 pm
by wtf
Here's quick and dirty example

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
    <title><!-- Insert your title here --></title>
    
    <script type="text/javascript" language="javascript">
        
        function submitForm( act )
        {
            document.getElementById( 'action' ).value = act;
            document.getElementById( 'frm' ).submit();
        }
        
    </script>
</head>
<body>
    
    <form id="frm">
        
        <input type="hidden" id="action" name="action" value="" />
        
        <input type="button" id="a" name="a" value="a" onclick="submitForm('modify')" />
        <input type="button" id="b" name="b" value="b" onclick="submitForm('delete')" />
        
    </form>
    
    
    
</body>
</html>

Posted: Mon Oct 02, 2006 6:28 pm
by impulse()
I don't want to start inserting Javascript just yet. I'm working more on my PHP/MySQL abilities.

Posted: Mon Oct 02, 2006 6:36 pm
by impulse()
Yeeeehawww. It's all working :)

I really am starting to love PHP/MySQL, things are starting to impress me loads

Posted: Mon Oct 02, 2006 6:47 pm
by quackor
This doesn't really belong in this forum, but I believe you should also incorporate CSS, not only does it clean up your HTML/PHP code but also makes your table look a LOT nicer and cleaner. Serif fonts are especially not cool.