Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Order form ~ PHP ??

  • 27-07-2009 10:10PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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