homepage Welcome to WebmasterWorld Guest from 23.22.179.210
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Submit form questions.
AlexLee




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

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?

 

Paul in South Africa




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

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;
}

grandpa




msg:1248476
 11:32 am on Feb 10, 2005 (gmt 0)

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...

AlexLee




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

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")

AlexLee




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

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.

hakre




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

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.

AlexLee




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

Thanks for the help and fast reply!

AlexLee




msg:1248481
 12:50 am on Feb 11, 2005 (gmt 0)

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.

AlexLee




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

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.

hakre




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved