473,395 Members | 1,595 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,395 software developers and data experts.

Problems with form processing: array_key_exists() and while (list($k,$v) = each($arr))

Hello,

I have an HTML form I'd like to process.

<select name="items[]" multiple>
<option value="doughnuts">Hot Doughnuts</option>
<option value="coffee">Hot Brewed Coffee</option>
<option value="tea">Hot Tea</option>
<option value="cake">Sponge Cake</option>
<option value="chips">Hot Chips</option>
</select>

The problem I have it where I follow the Programming PHP book:

// Should the 's' be the name of the select tag, or the name of
// the submit button?

if (array_key_exists( 's', $_POST ))
{
$selected = "User selected these options:\n\n";

while (list($k, $v) = each($_POST['items']))
{
// Is $v "doughnuts" or "Hot Doughnuts"?
$selected .= "$v\n"
}

// ...
}

Thanks for your help.

--
Spartacus
Jun 19 '07 #1
6 2836
Spartacus wrote:
// Should the 's' be the name of the select tag, or the name of
// the submit button?
Either.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 115 days, 22:05.]

dict, thes & ency
http://tobyinkster.co.uk/blog/2007/0...ict-thes-ency/
Jun 19 '07 #2
Toby A Inkster wrote:
Spartacus wrote:
>// Should the 's' be the name of the select tag, or the name of
// the submit button?

Either.
Were there any other potential problems with the code I posted? I
presume not otherwise you'd have said so. Thanks for your help. I'll use
the select tag name instead since PHP is complaining that it's not an array.

-Spartacus
Jun 20 '07 #3
Spartacus wrote:
Were there any other potential problems with the code I posted? I
presume not otherwise you'd have said so.
Not that I noticed. And $v will be "doughnuts", not "Hot Doughnuts".

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 11:23.]

dict, thes & ency
http://tobyinkster.co.uk/blog/2007/0...ict-thes-ency/
Jun 21 '07 #4
Toby A Inkster wrote:
Spartacus wrote:
>Were there any other potential problems with the code I posted? I
presume not otherwise you'd have said so.

Not that I noticed. And $v will be "doughnuts", not "Hot Doughnuts".
Thanks Toby very much. I've solved the problem by changing to:

array_key_exists( 'moreof', $_POST );

which is the name of my select tag.

I've also changed the value="" in the option tags to be the same as the
key, just to verify it works.

I was getting odd errors even when no such keys existed. I had changed
them all and still I was getting my old ones. Maybe my stupid browser
cached them or something. So I renamed the HTML form and the PHP script
and it worked just fine.

Again thanks for your help.

--
Spartacus
Jun 21 '07 #5
Spartacus wrote:
I've also changed the value="" in the option tags to be the same as the
key, just to verify it works.
This:

<option value="Foo">Foo</option>

can be written as this:

<option>Foo</option>

That is, when the value attribute is missing, the option's text content is
used instead.

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 18:36.]

dict, thes & ency
http://tobyinkster.co.uk/blog/2007/0...ict-thes-ency/
Jun 21 '07 #6
Spartacus wrote:
while (list($k, $v) = each($_POST['items']))
By the way, most people would say that the following is more readable:

foreach ($_POST['items'] as $k=>$v)

and it's functionally equivalent. (Indeed, after parsing I believe PHP
treats them exactly the same.)

--
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 2.6.12-12mdksmp, up 18:38.]

dict, thes & ency
http://tobyinkster.co.uk/blog/2007/0...ict-thes-ency/
Jun 21 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: leo | last post by:
Are there open source php form processing programs? I know there are free php form mail code, but if it's classified as open source, I would think any bugs can be fixed much faster. If not, then...
8
by: dmcconkey | last post by:
Hi folks, I have a client with four websites. Each site has a contact form that is identical. They all have "required" fields validated through a JavaScript onSubmit() function. Upon validation,...
5
by: WmGill | last post by:
I am experimenting with Python, and want to convert some of my PHP scripts to python cgi scripts. In PHP I use PHP_SELF for the action target. This way I can "reuse" data in fields (i.e. if the...
2
by: Scott | last post by:
Hi, I hope someone might be able to set me straight on how to handle the situation described below. I have a page that has an email field for user input and 2 radio buttons (subscribe +...
3
by: Robert Mark Bram | last post by:
Hi All! Let's say I have a form like this: <form action="handler.asp"> ... </form> Within handler.asp, I output a number of named anchors: <a name="important1">Important 1</a> ... <a...
1
by: John | last post by:
Hi I want to make a form that user can fill in and submit. I previously processed the form post in an asp script and sent the entries as email to a recipient using cdo. How does the form...
5
by: stephenburgess1972 | last post by:
Would C++ be a good choice for programming server-side applications for form processing? Or is there a better choice out there? Thanks.
3
by: raj chahal | last post by:
Hi there i have created a registration page containing a form than sends username password to an asp processing page. If the user exists it sends the user back to the registration page with...
4
by: zihuz86 | last post by:
I need help on php form processing for example : Menu no. : list_menu1 Menu name : list_menu2 Table : Menu
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.