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

Undefined index: submit [APP\View\Contacts\index.ctp, line 36] when trying to submit contact form

Why am i getting this error?

        <form class="contact" method="post">
           <h2 style="color:#ecaf1c;padding-left:20px; padding-top:25px;display:inline;">Name</h2>
            <h2 style="color:#ecaf1c;padding-left:205px; padding-top:25px;display:inline;">Email</h2><br>
        <input type="text" name="name">
        <input type="text" name="email">
            <h2 style="color:#ecaf1c;padding-left:20px; padding-top:25px;display:inline;">Contact Number</h2>
            <h2 style="color:#ecaf1c;padding-left:46px; padding-top:25px;display:inline;">Subject</h2><br>
        <input type="text" name="number">
        <input type="text" name="subject"><br>
            <h2 style="color:#ecaf1c;padding-left:20px; padding-top:25px;display:inline;">Your Message....</h2>
        <textarea name="message"></textarea>

        <input type="submit" style="margin-top:20px;margin-left:20px;" name="submit" >
        </form>



    <?php 

    if ($_POST['submit']) {
        $name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['number'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    $to = 'allcountyroadmarkings@live.co.uk';

    $body = "From: $name\n Email: $email\n Number: $number\n Message:\n $message";




        mail($to,$subject,$body);
    }
        ?>

1 Answer

Is this for a particular lesson or code challenge? I'm not sure what's causing the specific error, but it looks like you will need to concatenate the parts of $body since you are using a mixture of text and variables. Something like:

$body = "From: ". $name . "<br>Email: " . $email . "<br>Number: " . $number . "<br>Message: " . $message;
echo "<div>" . $body . "</div>";

You may also want to use labels for each input type instead of H2s to make it more clear for users which aligns with each input field. Good luck!

i don't think concatenation would make a difference to the end results because Matthew Smart has used double quotes around his string. See here under "Variable parsing", but I could be wrong!

Matthew - is it possible to notate line 36! I love a good count, but it's difficult to keep track without any line numbers ;-)

I also notice you're in a CakePHP template file? If I were you, I would either extract the sending code to the controller or put the mail-send code above the form. When you submit the form, the form will be written to the page again before the mail sends. If you wanted to do a header redirect depending on the success/failure of sending, this wouldn't work because you've already outputted content to the screen :-)

Thanks for the reference, Tom! You're right. When I pasted the code into my workspace as is and had the page echo out $body on submit, it worked just fine. So the data is getting posted correctly, just not sending...

Yes im using CakePHP , but very new to it. although i get the error i know the code is right and would work in a normal php file. im guessing that it will work still ,