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 = "my@email.com";

$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?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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