Welcome to WebmasterWorld Guest from 54.166.54.215

Forum Moderators: coopster & jatar k

Submit form questions.

   
10:42 am on Feb 10, 2005 (gmt 0)

10+ Year Member



How do I do an if else onn buttons being pressed in forms?
Let's say I have a couplr ot forms. With their submit buttons named as "Add", "Edit", "Remove"...

And I have a process.php.

How do I do...
if(buttton pressed == "Add")
Do this...
else if(buttton pressed == "Edit")
Do that...
else if(buttton pressed == "Remove")
Do that...

What would be the actually code there?

11:17 am on Feb 10, 2005 (gmt 0)

10+ Year Member



if (isset($_POST[submit]) && $_POST[submit] == "Add")
{
Do stuff;
}
elseif (isset($_POST[submit]) && $_POST[submit] == "Edit")
{
Do stuff;
}
elseif (isset($_POST[submit]) && $_POST[submit] == "Remove")
{
Do stuff;
}
11:32 am on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi AlexLee.

You need to do a couple of things to make this work. First, set up your forms, one each for Add, Edit and Remove.
The form action will be to self, so next thing is retrieve what you have posted when you submit the form.
Do that with the $_POST variable. Here's a look at one way to code this.

<?php

$Add = $_POST['Add'];
$Edit = $_POST['Edit'];
$Remove = $_POST['Remove'];

if ($Add)
{
// Add Something
exit;
}

if ($Edit)
{
// Edit Something
exit;
}

if ($Remove)
{
// Remove Something
exit;
}
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type=hidden name="Add">
<input type="submit" value="Add"></form>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type=hidden name="Edit">
<input type="submit" value="Edit"></form>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="hidden" name="Remove">
<input type="submit" value="Remove"></form>

Good luck.
don't blame me if it breaks anything...

12:56 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



I am getting this error: Notice: Use of undefined constant submit - assumed 'submit' in C:\Program Files\Apache Group\Apache2\htdocs\FYP\admin\process_users.php on line 7

And the line in question: if (isset($_POST[submit]) && $_POST[submit] == "Add")

1:01 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



Oh, I forget to state that "Add", "Edit" and "Remove" are 3 different pages. But they are posting to the same process.php

$_POST['ButtonName'] just keeps giving me undefined index error.

1:01 pm on Feb 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi AlexLee,

don't get confused about this warning. it's very good that your reporting settings are set to warning. the index of the array is a string, Paul in South Africa just forgot to quote them. use $_POST['submit'] instead of $_POST[submit] (i marked the difference in bold) and everything should be fine. you can compare this to grandpas' post, he is using the correct syntax, too.

i guess that paul in south africa has warning messages switched off, so he learned it's no problem to simply write array indexes as constants instead of strings. it's a common mistake.

1:31 pm on Feb 10, 2005 (gmt 0)

10+ Year Member



Thanks for the help and fast reply!
12:50 am on Feb 11, 2005 (gmt 0)

10+ Year Member



Opps, I htink I celebrated too early.

When I am using if (isset($_POST['submit']) && $_POST['submit'] == "Add")
I always ended up in the "else" section.

And when I use if($_POST['Add']) My else if will get undefined index error.

12:57 am on Feb 11, 2005 (gmt 0)

10+ Year Member



I think I know the error.

Must I put <input type=hidden name="ButtonName"> of all the buttons not included in that form? So that no matter what, the process.php will still catch the button Edit and Remove. And there would not be undefine index.

9:53 am on Feb 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



here is my suggestion on how to handle the problem with one form element:

<form ...>
<input type="submit" name="act_add" value="Add" />
<input type="submit" name="act_del" value="Del" />
<input type="submit" name="act_edit" value="Edit" />
</form>

to realize, which button was pressed is then quite easy. because it's all a submit-type input element, only the data of the pressed button is passed. you can then check a pressed button by checking the according $_POST[button name] array entry:

if (isset($_POST['act_add'])) { 
//Add Button pressed
...
}
if (isset($_POST['act_del'])) {
//Del Button pressed
...
}
if (isset($_POST['act_edit'])) {
//Edit Button pressed
...
}

this will even work with more then one form element but it does not make sense to have the same form element multiple times in your html.

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month