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

PHP Enhancing a Simple PHP Application Integrating Validation Errors Escaping Output

need php debuger any one out there help!!

maybe i am getting syntax error it would be helpful if php was more like javascript where the console is showing u where u have got error everytime you make a mistake. but this php is just showing blank page really having difficult time any one who good php debugger out there please share

15 Answers

Please look at your foreach loop. I might be wrong, but I think this should be run conditionally. Later in your code, you utilize the isset() function. I think you might need to wrap your foreach loop in an "if" statement that uses this function.

also maybe check all the paths to make sure the includes and require functions are linked to the proper paths.

You can paste your PHP code here to validate it: http://phpcodechecker.com/

it saying no issue found and there is cleary a reason why i am not getting the result i want

Well, it's likely that there's another issue. Where are you running the PHP code?

mamp

if you'd like, you can post your code and I can take a look at it. Of course, this might be hard if you're working with a very large application.

<?php 

ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $message = trim($_POST["message"]);


    if ($name == "" OR $email == "" OR $message == "") {
        $error =  "You must specify a value for name, email address, and message.";
    }

    foreach( $_POST as $value ){
        if( stripos($value,'Content-Type:') !== FALSE ){
            $error =  "There was a problem with the information you entered.";    
        }
    }

    if ($_POST["address"] != "") {
        $error =  "Your form submission has an error.";
    }

    require_once("inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();

    if (!$mail->ValidateAddress($email)){
        $error =  "You must specify a valid email address.";
        exit;
    }

    if (!isset($error))
    {

        $email_body = "";
        $email_body = $email_body . "Name: " . $name . "<br>";
        $email_body = $email_body . "Email: " . $email . "<br>";
        $email_body = $email_body . "Message: " . $message;

        $mail->SetFrom($email, $name);
        $address = "latiif607@gmail.com";
        $mail->AddAddress($address, "abdi ali");
        $mail->Subject    = "Shirts 4 Mike Contact Form Submission | " . $name;
        $mail->MsgHTML($email_body);

        if($mail->Send()) {
            header("Location: contact.php?status=thanks");
            exit;
        } else {
            $error = "There was a problem sending the email " . $mail->ErrorInfo;
        }

    }

}
if (isset($error)) {
    echo $error;
}

?><?php 
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php'); ?>

    <div class="section page">

        <div class="wrapper">

            <h1>Contact</h1>

            <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
                <p>Thanks for the email! I&rsquo;ll be in touch shortly!</p>
            <?php } else { ?>

                <?php if (isset($error)) {echo $error; } ?>

                <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>

                <form method="post" action="contact.php">

                    <table>
                        <tr>
                            <th>
                                <label for="name">Name</label>
                            </th>
                            <td>
                                <input type="text" name="name" id="name">
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="email">Email</label>
                            </th>
                            <td>
                                <input type="text" name="email" id="email">
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="message">Message</label>
                            </th>
                            <td>
                                <textarea name="message" id="message"></textarea>
                            </td>
                        </tr> 
                        <tr style="display: none;">
                            <th>
                                <label for="address">Address</label>
                            </th>
                            <td>
                                <input type="text" name="address" id="address">
                                <p>Humans (and frogs): please leave this field blank.</p>
                            </td>
                        </tr>                   
                    </table>
                    <input type="submit" value="Send" name="submit">

                </form>

            <?php } ?>

        </div>

    </div>

<?php include('inc/footer.php') ?>

and you also explain precisely what the issue is? (is the page displaying but not functioning somehow, or are you simply getting a blank page?)

is not large application is just one of the course here in php i want if an error exist i show which can of error exist if not i send email n redirect the user to thank you page. if i fill the form with the correct name email the redirection works just good but if i don't type anything it show blank page rather than displaying the error

the foreach loop just prevents malicious people attacking my code nothing else i don't thinks that is the issue though

ohhh ok got your point now thank you

solved

Good! I'm glad you got it.

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

Add the following two line to the top of you file to display errors with your code

error_reporting(E_ALL);
ini_set('display_errors',1);

For more information, check out the docs here http://php.net/manual/en/function.error-reporting.php

Also the "PHP Standards and Best Practices" is a great course :)