Jump to content

Using the $REQUEST with forms


jcoones

Recommended Posts

I, who like everyone else at one time or another, am just beginning to learn php.  I find, at least for me, that the easiest way to learn is to write little scripts and learn from that. 

 

Having said that, I wrote a small html "contact us" form and a processor script to retrieve the information from the form.  That worked fine, however, that was because I knew exactly what fields were in the form.

 

I wanted to figure out if I could create a generic script to handle any form.  That, of course, means that I would not necessarily know how many fields any given form might have. I found that php created an associative array for the form fields.  The following is the simple html form code that I used for testing.

 

---------------------------------------------------------

 

<html>

<title>Form Processor Test</title>

<form method="post" action="readform.php">

Username : <input type="text" name="username" /><br />

Email : <input type="text" name="email" /></br>

Phone : <input type="text" name="phone" /><br /><br>

<input type="submit" name="button" value="Submit" />

</form>

</html>

 

---------------------------------------------------------

 

And the following is the php code I tried.

 

---------------------------------------------------------

 

<?php

 

foreach($_REQUEST as $field => $value){

print <<<HERE

$field :  $value

HERE;

}

?>

 

---------------------------------------------------------

 

When I run the script, what I get is the following:

 

---------------------------------------------------------

 

username : Tom Jones

email : tjones@gmail,com

phone : 123 456-7890

button : Submit

skinadmin : pyrolight

skin : pyrolight

style : Small

SESS701061c2a9cae4f927bc79247ecce216 : 122bfceb8974640f48d2b4000d64dc0e

 

---------------------------------------------------------

 

I get the data from the fields but I don't know what the other five lines of garbage is.  Is it reading a cookie?  How would I code it to just get whatever fields are in the form and not the other garbage?

 

Any help appreciated.

 

Thanks

Link to comment
Share on other sites

It does work in php5.  The problem for me was that it also pulled the $_COOKIE, $_SESSION, and me being new to php, I didn't realize this when I used $_REQUEST.  Using the superglobal $_POST I get the Username, Email, Phone and Submit without the other baggage.  Now I just got to figure out how to print everything but the "Submit".

 

 

Link to comment
Share on other sites

just assign each to a variable at the start of your script such as

 

$username = $_POST['username'];
$email = $_POST['email'];
$phone= $_POST['phone'];

 

there further down do whatever you want with them for example if you want it emailed to you

 

echo them out pass them to the mail funtion or whatever

 

to echo them out after that just type say

 

echo "

Username: $username
<br /><br />
Email: $email
<br /><br />
Phone: $phone


";

 

that way your only getting the data you actualy want

 

 

Link to comment
Share on other sites

Thanks for the help prime.

 

I can see where that would work as long as I know that the form is using the fields Username:, Email:, and Phone:,   Those just happen to be the fields that I used in my test script, but what I am trying to do is to create a generic form processor that will handle any form, regardless of how many fields it may have.   In this case, I would not know the names of the fields being used in any particular form so I wouldn't know what names to assign them to.

 

I don't kknow if I am explaining this clearly but,... lets say a specific form has 8 fields. Each field has a name and a value but I don't know what they are.  How could I assign them? 

 

To get rid of the "Submit" when printing or emailing the form data, I guess I could use something like this:

 

foreach($_POST as $field => $value){

//Print everything except the "Submit" button press.

if ($value != "Submit"){

print <<<HERE

$field :  $value

<br>

HERE;

}

}

 

But that is a bit of a kludge and I thought there was a better, more efficient way to do it.

Thanks again for your help, it really is appreciated.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.