Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,184 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,105 people online right now. Registration is fast and FREE... Join Now!




Two Buttons... One Page

2 Pages V  1 2 >  
Reply to this topicStart new topic

Two Buttons... One Page

grinnZ
24 Aug, 2008 - 09:32 PM
Post #1

New D.I.C Head
*

Joined: 6 Aug, 2008
Posts: 12


My Contributions
From all the searches that I have done I am assuming that two buttons on the same page is pretty much worthless. So I am coming here not so much for the specific code (although I am new to PHP I'm muddling through and making things work) but more to ask about a strategy on how to approach my problem. This situation is this...

On the form are two pulldowns the second of which is dependent on the choice of the first. Once the first is changed the second needs to query the database to determine the number of related records. Initially I thought Javascript but I cannot access the database using it. So I thought about my other options, either a link or a button and determined that a button was necessary because I also need to retrieve the $_POST data after the pulldown change so that I can repopulate the fields with user input not yet saved in the database when the form is redrawn. Already on the form there is obviously a submit button and this update pulldown button would be the second. But a second button would do basically the same as the first even with different name values and the $_POST values would be set for both buttons no matter which is used which rules out an isset test. So my question is... how to test which button is used or.. is there another method to solve my problem?

I do appreciate any and all input.


~ grinnZ ~
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Two Buttons... One Page
24 Aug, 2008 - 09:49 PM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
if you mean testing which one was pressed, name them.... submit1 and submit2, then see which one... so if($_POST['submit']
User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Two Buttons... One Page
25 Aug, 2008 - 01:40 AM
Post #3

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 403



Thanked: 38 times
Dream Kudos: 75
My Contributions
Just to expland on what jbrace said:

PHP:
CODE

if(isset($_POST['submit1'])){
      //submit1 button was pressed
}else if(isset($_POST['submit2'])){
     //submit2 button was pressed
}


HTML:
CODE

<form action="page.php" method="post">
<input type="submit" name="submit1" id="submit1" value="sub btn 1" /><br /><input type="submit" name="submit2" id="submit2" value="sub btn 2" />
</form>

User is offlineProfile CardPM
+Quote Post

Computer_
RE: Two Buttons... One Page
25 Aug, 2008 - 04:24 AM
Post #4

New D.I.C Head
*

Joined: 22 Aug, 2008
Posts: 16



Thanked: 1 times
My Contributions
The solution to this problem is Javascript of course.
Use Ajax and make a XML request to some other php which gives you the list of the seconf pulldown according to first choise.
You can read more about Ajax and how it works on this page : Ajax
User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Two Buttons... One Page
25 Aug, 2008 - 04:28 AM
Post #5

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 403



Thanked: 38 times
Dream Kudos: 75
My Contributions
The only problem with AJAX is - what if the user has javascript disabled?
User is offlineProfile CardPM
+Quote Post

Computer_
RE: Two Buttons... One Page
25 Aug, 2008 - 04:40 AM
Post #6

New D.I.C Head
*

Joined: 22 Aug, 2008
Posts: 16



Thanked: 1 times
My Contributions
QUOTE(pemcconnell @ 25 Aug, 2008 - 05:28 AM) *

The only problem with AJAX is - what if the user has javascript disabled?


Tell me what is the percent of users that have javascript disabled ?
... around 2%.
User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Two Buttons... One Page
25 Aug, 2008 - 04:45 AM
Post #7

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 403



Thanked: 38 times
Dream Kudos: 75
My Contributions
If you are working on a site that needs to match accessibility and compliance regulations, with a quote including javascript you'd be kindly asked to think again.

Believe me if I could throw it into every site I make I would, but the fact of the matter is, depending on your client (most sites I work on are governmental), you can't always get what you want.

*AND to add to that, there are a heck of a lot of buildings & companies that disallow javascript for security issues. The building I'm in now won't let 90% of the computers run javascript in their browsers.

This post has been edited by pemcconnell: 25 Aug, 2008 - 04:48 AM
User is offlineProfile CardPM
+Quote Post

Computer_
RE: Two Buttons... One Page
25 Aug, 2008 - 05:01 AM
Post #8

New D.I.C Head
*

Joined: 22 Aug, 2008
Posts: 16



Thanked: 1 times
My Contributions
Of course always depends on the client and the goal of the application.
Regardless these reasons , Ajax still best method to do this smile.gif
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Two Buttons... One Page
25 Aug, 2008 - 08:07 AM
Post #9

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
Without using javascript, you need to have two submit buttons, but it can work:
CODE
<?php
$xname= array ("Jim", "Bob" , "John");
$Jimjob=array("Jim's Option One","Jim's Option Two","Jim's Option Three","Jim's Option Four");
$Bobjob=array("Bob's Option One","Bob's Option Two","Bob's Option Three","Bob's Option Four");
$Johnjob=array("John's Option One","John's Option Two","John's Option Three","John's Option Four");
for  ($j = 0; $j <= 3; $j++) {
$opt[0][$j] = $Jimjob[$j];
$opt[1][$j] = $Bobjob[$j];
$opt[2][$j] = $Johnjob[$j];

}
if (isset($_POST['name'])) {
$name = $_POST['name'];
}

?>
<form action="<?php echo $php_self ?>" method="post">
<select name="name" >
<?php
for  ($i = 0; $i <= 2; $i++) {
unset($ch1);
if ($name==$i){
$ch1="selected=\"selected\"";
}
?>
<option name="name" value="<?php print $i; ?>" <?php print $ch1 ?> ><?php echo $xname[$i];?>
<?php
}

?>
</select>
<input type="hidden" name="job" value="<?php print $job ?>" />
<input type="submit" />
</form>

<?php
if (isset($_POST['job'])) {
$job = $_POST['job'];
}
?>
<form action="<?php echo $php_self ?>" method="post">
<select name="job" >
<?php
for  ($j = 0; $j <= 3; $j++) {
unset($ch1);
if ($job==$j){
$ch1="selected=\"selected\"";
}

?>
<option name="job" value="<?php print $j; ?>" <?php print $ch1 ?> ><?php echo $opt[$name][$j];?>
<?php
}
?>
</select>
<input type="hidden" name="name" value="<?php print $name ?>" />
<input type="submit" />
</form>



User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Two Buttons... One Page
25 Aug, 2008 - 08:10 AM
Post #10

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 403



Thanked: 38 times
Dream Kudos: 75
My Contributions
nice job dude smile.gif
User is offlineProfile CardPM
+Quote Post

grinnZ
RE: Two Buttons... One Page
25 Aug, 2008 - 12:38 PM
Post #11

New D.I.C Head
*

Joined: 6 Aug, 2008
Posts: 12


My Contributions
Let me thank all who have jumped in with replies especially JBrace who so quickly supplied the original answer... he says while running for the PHP manual to decipher CTphpnwb's code.


~ grinnZ ~
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Two Buttons... One Page
25 Aug, 2008 - 01:50 PM
Post #12

D.I.C Regular
Group Icon

Joined: 8 Aug, 2008
Posts: 481



Thanked: 32 times
My Contributions
biggrin.gif

Here it is with some comments:
CODE
<?php
$xname= array ("Jim", "Bob" , "John");
$Jimjob=array("Jim's Option One","Jim's Option Two","Jim's Option Three","Jim's Option Four");
$Bobjob=array("Bob's Option One","Bob's Option Two","Bob's Option Three","Bob's Option Four");
$Johnjob=array("John's Option One","John's Option Two","John's Option Three","John's Option Four");
// This sets up the array to contain the second button's options: Row 0 contains Jim's, row 1 contains Bob's, and row 2 contains John's.
for  ($j = 0; $j <= 3; $j++) {
$opt[0][$j] = $Jimjob[$j];
$opt[1][$j] = $Bobjob[$j];
$opt[2][$j] = $Johnjob[$j];

}

if (isset($_POST['name'])) {
$name = $_POST['name'];
}

?>
<form action="<?php echo $php_self ?>" method="post">
<select name="name" >
<?php
// loop through each of the three names for the first button.
for  ($i = 0; $i <= 2; $i++) {
unset($ch1); // This sets $ch1 to be a null string.
// if $name =0, then the first name is to be selected, =1, then second name, =2, the third.
if ($name==$i){
$ch1="selected=\"selected\""; // This sets $ch1 to be: selected="selected"
}
?>
<option name="name" value="<?php print $i; ?>" <?php print $ch1 ?> ><?php echo $xname[$i];?>
<?php
//Still looping through the three names, we set each of the three options, only checking off the currently selected name.
}
// After looping, we add the hidden value of the currently selected job option # to the post.
?>
</select>
<input type="hidden" name="job" value="<?php print $job ?>" />
<input type="submit" />
</form>

<?php
// We do basically the same thing as above for the second button.

if (isset($_POST['job'])) {
$job = $_POST['job'];
}
?>
<form action="<?php echo $php_self ?>" method="post">
<select name="job" >
<?php
for  ($j = 0; $j <= 3; $j++) {
unset($ch1);
if ($job==$j){
$ch1="selected=\"selected\"";
}

?>
<option name="job" value="<?php print $j; ?>" <?php print $ch1 ?> ><?php echo $opt[$name][$j];?>
<?php
}
?>
</select>
<input type="hidden" name="name" value="<?php print $name ?>" />
<input type="submit" />
</form>



User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:00AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month