Forum Moderators: coopster

Message Too Old, No Replies

Determining Array Index within a $ Post

         

neophyte

4:58 am on Nov 20, 2007 (gmt 0)

10+ Year Member



Hello All -

When navigating from one page to the next from within a section of form pages, I've found a way that works, without using hidden inputs, but (I fear) is terribly inefficient.

It goes like this:

SUBMIT INPUT:

<form>

... bunch of form elements
...

<input
type="submit"
name="go=client,secure"
value="Cancel"
class="submitRed w75"
/>

<input
type="submit"
name="go=client,app1"
value="Next &gt;"
class="submitGreen w75"
/>

</form>

When a user submits the form, the keys for all submitted $_Post indexes are looped-through to see if one of them contains a "=".

If one of the keys does contain "=", it's exploded into section ($sec) and page ($pag) vars (See snippet below)

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
foreach($_POST as $key => $value)
{
if(strpos($key, '='))
{
$arr = explode('=', $key);
$sec = $arr[0];
$pag = $arr[1];
}
}
}

Using the $sec and $pag vars, the correct section and page content is then to the screen.

Now, this DOES work - and I'm kinda proud of it - but, due to the fact that each and every element in the $_Post array is being looped through (and sometimes there are lots and lots of $_Post elements) I'm trying to think of a better (more programatically efficient) way to do the same thing without a loop.

I've throught of trying to test for the "Submit" input TYPE, but don't know how to do this... or even if the "Type" of an input element is "posted" - it doesn't seem to be. I've also tried to set the names of the buttons to simply "submit", (so all that I'd have to do is get the value of $_POST['submit']) but then I'd have to put the $sec and $pag values into the button "value" which would, of course, cause a massive usability issue.

Does anyone else out there use this type of form navigation technique - without hidden inputs or loops?

Neophyte

joelgreen

1:35 pm on Nov 20, 2007 (gmt 0)

10+ Year Member



Why don't you have few submit buttons and check which one clicked?

<input type="submit" name="submit1" value="Button label Here">
<input type="submit" name="submit2" value="Button label Here">

php

if (isset($_POST['submit1'])) {
// some action
}
else if (isset($_POST['submit2'])) {
// something else
}

I believe only clicked button value will be submitted. So if user clicked submit1 then submit2 will not be passed to php