-2

Hey I am trying to figure out my problem with my form and where my hangups are in the code. Anyone have any suggestions?

<form method="post" action="tourney.php">
                <div class="row">
                    <div class="col-sm-4">
                        <input class="form-control" type="text" placeholder="Name" name="tname">
                    </div>
                    <div class="col-sm-4">
                        <input class="form-control" type="text" placeholder="Email" name="temail">
                    </div>
                    <div class="col-sm-4">
                        <input class="form-control" type="text" placeholder="School or Club" name="tclub">
                    </div>
                    <br>
                    <br>
                    <div class="col-sm-4">
                        <input class="form-control" type="text" placeholder="Address" name="taddress">
                    </div>
                    <div class="col-sm-3">
                        <input class="form-control" type="text" placeholder="City" name="tcity">
                    </div>
                    <div class="col-sm-2">
                        <input class="form-control" type="text" placeholder="State" name="tstate">
                    </div>
                    <div class="col-sm-2">
                        <input class="form-control" type="text" placeholder="Zip" name="tstate">
                    </div>
                </div>
                <br>
                <div class="row">
                    <div class="col-sm-12">
                        <textarea placeholder="Type your message here..." class="form-control" rows="5"></textarea>
                    </div>
                </div>
                <br>
                <div class="row">
                    <div class="col-sm-6">
                        <select name="type" class="form-control">
                            <OPTION VALUE="None Selected" SELECTED>Please select one</OPTION>
                            <OPTION VALUE="Middle School">Middle School Tournamanet</OPTION>
                            <OPTION VALUE="Elementary">Elementary Tournament</OPTION>
                        </select>
                    </div>



                    <div class="col-sm-6 pull-right">
                        <input class="btn btn-danger" type="submit" value="Register">
                    </div>
                </div>


            </form>

and the PHP to send the email:

<?php 
if(isset($_POST['submit'])){
$from = "minguswrestling.com";
$to = "puremeld@gmail.com";
$name = $_POST['tname'];
$email = $_POST['temail'];
$school = $_POST['tclub'];
$address = $_POST['taddress'];
$city = $_POST['tcity'];
$state = $_POST['tstate'];
$zip = $_POST['tzip'];
$type = $_POST['type'];
$message = $_POST['tmessage'];
$subject = "Tourney Signup!";

$body = "From: $from \n Name: $name \n Email: $email \n School: $school\n Address: $address\n City: $city State: $state Zip: $zip\n Event: $type\n Message: $message";

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
    '<h2>Thank You!</h2>';
header("Location: index.html");
} else { 
    echo '<p>Oops! An error occurred. Try sending your message again.      </p>'; 
}
}
}
?>

Any help would be appreciated. I am using bootstrap for my build, thank you.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Core
  • 38
  • 5

2 Answers2

1

There are a few things wrong with your code.

Firstly, and as I mentioned in comments; everything inside the following conditional statement will never fire up:

if(isset($_POST['submit'])){...}

because there is no form element that is named "submit", being your submit button and error reporting would have thrown you an undefined index submit notice....

Therefore you need to modify it to read as:

<input name="submit" class="btn btn-danger" type="submit" value="Register">

However, you have a second if ($_POST['submit']) { that needs to be removed; it's not needed.

You also have no name attribute for:

 <textarea placeholder="Type your message here..." class="form-control" rows="5"></textarea>

that needs to be modified to and adding name="tmessage" since $message = $_POST['tmessage']; is most likely to be used for the message:

 <textarea name="tmessage" placeholder="Type your message here..." class="form-control" rows="5"></textarea>

Also, you've used the same name here for both "state" and "zip":

<input class="form-control" type="text" placeholder="Zip" name="tstate">

which should read as:

<input class="form-control" type="text" placeholder="Zip" name="tzip">

Then, you're outputting before header having the '<h2>Thank You!</h2>'; and header:

Sidenote: You may have meant to do echo '<h2>Thank You!</h2>';, yet that would still constitute as outputting before header, because PHP is actually throwing you a parse error, but your server is probably not set up to catch and display notices/errors etc.

if (mail ($to, $subject, $body, $from)) {
    '<h2>Thank You!</h2>';
header("Location: index.html");
}

You need to choose from "one" of those and not both. Either modify '<h2>Thank You!</h2>'; to read as echo '<h2>Thank You!</h2>'; and remove the header, or remove the "Thank you" and use header.

Consult the following on Stack about outputting before header:

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.

Here's a rewrite and see the embedded comments:

<?php 
if(isset($_POST['submit'])){
    $from = "minguswrestling.com";
    $to = "puremeld@gmail.com";
    $name = $_POST['tname'];
    $email = $_POST['temail'];
    $school = $_POST['tclub'];
    $address = $_POST['taddress'];
    $city = $_POST['tcity'];
    $state = $_POST['tstate'];
    $zip = $_POST['tzip'];
    $type = $_POST['type'];
    $message = $_POST['tmessage'];
    $subject = "Tourney Signup!";

    $body = "From: $from \n Name: $name \n Email: $email \n School: $school\n Address: $address\n City: $city State: $state Zip: $zip\n Event: $type\n Message: $message";

if (mail ($to, $subject, $body, $from)) {
    echo '<h2>Thank You!</h2>'; // either use echo
// header("Location: index.html"); // or header, not both. Comment one or the other out.
} else {
    echo '<p>Oops! An error occurred. Try sending your message again.      </p>'; 
}

}
else { echo "Submit is not set."; }
?>

Footnotes:

It would be best to also use !empty() against your POST arrays, since anyone could use your form and pass empty values.

Reference:

Plus, $from = "minguswrestling.com"; be careful with that. mail() expects that to be an E-mail address and not a domain and may end up in Spam.

Best to use a From: as stated in the manual.

Consult the manual:


Final notes.

It's unclear if you're using this from your own PC or a hosted service.

If it's from your own PC, then a Webserver needs to be installed and properly configured, including PHP and mail.

If from a hosted service, make sure that mail is made available to you.

Check your Spam also when testing.

If you're going to use header, add exit; for it, otherwise your code may want to continue executing:

header("Location: index.html");
exit;

Remember not to use the "echo" if you're going to use header.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Wow thank you my man. I appreciate the knowledge drop. – Core Oct 06 '15 at 22:48
  • @DustinCoressel You're welcome Dustin. I hope this gets you success. I did make a few more edits, so you may need to reload my answer. If it did solve it, you could mark it as solved. You're not obligated to, but it informs everyone that a solution was found. *Welcome to Stack*, cheers. – Funk Forty Niner Oct 06 '15 at 22:49
  • It did work, but better I learned what is going on. – Core Oct 07 '15 at 00:00
0

The following:

'<h2>Thank You!</h2>';
header("Location: index.html");

Is wrong, you cannot output anything before a header. Remove:

'<h2>Thank You!</h2>';
taxicala
  • 21,408
  • 7
  • 37
  • 66