Page 1 of 1

Retrive form control array

Posted: Tue May 16, 2006 5:23 pm
by tfcRobert
As I understand, if there are a number of controls (Text controls in this case) with the same name, then on a POST the controls are sent as an array.

How do I access this array?

I'm using the code from "PHP and MySQL" to access the posted variables like:

$myVar = script_param(myVarName);


If I submit the form with a GET and examine resulting URL with appended control info, the duplicated names are simply duplicate in the return data.

I can, of course, just append an index to each control set name, myCtl1, myCtl2 etc. but the automatic array solution seems more elegant.

Please point me in the right direction?

Rob

Posted: Tue May 16, 2006 5:30 pm
by Burrito
some languages work that way...unfortunately php is not one of them.

php simply overwrites the previous values with the last instance of the form element.

you can however append '[]' to your field names to create an array and acheive what you're after.

ex:

Code: Select all

<input type="checkbox" name="checks[]" value="1">
<input type="checkbox" name="checks[]" value="2">
<input type="checkbox" name="checks[]" value="3">

It works!

Posted: Tue May 16, 2006 6:32 pm
by tfcRobert
That was not intuitive, at least to me, but it works.

I now have several text controls now with

Code: Select all

<INPUT TYPE="text" NAME=mList[] ....
<INPUT TYPE="text" NAME=mList[] ....
<INPUT TYPE="text" NAME=mList[] ....
then in the PHP code handling the POST:

$listings = script_param("mList");

returns a zero based array with all my data.

Thanks for the tip.

Rob

Posted: Tue May 16, 2006 7:58 pm
by RobertGonzalez
Are you coming from an ASP background?

Posted: Tue May 16, 2006 8:35 pm
by tfcRobert
Everah wrote:Are you coming from an ASP background?
Nope. Actually I was more of an embedded systems guy on micros, but more recently started a personal chef/catering business and I'm using PHP-MySQL both for our web site and for business internals.

I started writing a full application in Java for my business utility functions but I'm interested in using the browser for those functions.

I appreciate the help on this problem and hope it helps someone else.

Rob

Posted: Tue May 16, 2006 8:51 pm
by bdlang
tfcRobert wrote: I started writing a full application in Java for my business utility functions but I'm interested in using the browser for those functions.
Just out of curiosity, was there a reason you didn't go towards servlets / JSP for your application?

Posted: Tue May 16, 2006 9:11 pm
by tfcRobert
I had no knowledge of JSP but did have some limited experience with PHP-MySQL.

My web server supports that combo
I've already got many $$ worth of reference books on PHP and MySQL
It's a pretty standard setup with Apache for testing on my local machine.
I'm too old to learn yet another methodology...
We'll if not too old, then I just don't want to send more money to Amazon...

But... Why should I look at JSP? :D

Rob

Posted: Tue May 16, 2006 10:51 pm
by bdlang
tfcRobert wrote:I had no knowledge of JSP but did have some limited experience with PHP-MySQL.
My web server supports that combo
I've already got many $$ worth of reference books on PHP and MySQL
It's a pretty standard setup with Apache for testing on my local machine.
I'm too old to learn yet another methodology...
We'll if not too old, then I just don't want to send more money to Amazon...
I can appreciate that. If it ain't broke, don't fix it. :)

tfcRobert wrote:But... Why should I look at JSP? :D
Well, if you initially started writing it in Java, I thought perhaps you might have had a certain experience level with it, to the point where you might easily pick up the JSP methodology. I started with C and other similarly structured languages many years ago and PHP was a pretty natural progression given it's syntax, it just 'feels' like C alot of the time. Now of course, with OOP structure in PHP > v5, some of the language 'feels' like Java (IMHO). I have recently gone from PHP to JSP because our business wanted to collaborate with a JSP developer and the specific application they want to run is J2EE/JSP.

At any rate, building an application with servlets / JSP / JavaBeans is pretty much like assembling a set of Java classes to interact with each other. Servlet -> JavaBean -> JSP script. Classic MVC pattern. Sooo, if you're comfortable with Java structure and classes you might adjust to it rather quickly.

Posted: Wed May 17, 2006 9:42 am
by tfcRobert
OK. You got me. I'm comfortable in several OOP environments and used PHP because it was there on my shelves but now I'll have to trot off to Amazon!

What's a good reference on JSP?

Rob

Posted: Wed May 17, 2006 12:37 pm
by bdlang
I'm familiar with O'Reilly's JavaServer Pages, and for the most part I don't think you can go wrong with their references. You might take a look at it on O'Reilly's safari site, you can check out up to 10 books free for 14 days (I believe you sign up as a regular subscriber and then you can cancel after before you trial period runs out, in any event it's $19. a month, so it's not too bad).

Posted: Wed May 17, 2006 12:43 pm
by RobertGonzalez
tfcRobert wrote:But... Why should I look at JSP?
For the same reason you looked at PHP. Or at any other language for that matter. Although, I gotta say, PHP, for me, was such an easy language to learn. I had absolutely no programming background at all, save for the HTML I learned from a magazine years ago. After reading though my first tutorial I built my very first script that interacted with a database, accepted user input, created and used cookies and a host of other things. I was amazed at how easy it was to learn it. I have yet to find a language that compares to the ease of use of PHP.

I know my case is different, seeing as I had no programming background, but for me PHP was clearly the right choice. For others it could be .NET, Java, ColdFusion, Ruby, Python, the list goes on and on. I think it is more a matter of preference, availability of support services and cost of applying your development (LAMP vs Microsoft server setups, for example) that could sway you one way or the other.

This is just my $0.02.