treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Whats is wrong?

  • Can anyone tell me what is wrong with my coding?
    <?php
    $errors = '';
    $myemail = 'xxxxxx@xxxx.com';
    }

    $campaign = $_POST['campaign'];
    $page = $_POST['page'];
    $line = $_POST['line'];
    $quantity = $_POST['quantity'];
    $price = $_POST['price'];
    $email_address = $_POST['email'];
    $tel = $_POST['tel'];
    $name = $POST['name'];

    if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
    $email_address))
    {
    $errors .= "\n Error: Invalid email address";
    }

    if( empty($errors))
    {
    $to = $myemail;
    $email_subject = "Order form submission for: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n Email: $email_address \n Tel: $tell \n Campaign: $campaign \n Page: $page \n Line:$line \n Quantity: $quantity \n Price: $price";

    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
    <html>
    <head>
    <title>Contact form handler</title>
    </head>

    <body>
    <!-- This page is displayed only if there is some error -->
    <?php
    echo nl2br($errors);
    ?>


    </body>
    </html>
  • Fourth line has a closing bracket but there's no opening bracket......?
    $email_subject should have $name outside the quotes?
    $email_body should have various variables outside the qoutes?

    Same with $headers....?
  • @Senff (edited):
    He's using double-quotes. The variables will be parsed and expanded.

    -------------------------------------------
    @ricky122892 (original post):

    Do you really want a full assessment? Or do you have a specific problem that you're asking about?

    <?php
    $errors = '';
    $myemail = 'xxxxxx@xxxx.com';
    } // <---extraneous curly brace

    $campaign = $_POST['campaign'];
    $page = $_POST['page'];
    $line = $_POST['line'];
    $quantity = $_POST['quantity'];
    $price = $_POST['price'];
    $email_address = $_POST['email'];
    $tel = $_POST['tel'];
    $name = $POST['name'];

    if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
    $email_address))
    {
    $errors .= "\n Error: Invalid email address";
    }
    // your regex will reject some valid email addresses

    if( empty($errors))
    {
    $to = $myemail;
    $email_subject = "Order form submission for: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n Email: $email_address \n Tel: $tell \n Campaign: $campaign \n Page: $page \n Line:$line \n Quantity: $quantity \n Price: $price";
    // $tell is not defined (maybe you meant $tel ?)

    $headers = "From: $myemail\n"; // <--- newline in email headers must be \r\n
    $headers .= "Reply-To: $email_address"; // <--- no newline at all

    mail($to,$email_subject,$email_body,$headers);

    header('Location: contact-form-thank-you.html');
    // not a valid HTTP header ("Location" headers must be a fully-qualified URL)
    // it's also a good idea to exit after redirecting,
    // so your script doesn't continue execution
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
    <html>
    <head>
    <title>Contact form handler</title>
    <!-- no charset defined -->
    </head>

    <body>
    <?php
    echo nl2br($errors);
    ?>


    </body>
    </html>
  • Doh, of course. I'm so used to single quotes now. :-/