Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

Form - PHP question

Hello, I am somewhat new at PHP forms that email the results to an email address.... so please bare with me!!! I have had success with sine but this one is giving me grief and I can't identify where I went wrong!

I have created an online form at: http://www.davisburg.ca/sports/sportsform.php

and the file it calls is: http://www.davisburg.ca/sports/send_form_email.php or as follows:

<?php

if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "info@davisburg.ca";

$email_subject = "Davisburg Sports Registration";

function died($error) {

    // your error code can go here

    echo "We are very sorry, but there were error(s) found with the form you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go back and fix these errors.<br /><br />";

    die();

}



// validation expected data exists

if(!isset($_POST['players_first_name']) ||

    !isset($_POST['players_last_name']) ||

    !isset($_POST['players_birth']) ||
    !isset($_POST['soccer']) ||
    !isset($_POST['tball']) ||
    !isset($_POST['soccer_tball']) ||
    !isset($_POST['baseball_tball']) ||
    !isset($_POST['baseball_rookie']) ||
    !isset($_POST['baseball_mosquito']) ||
    !isset($_POST['baseball_peewee']) ||
    !isset($_POST['baseball_bantam']) ||

    !isset($_POST['parentnames']) ||

    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||

    !isset($_POST['mother_cell']) ||
    !isset($_POST['father_cell']) ||
    !isset($_POST['t_shirt']) ||
    !isset($_POST['volunteer']) ||

    !isset($_POST['comments']) ||
    !isset($_POST['waivername']) ||
    !isset($_POST['waiveraddress']) ||
    !isset($_POST['waiverphone']) ||
    !isset($_POST['waiver']) ||
    !isset($_POST['waiverdate'])) {


    died('We are sorry, but there appears to be a problem with the form you submitted.');       

}




$email_from = $_POST['email']; // required




$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'Please fill in a valid email address.<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Form details below.\n\n";



function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}
$email_message .= "Players First Name: ".clean_string($players_first_name)."\n";
$email_message .= "Players Last Name: ".clean_string($players_last_name)."\n";
$email_message .= "Players Birthdate: ".clean_string($players_birth)."\n";
$email_message .= "Soccer: ".clean_string($soccer)."\n";


$email_message .= "Baseball: ".clean_string($baseball)."\n";

$email_message .= "Parent's Names: ".clean_string($parentnames)."\n";
$email_message .= "Home Telephone: ".clean_string($telephone)."\n";
$email_message .= "Mother's Cell: ".clean_string($mother_cell)."\n";
$email_message .= "Fathers's Cell: ".clean_string($father_cell)."\n";
$email_message .= "T-Shirt:".clean_string($t_shirt)."\n";

            $email_message .= "Volunteer: ".clean_string($volunteer)."\n";
            $email_message .= "Comments: ".clean_string($comments)."\n";
            $email_message .= "Waiver Name: ".clean_string($waivername)."\n";
            $email_message .= "Waive Address: ".clean_string($waiveraddress)."\n";
            $email_message .= "Waiver Phone: ".clean_string($waiverphone)."\n";
            $email_message .= "Waiver: ".clean_string($waiver)."\n";
            $email_message .= "Waiver Date: ".clean_string($waiverdate)."\n";

// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);

?>

<!-- include your own success html here -->

Your Registration was successfully received, thank you!<br /><br />

<br /><br />

Please make payment: <br /><br />

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="info@davisburg.ca"> <input type="hidden" name="lc" value="CA"> <input type="hidden" name="item_name" value="SODBUSTER SPORTS"> <input type="hidden" name="button_subtype" value="services"> <input type="hidden" name="no_note" value="0"> <input type="hidden" name="currency_code" value="CAD"> <input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest"> <table> <tr><td><input type="hidden" name="on0" value="SODBUSTER SPORTS">SODBUSTER SPORTS</td></tr><tr><td><select name="os0"> <option value="Soccer">Soccer $40.00 CAD</option> <option value="T-Ball">T-Ball $50.00 CAD</option> <option value="Soccer + T-ball w T-Shirt">Soccer + T-ball w T-Shirt $75.00 CAD</option> <option value="Rookie, Mosquito, Peewee & Bantam">Rookie, Mosquito, Peewee & Bantam $90.00 CAD</option> </select> </td></tr> </table> <input type="hidden" name="currency_code" value="CAD"> <input type="hidden" name="option_select0" value="Soccer"> <input type="hidden" name="option_amount0" value="40.00"> <input type="hidden" name="option_select1" value="T-Ball"> <input type="hidden" name="option_amount1" value="50.00"> <input type="hidden" name="option_select2" value="Soccer + T-ball w T-Shirt"> <input type="hidden" name="option_amount2" value="75.00"> <input type="hidden" name="option_select3" value="Rookie, Mosquito, Peewee & Bantam"> <input type="hidden" name="option_amount3" value="90.00"> <input type="hidden" name="option_index" value="0"> <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!"> <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1"> </form>

<?php

}

?>