Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Php form issue!

Hello, ive been trying to find some code snippets on how to make a form work, because i do not have any php coding experience yet,

But i do not understand where i should put the php code,

What do i do with the code? i put it in a file named contact-us.php but how do i acces it? can someone please help with this. Thanks.

<?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.
    }
?>

and heres the form

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

          <legend>Your Name and Email</legend>
         <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>
          </fieldset>
         <fieldset>
         <textarea maxlength="100" placeholder="Write your text here" id="message" name="message" cols="40" rows="6" required></textarea>
         <button name="submit" type="Submit">Send Email!</button>
          </fieldset>


        </form>

2 Answers

Paulo Marques
Paulo Marques
22,845 Points

Hello Erdrag,

Put the php code and save as submit.php or other name that you like for it.

In the form.php, where is the html code, you use an include to call the submit.php variables to be used on the page.

SO you go to insert:

<?php include('submit.php') ?>

best regards,

Oh so i need 2 php files, one for The html and one for The php code, thanks for help in me, i really need to start learning about php haha :)

// erdrag

I did a submit.php file and put all the php code, i showed above, in it. and then i made a form.php with all the html,

and i put the <?php include('submit.php') ?> in the form.php over the form code, but this does not work?

Any ideas?

erdrag

Paulo Marques
Paulo Marques
22,845 Points

Hello Erdrag,

I missed one part, you need to add the submit button to your form. Remove that <button>, and add a <input type="submit" />. In this moment of the code nothing is submitting.

Best regards.

Hello, in the contact-us.php where the html form is in, the button is there, but its not submitting, what do you mean ?

erdrag

Paulo Marques
Paulo Marques
22,845 Points

Sorry, my comment came whitout html tags.

Remove the:

<button>

And add a:

<input type="submit" />

Hey i added the submit button like that, but still when i press submit it does not send?

Okey ill go throught what i have..

I have a "Contact-us.php" file with all the html for the site, including the form, then i have a "submit.php" file with all the php code for the email sending, and i included it in the "contact-us.php" file using the include tag,

And now i want so when the submit button is pressed the email sends, but its not doing it.

Paulo Marques
Paulo Marques
22,845 Points

First thing:

  • Are you using a php server ? If not, install one. (MAMP for MAC, Wamp for windows, XAMPP for Linux)

Let's change the logic, ok ? Remove the include and put the name of the file on the action.

Will be like this:

<form action="submit.php" method="POST">

Try this please.

Okey i will install "Wamp" but isnt there anyway to enable a php server at my Webhost? "one.com"

EDIT: Wamp's website is down

Hello erdrag erdragsson i have looked at your php code if u where going to use this on a website u might won't to think about security for the form because it does not strip html tags or anything like that so attacker can upload his own code to the server.

I have no experimentera in php coding as of yet, but can you show how to fix that?

Paulo Marques
Paulo Marques
22,845 Points

Hello erdrag erdragsson.

Use this code on the value of the inputs.

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

Regards,