472,960 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,960 software developers and data experts.

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

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
5 16093
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
4
by: meltedown | last post by:
Theres something very basic about javascript arrays I'm missing. The value of unit.value is 17.00 and value of the qty.value is 5 and I put these values into an array: myarray.value]=qty.value;...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
5
by: Trond | last post by:
I have a webservice that is returning a dataset. In dataset there is a table with 4 columns. (LDate, LTime, LDepth and ServiceNumber) I then try to read only 2 of the columns into array (double...
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
5
by: Michal Táborský | last post by:
I am wondering, if it's effective to use text arrays to store multilanguage information. We used to do it like this: CREATE TABLE product ( id serial NOT NULL, price float4, ... )
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.