473,415 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,415 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 16126
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.