0

Hi, I have a problem with my php and html form. What I am trying to do is just get a form with 7 input fields, 6 of these are input field or text area and one will be a checkbox. I have one hidden field, the first 3 boxes the hidden field the first name and message. The only problem I have is when I add a new input box it shows me the 500 error. My code is below:

<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
  {
  ?>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

  <input type="hidden" name="subject" value="can you create me an account"><br>
  Message: <textarea rows="10" cols="40" name="message"></textarea><br>
  first <input type="text" name="first_name" >
  <input type="submit" name="submit" value="Submit Feedback">
  </form>

<?php 
  }
else
  // the user has submitted the form
  {
  // Check if the "subject" input field is filled out
  if (isset($_POST["subject"]))
    {
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    $first = $_POST["first_name"];


    $message = wordwrap($message, 70);
    $first = wordwrap($first, 70);
    // send mail
    mail("summat@gmail.com",$subject,$message,$first,"subject: $subject\n");
    echo "Thank you for sending us feedback";

When I add a new input box my code looks like :

<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
  {
  ?>

<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

   <input type="hidden" name="subject" value="can you create me an account"><br>
   Message: <textarea rows="10" cols="40" name="message"></textarea><br>
   first <input type="text" name="first_name" >
   last <input type="text" name="last_name" >
   <input type="submit" name="submit" value="Submit Feedback">
  </form>

<?php 
  }
else
  // the user has submitted the form
  {
  // Check if the "subject" input field is filled out
  if (isset($_POST["subject"]))
    {
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    $first = $_POST["first_name"];
    $last = $_POST["last_name"];


    $message = wordwrap($message, 70);
    $first = wordwrap($first, 70);
    $last = wordwrap($last, 70);
    // send mail
    mail("summat@gmail.com",$subject,$message,$first,$last,"subject: $subject\n");
    echo "Thank you for sending us feedback";

Everything shows on the screen when I add them but when I press submit I get:

500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed.

and nothing gets submitted. Is it because it times out before it send or to much data or have I just missed something very basic?

Any help would be much appreciated.

Toby van Kempen
  • 1,447
  • 4
  • 13
  • 20
user3387522
  • 127
  • 4
  • 19

2 Answers2

3

The problem is that in the 2nd example you're trying to pass 6 variables to the mail() function when it accepts 5. Check here on how you can pass additional headers.

tliokos
  • 3,606
  • 3
  • 28
  • 28
  • ohhh that makes more sence how would i add the variable into the $header section would it just be like `mail("summat@gmail.com"mail, $subject, $message, $headers);");` @tliokos – user3387522 Apr 03 '14 at 08:15
2

6 Variables will result in:
Warning: mail() expects at most 5 parameters, 6 given in YOUR WEBSITE on LINE

Solution example:

    <?php
     //var_dump($_POST);
    if (isset($_POST["subject"]))
        {
        $subject = $_POST["subject"];
        $message = $_POST["message"];
        $first = $_POST["first_name"];
        $last = $_POST["last_name"];
        $name= "$first $last";
    }
        $message = wordwrap($message, 70);
        $first = wordwrap($first, 70);
        $last = wordwrap($last, 70);
        mail("summat@gmail.com",$subject,$message,$name,"subject: $subject\n");
        echo "Thank you for sending us feedback";
      ?>

Answer to your reply:
Php:

 <?php
 //var_dump($_POST);
if (isset($_POST["subject"]))
    {
    $subject = $_POST["subject"];
    $message = $_POST["message"];
    $first = $_POST["first_name"];
    $last = $_POST["last_name"];
    $company = $_POST["company"];
    $email = $_POST["email"];
    $telnr = $_POST["telnr"];
    $description = $_POST["description"];
    $therest = "First name= $first" . "\r\n" . "Last name= $last" . "\r\n" . "Last name= $last" . "\r\n" . "Company= $company" . "\r\n" . "Email= $email" . "\r\n" . "Telnr= $telnr" . "\r\n" . "Description= $description";          

    //echo "$therest <br>";
    $message = wordwrap($message, 70);
    $first = wordwrap($first, 70);
    $last = wordwrap($last, 70);
    mail("Your Email Address Here",$subject,$name,$therest,"subject: $subject\n");
    echo "Thank you for sending us feedback";
}

HTML

<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

   <input type="hidden" name="subject" value="can you create me an account"><br>
   Message: <textarea rows="10" cols="40" name="message"></textarea><br>
   first <input type="text" name="first_name" ><br>
   last <input type="text" name="last_name" ><br>
   company <input type="text" name="company" ><br>
   email <input type="text" name="email" ><br>
   Telephone number <input type="text" name="telnr" ><br>
   Description <input type="text" name="description" ><br>
   <input type="submit" name="submit" value="Submit Feedback">
  </form>

Demo: here
It will send the mail to the mail your entered in the form

Jouke
  • 459
  • 1
  • 7
  • 20
  • I know this is probably a simple question what is $resthere ? and two how would i add other fields cause what i am trying to do in the end is @user3489793 first name, last name , company, email, telephone number, description – user3387522 Apr 03 '14 at 08:26
  • $resthere was a line i forgot to delete(edited the post now). I will edit my post and add an example for the rest of your questions – Jouke Apr 03 '14 at 08:29
  • thank you that would be much appreciated due to it being the first time that i have used the `mail()` – user3387522 Apr 03 '14 at 08:39
  • I edited my answer. Tell me if you need any more help! – Jouke Apr 03 '14 at 09:09
  • this has completely worked the only thing i would like you to do is just edit your answer in the html you put `Description` it should be `description` I have changed this in my code and it is working perfectly – user3387522 Apr 03 '14 at 09:15