Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Order form ~ PHP ??

Options
  • 27-07-2009 10:10pm
    #1
    Registered Users Posts: 47


    Hello People

    I need help..
    I am doing a web design course with my final assessment due in Friday
    To finish off my site I need an order form and a comment page, Alas the course instructor is not versed in PHP so is unable to assist me here... (Do I even need PHP?)

    Does anyone know of a site that I can build what I need, even a site with a template....

    All help/assistance is greatly appreciated...

    Thanks

    Poppa


Comments

  • Registered Users Posts: 2,793 ✭✭✭oeb


    It's pretty simple.

    Your input elements on a form are normally along the lines of : <input name="firstname" type="text" value="" />

    It's the 'name' attribute that's important here. When you submit a form to a php script (The target of the script is defined in the form opening tag <form method="post" action="path/to/my/script.php">). Now, a php page recieves a post request, it keeps all the request details in what is known as a superglobal. Superglobals are constructs created by php where you can retrieve information such as get or post variables, server information etc. Post variables are accessed like this

    $_POST;

    Where variable_name is the same as the 'name' attribute from the previous page.

    Now, mail is also easy to manage with php. It's simply :

    mail("email address", "subject", "message");
    (There is also a fourth argument, for additional headers, but you don't need this for now.)

    So now, onto a very basic contact form, which when submitted, will email the results to you.

    Your contact form, contact.html
    [html]
    <html>
    <head><title>Contact Us</title></head>
    <body>
    <form method="post" action="domail.php">
    <label for="name">Your Name :</label> <input type="text" name="name" value="" /><br />
    <label for="email">Your Email :</label> <input type="text" name="email" value="" /><br />
    <label for="comments">Comments :</label> <textarea name="comments"></textarea><br />
    <input type="submit" value="Submit Form" />
    </form>
    </body>
    </html>
    [/html]And now the php script, domail.php
    [php]
    <?php
    // Anything with 2 slashes before it like this, is a comment and ignored by the interpretor.

    //First we take the input. We should sanatize it to make sure someone has not submitted malicious code, but this is beyond the scope of this
    $name = $_POST;
    $email = $_POST;
    $comments = $_POST;

    // Now we need to build up the body of the email.
    // We will do this with what is called heredoc http://ie.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

    $email_body = <<<EOB
    You have recieved a new email from your website!
    Name : $name
    Email : $email
    Comments : $comments
    EOB;

    // Now we will define the subject and to address for the email
    $toadd = "my@email.com";
    $subject = "New contact form enquiry!";

    // And now we send the email
    mail($toadd, $subject, $email_body);
    ?>
    [/php]Now, keep in mind that I am delibratly leaving out things here, such as error checking, data sanatization etc. But if you want to know more about them, there are a million and one introductory php tutorials available online.

    You will also probably want to do a "Thank you for your enquiry, we will get back to you, blah blah!" page. You can just stick the html in after the closing php (?>) tag on domail.php.


Advertisement