SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get name and value from a form and echo it

    Hi All

    I have the following code which is great for taking all the information from a form and assign it to a variable the same name as the name on the field.

    Please feel free to use it if you want.

    PHP Code:
    foreach($_POST as $fieldname => $value){ $$fieldname=$value; } 
    I have created a tool to build a form in the admin area and then have it show on the website. I need to get all the information from the form and email it.

    The problem I am having is that I have no idea what the form will consist of and how many fields there are on it.

    I have spent quite a bit of time trying to work this out and I think the answer lays in the code above but need help.

    Basically I want to take the name field in the form and the value added to it and echo it. E.g. If the form name was name=’Address’ I would echo Address: 123 Whatever Street.

    Hope this makes sense.

    Any help would be great.

    Thanks

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $body "Your form was submitted:\n\n";

    foreach (
    $_POST as $fieldname => $value) {
      
    $body .= $fieldname ": " $value "\n";
    }

    mail("you@example.com""Form Submitted"$body"From: you@example.com"); 

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    How about if you modify your example:
    PHP Code:
    foreach($_POST as $fieldname => $value){ $from_form[$fieldname]=$value; } 
    Then try:

    print_r($from_form);

    Would creating a multi-dimensional array give you a start?

    At least you can then loop through that programatically.
    Last edited by Cups; May 14, 2007 at 21:27. Reason: I think I might go back to bed actually ...

  4. #4
    SitePoint Evangelist
    Join Date
    Nov 2003
    Location
    Brisbane, Australia
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is it.

    You are awesome!!!!

    Thanks

  5. #5
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan's given the solution to your query but I thought I'd point something out in your code example..

    Quote Originally Posted by mrmbarnes View Post
    I have the following code which is great for taking all the information from a form and assign it to a variable the same name as the name on the field.
    PHP Code:
    foreach($_POST as $fieldname => $value){ $$fieldname=$value; } 
    Don't do this.. this is effectively the same as doing -
    extract($_POST);

    Which poses certain security risks.. suppose I submit my own form to your query and put a field in like this -

    Code:
    <input type="text" name="_SESSION[admin]">
    if, by chance, you happen to have a $_SESSION['admin'] variable I can overwrite it and perhaps break in to your admin section.. there's many other, more frightening, possibilities as well.

    Never trust user submitted data, not even the form names... since a malicious user would most likely use their own form.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •