Page 1 of 1

Send Contents of HTML form

Posted: Sun Nov 09, 2008 9:56 am
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

Re: Send Contents of HTML form

Posted: Mon Nov 10, 2008 11:08 pm
by novice4eva
you missed out the name attribute in your form
Name:<input type="text" size="30" />

Re: Send Contents of HTML form

Posted: Mon Nov 10, 2008 11:29 pm
by Stryks
You should remove that referrer check while you're at it.

It's only going to cause you grief.

Re: Send Contents of HTML form

Posted: Tue Nov 11, 2008 10:34 am
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

Re: Send Contents of HTML form

Posted: Tue Nov 11, 2008 4:20 pm
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

Re: Send Contents of HTML form

Posted: Wed Nov 12, 2008 12:25 am
by ardan16
Thanks Stryks,
worked like a cham,
cheers

Re: Send Contents of HTML form

Posted: Wed Nov 12, 2008 12:26 am
by ardan16
ok so that was meant to be charm :oops: