[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Any easy or smarter way to do this?

Posted on 2009-02-12
15
Medium Priority
?
180 Views
Last Modified: 2013-12-12
I have multiple buttons on a form which look like this

<input type="image" name="Button1AB" src="<?php echo"images/mainbutton.php"?>" border="0">
<input type="image" name="Button1CD" src="<?php echo "images/mainbutton.php"?>" border="0">
<input type="image" name="Button2XY" src="<?php echo"images/mainbutton.php"?>" border="0">
<input type="image" name="Button2HG" src="<?php echo "images/mainbutton.php"?>" border="0">
<input type="image" name="Dutton2NY" src="<?php echo"images/mainbutton.php"?>" border="0">         


then when someone clicks on those buttons I set a session like this

if((isset($_POST['Button1AB_x'])) || (isset($_POST['Button1AB_x'])))
{
session_destroy($_SESSION['Button']);
unset($_SESSION['Button1AB']);
$_SESSION['Button'] = 'Button1AB';
header("Location: ".src/students.php");
exit;
}
if((isset($_POST['Button1CD_x'])) || (isset($_POST['Button1CD_x'])))
{
session_destroy($_SESSION['Button']);
unset($_SESSION['Button1AB']);
$_SESSION['Button'] = 'Button1CD';
header("Location: ".src/students.php");
exit;
}

now there are around 50 buttons and you can see how repeatitive it can get. Is there a smart and easy way to accomplish this?
Thanks
0
Comment
Question by:syedasimmeesaq
  • 5
  • 4
  • 3
  • +1
15 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23625609
If you have 50 buttons on a form, you might want to think about changing them to radio buttons.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23625618
Try something like this:
foreach($_POST as $key=>$ignore) 
  if(substr($key,0,6)=='Button') {
    session_destroy($_SESSION['Button']);
    unset($_SESSION[$key]);  # ?
    $_SESSION['Button'] = $key;
    header("Location: ".src/students.php");
    exit;
  }

Open in new window

0
 
LVL 2

Expert Comment

by:basselkh
ID: 23625646
if you always have the word ( Button ) in the $_POST you may try:
foreach($_REQUEST as $key => $var){
	$_REQUEST[$key] = $var;
		
	if (preg_match('/Button/i', $key)){ //if you have another common word add it here you may /Button|AnotherWord/i
 
		$key = str_replace("_x", "", $key); // I noticed you remove the _x from each $_POST
 
		session_destroy($_SESSION['Button']);
		unset($_SESSION[$key]);
		$_SESSION['Button'] = $key;
		header("Location: .src/students.php");
		exit;
 
	}
 
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 39

Accepted Solution

by:
Roger Baklund earned 1000 total points
ID: 23625672
A couple of bugs fixed:
foreach($_POST as $key=>$ignore) 
  if(substr($key,0,6)=='Button') {
    list($btn,$ignore) = @explode('_',$key,2);
    session_destroy($_SESSION['Button']);
    unset($_SESSION[$btn]);  # ?
    $_SESSION['Button'] = $btn;
    header("Location: src/students.php");
    exit;
  }

Open in new window

0
 
LVL 2

Expert Comment

by:basselkh
ID: 23625677
thank you cxr you were faster than me
and yes

do :
foreach($_POST as $key=>$ignore)

instead of
foreach($_REQUEST as $key => $var)

but the :
$key = str_replace("_x", "", $key); // I noticed you remove the _x from each $_POST

may be useful
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23625750
Run this, and look at what comes through the form, and then think about a "Switch / Case" structure to deal with the input from $_POST

HTH, ~Ray
<?php // RAY_radio_plethora.php
 
 
if (!empty($_POST)) {
	var_dump($_POST); // SHOW THE POST VARIABLES
 
}
?>
<form action="<?=$PHP_SELF?>" method="post">
<br/><input name="a" type="radio" value="0" />0
<br/><input name="a" type="radio" value="1" />1
<br/><input name="a" type="radio" value="2" />2
<br/><input name="a" type="radio" value="3" />3
<br/><input name="a" type="radio" value="4" />4
<br/><input name="a" type="radio" value="5" />5
<br/><input name="a" type="radio" value="6" />6
<br/><input name="a" type="radio" value="7" />7
<br/><input name="a" type="radio" value="8" />8
<br/><input name="a" type="radio" value="9" />9
<br/><input name="a" type="radio" value="A" />B
<br/><input name="a" type="radio" value="B" />B
<input type="submit" name="go" value="go" />
</form>

Open in new window

0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 23627078
Thank you guys for your input. I tried CXR and basselkh's method. Which works fine for me. However I have a question. In my form I have other buttons which I don't want to process through this code and I have this now
if(!empty($_POST)){
foreach($_POST as $key=>$ignore)
  if(substr($key,0,6)=='region') {
    list($btn,$ignore) = @explode('_',$key,2);
    session_destroy($_SESSION['region1ba']);
    unset($_SESSION[$btn]);  # ?
    $_SESSION['region'] = $btn;
    header("Location: ../src/students.php");
    exit;
  }
}
you see its checking for all the posts and then going through iteration. What if I have a few buttons that I don't want them to go through it? what would I do in the scenario.
Thanks
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23627139
That would be the advantage of using the radio button, which is what the Founding Fathers of the WWW were thinking about when they devised it.  "We want to give our clients mutually exclusive choices."
0
 
LVL 2

Expert Comment

by:basselkh
ID: 23627180
in line 4 add the buttons names that you don't wont to be checked
split between them with |
 i.e:

 if (preg_match('/Button|buttonOne|anotherbutton/i', $key)){
foreach($_POST as $key => $var){
        $_POST[$key] = $var;
                
        if (preg_match('/Button/i', $key)){
 
                $key = str_replace("_x", "", $key); // I noticed you remove the _x from each $_POST
 
                session_destroy($_SESSION['Button']);
                unset($_SESSION[$key]);
                $_SESSION['Button'] = $key;
                header("Location: .src/students.php");
                exit;
 
        }
 
}

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 23627214
>> What if I have a few buttons that I don't want them to go through it? what would I do in the scenario.

Three options:

- Check those before the iteration
- Put exceptions within the loop: if($btn=="Button1XY") break;  
- Give them a name that does not start with "Button"
0
 
LVL 2

Assisted Solution

by:basselkh
basselkh earned 1000 total points
ID: 23627220
Sorry sorry
They must but edit button here


here the fixed code

on line 5 but the buttons you don't wont to process
foreach($_POST as $key => $var){
        $_POST[$key] = $var;
                
        if (preg_match('/Button/i', $key)){
         if (!preg_match('/DontprocessMe|button123/i', $key)){
 
                $key = str_replace("_x", "", $key); // I noticed you remove the _x from each $_POST
 
                session_destroy($_SESSION['Button']);
                unset($_SESSION[$key]);
                $_SESSION['Button'] = $key;
                header("Location: .src/students.php");
                exit;
 
        }
}
}

Open in new window

0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 23627310
Ray thank you for the input and it is great as it always have been. I completely understand what you are saying hbut unfortunately the application design currently require us to have buttons thats why I am emphasizing on buttons.
basselkh, I am not sure where that would go in my current code I posted above.
Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 23627338
ok will test out the suggestions
0
 
LVL 2

Expert Comment

by:basselkh
ID: 23627376
syedasimmeesaq just use the last code and modify line 5 as you need
0
 
LVL 9

Author Closing Comment

by:syedasimmeesaq
ID: 31546241
Thank you all of you for great input and solutions
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question