Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > PHP
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 03-06-2008, 02:38 PM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
metazai is on a distinguished road
generic form mailer

All the form processors I've ever created with PHP have used $_POST or $_GET or $_REQUEST along with the specific variable names tied to the form elements. Is there a way to build a generic PHP Form processor (and mailer) that just reads everything sent over, variable names, values, and all? The idea is that it would be an ugly but flexible script that I wouldn't have to match up to the form elements by hand in the code every time.

I'm not looking for the code, just a shove in the right direction. Any ideas would be appreciated.
__________________
metazai is offline   Reply With Quote
Old 03-07-2008, 08:52 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
$_REQUEST contains everything in $_GET and $_POST.

so, to read every variable sent over, you would just need to loop through the $_REQUEST array.

never tried to make one myself, but you could take the drupal approach. they define forms in arrays. then there's a separate 'validate' handler function, and then a 'submit' handler.

here's an example of a form element defined in drupal:
PHP Code:
$form['value1'] = array(
      
'#type' => 'textfield',
      
'#title' => 'Value 1',
      
'#size' => 4,
      
'#maxlength' => 4,
      
'#description' => 'The first value to perform the multiplication with.'
); 
so you could take a structure like that and dynamically create your form. then, you could loop through the request vars to validate and submit. how to make that really generic may be tricky.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 03-07-2008, 08:57 AM   #3 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
metazai is on a distinguished road
hmmmm

So, are you saying that $_REQUEST itself is an associative array? (Guess that would explain the square brackets)

That makes the problem a little easier. Don't know why I didn't think of it.
__________________
metazai is offline   Reply With Quote
Old 03-07-2008, 09:09 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
yes it is. throw this at the top of your code to test it out.
PHP Code:
<?php
if (is_array($_REQUEST)) {
  foreach (
$_REQUEST as $key => $value) {
    echo 
$key ' = ' $value '<br />';
  }
}
?>
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 03-07-2008, 10:23 AM   #5 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 635
DJMaze is on a distinguished road
Ehm, $_REQUEST also contains cookies
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 03-07-2008, 10:27 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
good point djm, didn't think about that.

$_POST is an associative array to so you could just use that.. or do 2 loops.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 03-08-2008, 02:02 PM   #7 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
metazai is on a distinguished road
What is the implication of the "cookies" thing?
__________________
metazai is offline   Reply With Quote
Old 03-08-2008, 05:39 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,397
sde is on a distinguished road
you just need to consider that you may be reading cookie data and you don't really have any way to tell if the data is coming from your form, or from cookies.
__________________
testing 1 2 3
sde is offline   Reply With Quote
Old 03-08-2008, 05:48 PM   #9 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
metazai is on a distinguished road
Got it . . .thanks, both of you!
__________________
metazai is offline   Reply With Quote
Old 03-09-2008, 11:03 PM   #10 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 120
metazai is on a distinguished road
One other question, though . . .how can you avoid that cookie problem, other than using $_POST? I started using $_REQUEST to read my form data because I had been led to believe it was better that $_POST or $_REQUEST. Is there a way to check if the data being read by $_REQUEST isn't cookie-d or cached? sde, what did you mean by "2 loops"?
__________________
metazai is offline   Reply With Quote
Old 03-10-2008, 08:43 AM   #11 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 635
DJMaze is on a distinguished road
$_REQUEST is bad, just for questions you ask already!

Always only use $_POST methods, because $_GET fails if the URI in IE is more then 2048 bytes.

Also this way you can properly distinguish form mode settings ($_GET stuff in action="") and the form data ($_POST contains form fields)
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Child Form to Parent Form WmFenn MS Technologies ( ASP, VB, C#, .NET ) 2 07-20-2005 08:10 AM
Generate query automatically from form phase PHP 1 04-06-2005 01:11 AM
Passing Values from Popup Form to Main Form chrislopezz PHP 7 03-28-2005 12:45 PM
EMERGENCY: Dynamic form processing DavH27 PHP 8 10-27-2004 07:52 PM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 10:18 AM


All times are GMT -8. The time now is 08:06 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle