PDA

View Full Version : 2 button form...how to choose which is pressed.



Moshambi
09-22-2008, 04:29 AM
Ok so I have two buttons in a form. One is to modify text and one is to delete the photo. My problem is that I only want one to be read. Here is the code I tried:



if(isset($_POST['edit']))
{
$sql = "UPDATE pictures SET pictext = '".$_POST['pictext']."' WHERE id = '".$_POST['id']."'";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
else
{
echo "1 record updated";
}
}
else if(isset($_POST['delete']));
{
$sql = "DELETE FROM pictures WHERE id = '".$_POST['id']."'";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
else
{
echo "1 record deleted";
}
}


Now this doesn't work because both buttons are being posted obviously. Any quick ways to fix this? All help is appreciated.

schorhr
09-22-2008, 08:25 AM
One quick fix: give the buttons a different
name=foo / name=foo2 and check with isset for example

or...

use two forms (if possible).


here the example with name=


<form action="thispageorsomeother.php" method="post">
<input type="submit" name="buttonone" value="boo" />

<input type="submit" name="buttontwo" value="foo" />
<form/>


Now you can simply check
$_POST["buttonone"] and $_POST["buttontwo"]

Moshambi
09-22-2008, 04:20 PM
I'm not sure if you looked at my code or not. That is exactly what I'm doing, but it is not working. And I don't want to do two forms..unless I ABSOLUTELY have to. Thank you for the reply.

schorhr
09-22-2008, 05:27 PM
Strange, works fine here.
Still, could you post the HTML-Part (at least the form)?