Send Contents of HTML form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ardan16
Forum Commoner
Posts: 32
Joined: Fri Aug 01, 2008 6:07 am

Send Contents of HTML form

Post by ardan16 »

Hi all,
Can some one please help me with this?
I have a html page

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>why</title>
<link rel="stylesheet" type="text/css" href="test1.css"/>
</head>
<body>
<form action="process.php" method="post">
Name:<input type="text" size="30" />
<input type="submit" value="Send"/>
</form>
</body>
</html>
And using this php

Code: Select all

<?php
    if ($_SERVER['REQUEST_METHOD']=="POST"){
      // In testing, if you get an Bad referer error
      // comment out or remove the next three lines
      if (strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])>7 ||
         !strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']))
         die("Bad referer");
      $msg="Values submitted by the user:\n";
      foreach($_POST as $key => $val){
         if (is_array($val)){
            $msg.="Item: $key\n";
            foreach($val as $v){
               $v = stripslashes($v);
               $msg.="   $v\n";
            }
         } else {
            $val = stripslashes($val);
            $msg.="$key: $val\n";
         }
      }
      $recipient="myEmail@yahoo.com.au";
      $subject="Form submission";
      error_reporting(0);
      if (mail($recipient, $subject, $msg)){
         echo "<h1>Thank you</h1><p>Message successfully sent:</p>\n";
         echo nl2br($input);
      } else
         echo "An error occurred and the message could not be sent.";
   } else
      echo "Bad request method";
?>


I only get this 'Values submitted by the user:' in email and no values even when name is filled out.
thanks
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Send Contents of HTML form

Post by novice4eva »

you missed out the name attribute in your form
Name:<input type="text" size="30" />
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Send Contents of HTML form

Post by Stryks »

You should remove that referrer check while you're at it.

It's only going to cause you grief.
ardan16
Forum Commoner
Posts: 32
Joined: Fri Aug 01, 2008 6:07 am

Re: Send Contents of HTML form

Post by ardan16 »

Thanks Both,
yes it was the name part I had missed. :oops:
another question if I may the php code gives all the data in a long form is there away to only get those values that are filled in?
Cheers
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Send Contents of HTML form

Post by Stryks »

Inside that foreach loop, you could just do ...

Code: Select all

if(trim($val) != '')
    //Value is set
}
Some might point you towards empty(), but the results are a little flaky for me. For example, if your user enters 0 in a textbox, it will be empty($val) = true. Not correct for our purposes.

Outside of the foreach, it'd be more like ...

Code: Select all

if((isset($_POST['value']) && (trim($_POST['value']) != ''))
    //Value is set
}
Cheers
ardan16
Forum Commoner
Posts: 32
Joined: Fri Aug 01, 2008 6:07 am

Re: Send Contents of HTML form

Post by ardan16 »

Thanks Stryks,
worked like a cham,
cheers
ardan16
Forum Commoner
Posts: 32
Joined: Fri Aug 01, 2008 6:07 am

Re: Send Contents of HTML form

Post by ardan16 »

ok so that was meant to be charm :oops:
Post Reply