Wrox Programmer Forums
|
BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5
This is the forum to discuss the Wrox book Beginning PHP4 by Wankyu Choi, Allan Kent, Chris Lea, Ganesh Prasad, Chris Ullman; ISBN: 9780764543647
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 21st, 2003, 12:29 AM
Registered User
 
Join Date: Nov 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dealing with long form php

Hi,
Probably a real newbie question, but when writing a php script to handle a long form (over 40 fields) does it make sense to gather each field individually:
$myFirstname = $_POST['firstname'];
$myLastname = $_POST['lastname'];
etc...?

Or is there a common shorthand that would let you gather all form data into an array, validate the fields, format each of the fields(addslashes), and then enter them all into a database?

Cause, so far the code feels really clunky - I'm cutting and pasting all my field name for each operation.

Thanks,
Cottager

The best ideas are not the simple ones, they're the complex ones made easy.
 
Old November 21st, 2003, 01:00 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's exactly how I do it, actually. Read my tip on using arrays to organize data. It's pretty old, but I still stick to what I said back then:

  http://p2p.wrox.com/archive/beginnin...2002-08/52.asp

I should really repost this on the NEW p2p site so that forum members unfamiliar with the "old style" archives don't get all confused.


Take care,

Nik
http://www.bigaction.org/
 
Old November 21st, 2003, 01:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I should add that your form input names can be taken directly from the database tables you plan on inserting the form data into. You don't hard-code your input field names, so that if you change your database schema, your form is automatically aware of the change. This also lets you generate your INSERT and UPDATE queries by using your form input names. For example:

$add_slashes = (0 == get_magic_quotes_gpc());

foreach ($_POST['some_form'] as $input_name => $input_value)
{
    $columns[] = $input_name;
    $values[] = "'" . ($add_slashes? addslashes($input_value) : $input_value) . "'";
}

$colstr = join(', ', $columns);
$valstr = join(', ', $values);

$query = "INSERT INTO table_name ({$colstr}) VALUES ({$valstr})";



Keep in mind this works for simple schemas where each form's inputs corresponds with the columns in a single table. If you're going to have to insert into multiple tables (or assemble the form from multiple tables), it kind of falls apart.

For this reason, though, I recommend using wrapper functions to encapsulate this functionality -- have a function generate your column names. Have a function perform the insert queries. That way, should your schema become more complicated (read: multiple tables), you can just modify the contents of those functions and the rest of your script doesn't have to know or care about those changes. It still calls the function.



Take care,

Nik
http://www.bigaction.org/
 
Old November 25th, 2003, 12:28 PM
Registered User
 
Join Date: Nov 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is awesome! Thanks for the great info.
But I've got a "part two": I have a string which is assembled from checkboxes using join(). Before I changed my form - putting all form fields into one array, my long list of checkboxes was an array:
<input type="checkbox" name="included[]" value="monitor">
<input type="checkbox" name="included[]" value="keyboard"> ...etc.

Now I'm trying to make everything go into one array myform[].

I'm trying things like
<input type="checkbox" name="myform[included[]]" value="monitor"> etc.

Is it possible to do something like this? Or am I wasting my time, find another way?

Thanks again for all the help!
Mike


The best ideas are not the simple ones, they're the complex ones made easy.
 
Old November 25th, 2003, 01:31 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It's definitely possible. Take a close look at the array you're trying to create, though:

  myform[included[]]

You're creating two separate arrays, each with just one level of nesting. The value of included[] becomes the index of the array "myform". This doesn't make sense because included[] is guaranteed to be empty, since the empty-bracket notation "[]" just appends to the end of an array.

You want to do this:

myform[included][]

See, "included" is the first-level index of myform. The value stored in that index is another array storing your included peripherals (keyboard, mouse, etc).

In other words,

$myform is an array. "included" is a string index in that array. The value of stored at $myform["included"] is an array.

When dealing with arrays, it's very very very useful to use the print_r() function to see what's stored in the array and how it's structured.

echo "<pre>";
echo "myform is: ";
print_r($_POST['myform']); // or $_GET, depending on your form method.
echo "</pre>";



Take care,

Nik
http://www.bigaction.org/
 
Old November 25th, 2003, 01:37 PM
Registered User
 
Join Date: Nov 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Nik! Another headache gone! You work way better than asprin!
Makes perfect sense too.

The best ideas are not the simple ones, they're the complex ones made easy.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Long Long int to bin walid C# 0 January 23rd, 2007 12:47 PM
add long control to web form manasic ASP.NET 2.0 Basics 1 July 5th, 2006 11:17 PM
dealing with concurrency using web form insert method SQL Server 2000 1 May 23rd, 2005 04:09 PM
string to "long long" without using atoll sarraju C++ Programming 2 August 4th, 2004 07:19 AM
Modifying long text in a long field sajsal Classic ASP Databases 1 February 20th, 2004 12:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.