PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Mon Oct 02, 2006 6:05 pm
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,
quackor
Forum Newbie
Posts: 6 Joined: Mon Oct 02, 2006 5:34 pm
Post
by quackor » Mon Oct 02, 2006 6:12 pm
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
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Mon Oct 02, 2006 6:14 pm
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>
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Mon Oct 02, 2006 6:15 pm
That's a good idea. I was taught to do that earlier.
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Mon Oct 02, 2006 6:18 pm
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
wtf
Forum Contributor
Posts: 331 Joined: Thu Nov 03, 2005 5:27 pm
Post
by wtf » Mon Oct 02, 2006 6:24 pm
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>
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Mon Oct 02, 2006 6:28 pm
I don't want to start inserting Javascript just yet. I'm working more on my PHP/MySQL abilities.
impulse()
Forum Regular
Posts: 748 Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:
Post
by impulse() » Mon Oct 02, 2006 6:36 pm
Yeeeehawww. It's all working
I really am starting to love PHP/MySQL, things are starting to impress me loads
quackor
Forum Newbie
Posts: 6 Joined: Mon Oct 02, 2006 5:34 pm
Post
by quackor » Mon Oct 02, 2006 6:47 pm
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.