Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable name on form submit

Status
Not open for further replies.

jgd12345

Technical User
Apr 22, 2003
124
GB
Hi, I've got an text box:

<input type="text" name="drawGameBoards[0].numbersChosen[0]" class="text">

If I pass this variable across using a php script then what would it be called. My instincts were that it would be $drawGameBoards[0].numbersChosen[0] but that returns a parse error.

I'd be greatful if anyone could help. Thanks
 
Assuming that the method of the form in which the input appears is "POST", then the value is probably available in

$_POST['drawGameBoards'][0]


The "." you are using as a JavaScript- or VBScript-style object hierarchy separator is only used in PHP as a string contatenation operator. So PHP probably will ignore everything after the ".".


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yeah the method is post.

That hasn't helped too much though because I have two fields which would give the same variable name like that, example below:

<input type="text" name="drawGameBoards[0].numbersChosen[0]" class="text">

<input type="text" name="drawGameBoards[0].numbersChosen[1]" class="text">

It's important that I keep these names for the input boxes because otherwise the form would not work. Would it be possible me to have an event (using javascript) which would set a whole lot of new variables with proper php variables names equal to all those weird ones in the form.

Hope you understand if so how would I make sure they pass back to where the information is collected.
 
Don't try to create some kind of object in your variable-naming. Think in terms of arrays, not object

This will work:

<input type="text" name="drawGameBoards[0][numbersChosen][0]" class="text">

And the value will be in $_POST['drawGameBoards'][0]['numbersChosen'][0].




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Hi, Cheers I replaced all the object names with arrays. When I tried echo $_POST['drawGameBoards'][0]['numbersChosen'][0]; after submitting the variable was blank even though I put text in the text box.
 
I'm not sure what to tell you.

On my system I have the following HTML page:

Code:
<html><body>
	<form method="post" action="show_post.php">
		<input type="text" name="drawGameBoards[0][numbersChosen][0]" class="text">
		<input type="submit">
	</form>
</body></html>

The script show_post.php (to which the form above submits) consists of:

Code:
<?php
print '<pre>';
print_r ($_POST);
?>

When I point my browser to the HTML page, enter a in the text field, and submit, the script returns the following:

Code:
Array
(
    [drawGameBoards] => Array
        (
            [0] => Array
                (
                    [numbersChosen] => Array
                        (
                            [0] => a
                        )

                )

        )

)


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Hi thanks for your help already but it still does not work for me. I've tried creating a test.php file with the following contents:

<?php
if(!$submit) {
?>

<html>
<form method="post" action="test.php">
<input type="text" name="drawGameBoards[0]numbersChosen[0]">
<input type="text" name="drawGameBoards[0]numbersChosen[1]">
<input type="text" name="drawGameBoards[0]numbersChosen[2]">
<input type="text" name="drawGameBoards[1]numbersChosen[0]">
<input type="text" name="drawGameBoards[1]numbersChosen[1]">
<input type="text" name="drawGameBoards[1]numbersChosen[2]">
<input type="submit" name="submit" value="submit">
</form>
</html>

<?php
} else {
print '<pre>';
print_r ($_POST);
}

?>

But when I goto enter some values and click submit I get the following output:

Array
(
[drawGameBoards] => Array
(
[0] => 3
[1] => 6
)

[submit] => submit
)

I donno if this is of any use to you but my current version of PHP is 4.3.6.

I'd be greatful for your help as this is driving me nuts. Thanks
 
You're still thinking in terms of objects. You need to think in terms of a multidimentional array.

Look at your field name:

drawGameBoards[0]numbersChosen[0]

Look at my form field name. It's all array elements:

drawGameBoards[0][red][[/red]numbersChosen[red]][/red][0]




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top