0

I have made a web form that sends an email with the form information contained within. I have a couple of issues I haven't been able to fix though, and I am hoping someone can help.

  1. The selection boxes for Current Club Membership do not return any information in the email, just a blank space where the information should be, even though an option is selected.
  2. After the form is sent the mail.php file loads. I would like a thank you HTML page to load instead. How do I do that? I tried changing the action attribute in the HTML to the page I want, but then the email doesn't send.

The HTML:

<form method="post" action="mail.php">
<div class="row uniform">

<div class="6u 12u$(xsmall)">
<label for="name">Full Name</label>
<input name="name" type="text" class="required" id="name" value="" />
</div>


<div class="6u$ 12u$(xsmall)">
<label for="email">Email Address</label>
<input name="email" type="email" class="required" id="email" value="" />
</div>


<div class="6u 12u$(xsmall)">
<label for="date">Date of Birth</label>
<input name="date" type="text" class="required" id="date" value="" />
</div>


<div class="6u$ 12u$(xsmall)">
<label for="age">Age as of July 1, 2016</label>
<input name="age" type="text" class="required" id="age" value="" />
</div>

<div class="6u 12u$(xsmall)">
<label for="phone">Phone Number</label>
<input name="phone" type="tel" class="required" id="phone" value="" />
</div>

<div class="12u$">
<label for="club">Current Club Membership</label>
<div class="select-wrapper">
<select name="club" class="required" id="club">
<option value="">-</option>
<option value="Colonial Road Runners">Colonial Road Runners</option>
<option value="Peninsula Track Club">Peninsula Track Club</option>
<option value="Tidewater Striders">Tidewater Striders</option>
</select>
</div>
</div>


<div class="12u$">
<label for="message">Message</label>
<textarea name="message" id="message" rows="6"></textarea>
</div>

<div class="12u$">
<ul class="actions">
<li><input type="submit" name="submit" value="Send Message" class="special" /></li>
<li><input type="reset" value="Reset" /></li>
</ul>
</div>
</div>
</form>

The php:

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $date = $_POST['date'];
        $age = $_POST['age'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
        $from = 'From: hrsupergp@gmail.com'; 
        $to = '6linewrasse@gmail.com'; 
        $subject = 'New Registration';

        $body.='email: '. $_POST['email']."\n";
        $body.='date: '. $_POST['date']."\n";
        $body.='age: '. $_POST['age']."\n";
        $body.='phone: '. $_POST['phone']."\n";
        $body.='club: '. $_POST['club']."\n";
        $body = "From: $name
                 E-Mail: $email
                 Date: $date
                 Age 7/2016: $age
                 Phone:  $phone
                 Club:  $club
                 Message: $message";
        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }


    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
?>
Barbra
  • 31
  • 7

2 Answers2

3

I may as well post an answer here.

Your last $body is broken

$body = "From: $name

and is missing the concatenate .

Change that to:

$body .= "From: $name...

and for a redirection, it's as easy as doing a header.

header('Location: http://www.example.com/');
exit;

But, make sure there is nothing about your PHP, because you may be outputting before header.

If your HTML form is above PHP, place it below it.

If you get a headers sent notice, consult:

Sidenote:

It's best to use a conditional empty() for everything in order to avoid blank data.


Footnotes:

The header, if you're going to use it, should be placed where you presently have $result='<div class="alert alert-success">Thank You! I will be in touch</div>';. Don't use it "with" that, but replaced by it, should you be echoing that $result variable somewhere that you haven't shown us.

  • That alone will trigger a headers sent notice, or fail silently because your system may not be setup to catch and display errors/warnings/notices.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

You need to include at the select tagonchange="document.getElementById('text_content').value=this.options[this.selectedIndex].text"

And this input is used to store the value from the option

<input type="hidden" name="test_text" id="text_content" value="" />

get the text of the selected option using php

To redirect,

   header("Location: thankyou.html");
   die();

after the if statement where you return the string of you will be in touch. How to make a redirect in PHP?

You can pass the string to another page via URL. Just create a <div class="thankyou"> on the new page and the URL be something like thankyou.html?thankyou=thank%20you%20very%20much. So your header will look something like this:

header("Location: thankyou.html?thankyou=thank%20you%20very%20much");
       die();
Community
  • 1
  • 1
cweitat
  • 1,199
  • 1
  • 11
  • 28