By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,222 Members | 1,730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,222 IT Pros & Developers. It's quick & easy.

can I use arrays in forms? with a string index?

P: n/a
I've waited 6 weeks for an answer to my other question and still no
luck, so let me rephrase the question. I know I can do this:

<form method="post" action="$self">
<input type="text" name="filesToDelete[]">
<input type="text" name="filesToDelete[]">
<input type="text" name="filesToDelete[]">
</form>
On the recieving end, I'll get an array called $filesToDelete, and
then I can loop through to get all the ids of the files that need to
be deleted.

But is there anyway to build an array in a form and get a string
index? I've tried this, and I seem to get an array, but I'm unable to
get any info out of it, neither through extract() nor direct use of
the string index:
<form method="post" action="$self">
<input type="text" name="userInfo['name']">
<input type="text" name="userInfo['email']">
<input type="text" name="userInfo['password']">
</form>
Any help?
Jul 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 5 Jul 2003 11:09:56 -0700, lk******@geocities.com (lawrence) wrote:
I've waited 6 weeks for an answer to my other question and still no
luck, so let me rephrase the question. I know I can do this:

<form method="post" action="$self">
<input type="text" name="filesToDelete[]">
<input type="text" name="filesToDelete[]">
<input type="text" name="filesToDelete[]">
</form>
On the recieving end, I'll get an array called $filesToDelete, and
then I can loop through to get all the ids of the files that need to
be deleted.

But is there anyway to build an array in a form and get a string
index? I've tried this, and I seem to get an array, but I'm unable to
get any info out of it, neither through extract() nor direct use of
the string index:
<form method="post" action="$self">
<input type="text" name="userInfo['name']">
<input type="text" name="userInfo['email']">
<input type="text" name="userInfo['password']">
</form>


So close.

Just don't put the quotes in there.

<form method="post" action="">
<input type="text" name="userInfo[name]">
<input type="text" name="userInfo[email]">
<input type="text" name="userInfo[password]">
<input type="submit">
</form>
<pre>
<?php
var_dump($_POST);
?>
</pre>

Submit a,b,c and get:

array(1) {
["userInfo"]=>
array(3) {
["name"]=>
string(1) "a"
["email"]=>
string(1) "b"
["password"]=>
string(1) "c"
}
}

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #2

P: n/a
Andy Hassall <an**@andyh.co.uk> wrote in message
So close.

Just don't put the quotes in there.

<form method="post" action="">
<input type="text" name="userInfo[name]">
<input type="text" name="userInfo[email]">
<input type="text" name="userInfo[password]">
<input type="submit">
</form>
<pre>
<?php
var_dump($_POST);
?>
</pre>

Submit a,b,c and get:

array(1) {
["userInfo"]=>
array(3) {
["name"]=>
string(1) "a"
["email"]=>
string(1) "b"
["password"]=>
string(1) "c"
}
}

Thank you. However, that syntax is considered incorrect, yes? I may
use it for now, but it may not work in the future, yes?

There is all this, on this page:
http://us3.php.net/types.array

Array do's and don'ts

Why is $foo[bar] wrong?

You should always use quotes around an associative array index. For
example, use $foo['bar'] and not $foo[bar]. But why is $foo[bar]
wrong? You might have seen the following syntax in old scripts:
<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>

This is wrong, but it works. Then, why is it wrong? The reason is that
this code has an undefined constant (bar) rather than a string ('bar'
- notice the quotes), and PHP may in future define constants which,
unfortunately for your code, have the same name. It works, because the
undefined constant gets converted to a string of the same name
automatically for backward compatibility reasons.
Jul 16 '05 #3

P: n/a
On 6 Jul 2003 12:15:24 -0700, lk******@geocities.com (lawrence) wrote:
Andy Hassall <an**@andyh.co.uk> wrote in message
So close.

Just don't put the quotes in there.

<form method="post" action="">
<input type="text" name="userInfo[name]">
Thank you. However, that syntax is considered incorrect, yes? I may
use it for now, but it may not work in the future, yes?


No, it's fine for HTML.
There is all this, on this page:

http://us3.php.net/types.array

Array do's and don'ts

Why is $foo[bar] wrong?


That syntax is incorrect within PHP. The syntax I'm referring to is within the
form in HTML. Using quotes there is incorrect.

It's somewhat similar to how you use array indexes within double quotes.

$a = $b['x']; // right
$a = $b[x]; // wrong

$a = "$b['x']"; // wrong
$a = "$b[x]"; // right

http://www.php.net/manual/en/languag...parsing.simple

But remember it's not really the same situation - the stuff you write in HTML
is being parsed by different code to the stuff you're writing in PHP.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #4

P: n/a
On 7 Jul 2003 10:18:14 -0700, lk******@geocities.com (lawrence) wrote:
Andy Hassall <an**@andyh.co.uk> wrote in message
But remember it's not really the same situation - the stuff you write in HTML
is being parsed by different code to the stuff you're writing in PHP.
Good to be reminded of that. What you're saying is that if an HTML form submits

<input type="text" name="for[bar]">


ITYM name="foo[bar]"
then PHP will turn that into:

$foo["bar"]

yes?


Yes.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #5

P: n/a
Andy Hassall <an**@andyh.co.uk> wrote in message news:<9m********************************@4ax.com>. ..
On 7 Jul 2003 10:18:14 -0700, lk******@geocities.com (lawrence) wrote:
Andy Hassall <an**@andyh.co.uk> wrote in message
But remember it's not really the same situation - the stuff you write in HTML
is being parsed by different code to the stuff you're writing in PHP.


Good to be reminded of that. What you're saying is that if an HTML form submits

<input type="text" name="for[bar]">


ITYM name="foo[bar]"
then PHP will turn that into:

$foo["bar"]

yes?


Yes.

Good to know. Thanks a million.
Jul 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.