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

Php form problem!

Hello, i have been trying to gather information on how to fix a php form for a few days, due to that i do not have any php coding knowledge as of yet.

Ive been trying with different codes from other people on forum that have helped me, but i have not succeded in fixing so the email gets sent on clicking on the submit button,

So i wonder if anyone here with php coding experience, who has time, can fix a quick php code for email sending. Im currently learning about JavaScript, and i do not wanna mix everything up by starting to learn php all of the sudden, but i will jump into php later on.

Heres the html form code, that is inside the file ("contact-us.php")

<form action="" method="POST">
          <h1>Contact</h1>

         <fieldset>
            <input maxlength="30" type="text" name="name" placeholder="John" required><br>
            <input maxlength="30" type="text" name="lastname" placeholder="Smith" required><br>
            <input maxlength="30" type="email" name="email" placeholder="john_doe@example.com" required>
         </fieldset>

          <fieldset>       
            <textarea maxlength="300" placeholder="Write your text here...." id="message" name="message" cols="40" rows="6" required></textarea>
            <button name="submit" type="submit">Send Message</button>
          </fieldset>
        </form>
      </section>
<?php 
if(isset($_POST['submit'])){
    $to = "example@example.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

 <?php if (isset($name-of-your-input)) { echo htmlspecialchars($name-of-your-input); } ?>

Thanks!

Kind Regards..

erdrag

2 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

Hi, Erdrag. Unfortunately, that appears to be the page that is served to the browser after processing. Because PHP is a server-side language, it is executed before being sent to the requesting client computer, meaning that all PHP code blocks will be removed by the time the client computer receives the page. Do you have access to the original code for the page? If you could post that, we might be able to help you out. :)

Should the form html be inside a ".php" file or should it be in a ".html" ?

Because currently i have a full page of html in a php file named "contact-us.php" and then the php code in another file named "submit.php" then i use the "include" to include it in the contact-us.php file

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

Any file that is supposed to run PHP in it should be named with a .php extension, otherwise the code won't run. If your code for submitting the form is in a submit.php file and your contact-us.php file simply has include('submit.php'); at the top, then we'd need to see your submit.php file contents.

I included the php code above!

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

Awesome! So, when you POST a message to an email like this, you can use the $_POST variable to access what a user has inputted into a form. The value of submitted fields can be retrieved, like you have done, with $_POST['your_variable']. The your_variable part, however, comes from the name attribute in form fields. In your PHP, you are trying to access these variables:

$_POST['first_name'];
$_POST['last_name'];
$_POST['email'];

but in your html form, you have these fields:

<input maxlength="30" type="text" name="name" placeholder="John" required><br>
<input maxlength="30" type="text" name="lastname" placeholder="Smith" required><br>
<input maxlength="30" type="email" name="email" placeholder="john_doe@example.com" required>

As such, you may be causing PHP errors since those are different than the ones you're trying to access in your PHP. Try changing your HTML form elements to this:

<input maxlength="30" type="text" name="first_name" placeholder="John" required><br>
<input maxlength="30" type="text" name="last_name" placeholder="Smith" required><br>
<input maxlength="30" type="email" name="email" placeholder="john_doe@example.com" required>

and see how that works.

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

Sorry, I had some formatting problems. All fixed! XD

Hey again, i changed the code to the one you commented, but it still does not work. Can it have something to do with my webhost to do? I have the webhost service "one.com"

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

It might. Some webhosts don't allow the sending of emails like this, so that may be causing your problems. I know my webhost does, so let me try your code out on my server and I'll get back to you!

Okey do that! :) i saw that my Webhost has something called SMTP server for sending emails and then a url for it, to include. Can that have something to do with this problem? how can i include this?

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

Alrighty! So I've verified that your form does work, but there are two things you need to make sure you have changed from what you have pasted in your submit.php file above.

1) Make sure that you have changed example@example.com to the email address to which you want the form submission to be sent.

2) The code at the bottom is using an illegal variable name. PHP variables cannot use minuses (-), so make sure that looks something like this:

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

I'm not too sure what that last line is there for, but if you change it to what I have, your form should submit properly. Note, however, that even though it should work, you should still be sanitizing your input before it is sent. So look at some of the PHP videos here at Treehouse to find out how to do that if you are going to be using this on a real site.

Okey i changed everything to what you said, but the outcome is still the same, When i clicl the submit button, the page just refresh.

This has to do something with the server im using, at my webhost site it says that i can send mails using a "SMTP" server with a url like send.one.com, how do i include this in the php, this might be the problem

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

Aww, that's a bummer. Unfortunately, I'm not sure how your webhost needs your form to be submitted to properly send emails, but it looks like PHP Mailer has support for SMTP, so you could try using that library. The documentation explains how to do it, though it's a little more complicated (not by much, though) than what you have here. Give that a try and see if you can get it to work.

I'll look it up with my webhost tomorrow, thanks for helping me with this problem!

I appreciate it alot :)

Kind Regards...

Erdrag

Hello i was thinking about this, and i realised that my website is not on a webhosting server, because im developing it right now, can that have something to do with this? because i only have the files on my computer, not on any webhosting server

Ryan Field
Ryan Field
Courses Plus Student 21,242 Points

Yes, that will absolutely make a difference. If you are just developing locally, then you probably won't be able to send the message.

Okey that probably explains it, because i change my Web host email to smtp but it still did not work, so ill have to wait until i upload the site to a server to see if it works :)