jcoones Posted October 14, 2007 Share Posted October 14, 2007 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 Quote Link to comment Share on other sites More sharing options...
Fadion Posted October 14, 2007 Share Posted October 14, 2007 $_REQUEST gets the data of get, post, cookies and probably sessions. User superglobals instead for specific information: $_POST, $_GET, $_COOKIE, $_SESSION. Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 14, 2007 Share Posted October 14, 2007 using request is a bad practice just use what you need eg...$_POST, $_GET, $_COOKIE, $_SESSION. Quote Link to comment Share on other sites More sharing options...
jcoones Posted October 14, 2007 Author Share Posted October 14, 2007 Thank you. That worked fine! I still get the "Submit" but I'm sure I can find a way around that. Thanks,... much appreciated. Quote Link to comment Share on other sites More sharing options...
teng84 Posted October 14, 2007 Share Posted October 14, 2007 everything will work fine when you use request in fact that is easier but like i said bad practice it might cause some problem in the future you might get hack etc.... Quote Link to comment Share on other sites More sharing options...
prime Posted October 15, 2007 Share Posted October 15, 2007 $_request is actualy being removed from php6 I think its actualy turned off by default in version 5 not sure havn't actualy trie to use it in anything past php4 Quote Link to comment Share on other sites More sharing options...
jcoones Posted October 15, 2007 Author Share Posted October 15, 2007 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". Quote Link to comment Share on other sites More sharing options...
prime Posted October 15, 2007 Share Posted October 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
jcoones Posted October 15, 2007 Author Share Posted October 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
mattal999 Posted October 15, 2007 Share Posted October 15, 2007 after posting the information and at the end of your code, put: die(); that will get rid of the rest of the information after the word die();, even html. Quote Link to comment Share on other sites More sharing options...
jcoones Posted October 16, 2007 Author Share Posted October 16, 2007 Thanks mattal999. Good tip! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.