Forum Moderators: coopster

Message Too Old, No Replies

Getting a single $_Post variable

So simple, why doesn't it work?

         

neophyte

5:52 am on Mar 15, 2006 (gmt 0)

10+ Year Member



I'm making a "controller" page which takes the value of the "name" variable of a submit button and, based upon the name, it will include the proper content.

I'm having a problem getting the contents of this $_Post index into my trigger variable. Here's the snippet:

if (isset($_POST['submit'])) {
$buttonHit = $_POST['name'];
} else {
$buttonHit = "step1";
}
The input button on this page looks like this:

<input type="submit" name="step2" value="Make Reservation" />

So... when I hit this button, "step2" should be loaded into $buttonHit, but it isn't - only the default choice of "step1". Hummm.

I've done a lot of Googling for the error of my ways, but ever example I see is the same as the syntax that I've written above.

Where am I going wrong?

Neophyte

Anyango

6:01 am on Mar 15, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$buttonHit = $_POST['name'];

is looking for a post variable called "name" but on your form there is no input called "name" rather you have a form input called step2

<input type="submit" name="step2" value="Make Reservation" />

so that "name" input is nowhere arround, thats why it cant find it

maerk

2:35 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



Try using a hidden input:

<input type="hidden" name="name" value="step2" />

Or you could use radio buttons:

<input type="radio" name="name" value="step1" />
<input type="radio" name="name" value="step2" />

Basically, you need an input element of some sort which has a name="name" somewhere -- the value (what's the $_POST variable actually contains) is sent in value="whatever".

phpmaven

3:21 pm on Mar 15, 2006 (gmt 0)

10+ Year Member



I'm not sure exactly what you are trying to accomplish, but I have a few scenarios on my site where I have more than one form "submit" button on a page and take different actions depending on which submit button is clicked.

In that case you could do the following:
<input type="submit" name="reservation" value="Make Reservation">
<input type="submit" name="information" value="Get Information">

if ($_POST['reservation'])) {
$buttonHit = "reservation";
}
if ($_POST['information'])) {
$buttonHit = "information";
}

Only one of the two variables will exist depending on which button was clicked.

neophyte

12:51 am on Mar 16, 2006 (gmt 0)

10+ Year Member



Thanks for everyones input.

phpmaven - you've given me the closest solution to what I'm trying to do; appreciate that.

What I'm really trying to do is to streamline what I've found works in this snippet:

foreach($_POST as $key => $value){
$arr = explode("_", $key);
$buttonHit=$arr[0];
$rowId=$arr[1];
}

I use the above when I have multiple submit buttons on a single page (as you mentioned) which works like a charm.

In the above, I name my submits name="edit_1" for example. "edit" being the buttonhit and "1" being the db table row that will be affected. Works great.

What I'm trying to do NOW is use button name values, (without using the explode) in order in order to detect what button has been hit in order to determine what content to "include", like this:

<?php
if (isset($_POST['submit'])) {
$buttonHit = $_POST['name'];
} else {
$buttonHit = "step1";
}
?>

<?php
include('_includes/header.inc.php');

switch ($buttonHit){

case "step1":
// include the first reservation page.
include('_includes/step1.inc.php');
break;

case "step2":
include ('_includes/step2.inc.php');
break;

case "step3":
// include the detail verification page
include ('_includes/step3.inc.php');
break;
}

case "step4":
// include the "Reservation Competed" page
include ('_includes/step4.inc.php');
break;
}
include('_includes/footer.inc.php');

?>

So, I'm just trying to get the value of whatever "name" is set to.

if an input button's name is defined as "continue" <name="continue"> I don't understand what I'm not doing to simply get the name of the button as I can by using explode.

Any further guideance would be great; failing that I'll just use the stacked "ifs" as suggested.

Neophyte