Jump to content

Form data doesn't transfer to my PHP script


Gier NL

Recommended Posts

Hey guys!

 

I've got a simple web form for sending e-mail.

The form contains a name, email, subject and body textfield and a submit button:

 

Userinfo.html:

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

<input type="text" name="name" size="30">

<input type="text" name="replyemail" size="30">

<input type="text" name="subject" size="30">

<textarea rows="6" cols="54" name="body"></textarea>

<input type="submit" name="submit" value="send">

 

sendmail.php:

<?PHP

$to = "[email protected]";

$from_header = "From: $replyemail";

if($body!= "")

{

  //send mail - $subject & $body come from surfer input

  mail($to, $subject, $body, $from_header);

}

  else

{

  print("<HTML><BODY>Error, no comments were submitted!");

  print("</BODY></HTML>");

}

?>

 

 

I'm getting the following error:

Notice: Undefined variable: subject in C:\Inetpub\wwwroot\cms\Download\Sendmail.php on line 3

 

Notice: Undefined variable: to in C:\Inetpub\wwwroot\cms\Download\Sendmail.php on line 4

 

Notice: Undefined variable: replyemail in C:\Inetpub\wwwroot\cms\Download\Sendmail.php on line 6

 

Notice: Undefined variable: body in C:\Inetpub\...\Sendmail.php on line 7

Error, no comments were submitted!PHP Notice: Undefined variable: subject in C:\Inetpub...\Sendmail.php on line 3 PHP Notice: Undefined variable: to in C:\Inetpub\...\Sendmail.php on line 4 PHP Notice: Undefined variable: replyemail in C:\Inetpub\...\Sendmail.php on line 6 PHP Notice: Undefined variable: bericht in C:\Inetpub\...\Sendmail.php on line 7

 

Does anyone have an idea what could be wrong?

 

You need to use $_POST variables - http://us.php.net/manual/en/language.variables.external.php

 

The code you have now would have only worked when register_globals are on and they were turned off six years ago because they created a security hole. Wherever you got or learned that code is way out of date.

If you want, you can use php to serve the form as well as process it.  You should always check your variables before using them; empty is just a simple check to see whether or not a user filled them in.  You'd want to make sure any input was valid for the field you expect before processing it.

 

<?php

   if(isset($_POST["submit"]) && $_POST["submit"] === "send") {

      if(empty($_POST["name"]) || empty($_POST["replymail"]) || empty($_POST["subject"]) || empty($_POST["body"])) {

         // one or more post vars had an empty field

      } else {

         // validate input
         // assign post vars as needed          
         // call mail function

         // mail returns a boolean value, true indicates it was sent, but
         // does not indicate whether or not it will actually arrive
      } 

   } else {
?>

<pre>
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
   Name: <input type="text" name="name" size="30">
   From: <input type="text" name="replyemail" size="30">
Subject: <input type="text" name="subject" size="30">

   Body:
         <textarea rows="6" cols="54" name="body"></textarea>
<input type="submit" name="submit" value="send">
</pre>

<?php
   }
?>

 

HTH

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.