Forum Moderators: coopster

Message Too Old, No Replies

passing arguments via submit button

         

neophyte

6:46 am on Jan 2, 2005 (gmt 0)

10+ Year Member



I've got a submit button that's named Update_$aRowID. It looks like this:

print"<td align='center'><input type=submit name=Update_$aRowId value=Edit></td>";

--------------

When I click this button, another page is loaded. In this second page, I use the explode function to pick apart the $_POST array like this:

foreach($_POST as $key => $val){
$arr = explode("_", $key);

$buttonHit=$arr[0];
$rowId=$arr[1];
}

--------------

When I do a test print to make sure my array elements are there...

print "Button Hit is $buttonHit, Row Id is $rowId";

... everything is fine: "Button hit is Update, Row Id is 13

--------------

BUT, now I want to pass one more argument for a table name. So I alter my button name to read:

<input type=submit name=Update_$aRowId_$aTable value=Edit>

NOTE: "article" is the value within $aTable.

The explode function now looks like this:

foreach($_POST as $key => $val){
$arr = explode("_", $key);

$buttonHit=$arr[0];
$rowId=$arr[1];
$table=$arr[2];
}

--------------

When I test this with the following print statement:

"Button Hit is $buttonHit, Row Id is $rowId", and Table affected is $table";

Here's what I get:

Button Hit is Update, Row Id is article, Table is

Huh?

--------------

The first value "Update" is correct, the second value should be a row number (but, instead, it's "article"), and the third value should be "article", but nothing shows up at all.

Beating my head against the wall on this. What am I doing wrong? I looked in the php manual and explode can use a whole bunch of string elements, so it's obviously me (what a surprise).

Can someone please tell me how to make this work?

Neophyte

dreamcatcher

9:24 am on Jan 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi neophyte,

Can you not just send the variables in hidden form fields? Would make things a lot easier.

dc

Salsa

9:51 am on Jan 2, 2005 (gmt 0)

10+ Year Member



I'd probably use hidden fields like dreamcatcher suggested, but then you'll always wonder why your current method didn't work. Have you looked at the html source code of your form to see if the expected name is in the button? I think you'll spot the problem there. Because an underscore is a valid character in a variable name, when you conststruct "Update_$aRowId_$aTable", the variable you are including is not $aRowId as you suspect, but $aRowId_, which doesn't exist. So, in effect, you are printing "Update_$aTable" because there is no value in $aRowId_ . To get the effect that you want, try concatening the two variables like this:

...name=Update_$aRowId."_".$aTable value=...

I hope this helps.

PS: When developing, turn your error reporting up to

error_reporting(E_ALL);
and you will get undefined variable notices to help you spot problems like this.