#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Validation of HTML Forms and Email via PHP


    Hi,
    I am new to web development (just 2 months).

    I have been given a task to create a web page with an HTML form and upon clicking the "SUBMIT" button, the information in the form has to be emailed (a simple contact us form).
    I have almost done it. This is how I did it.

    Upon pressing the SUBMIT button, its directing to sendit.php file, where its doing the emailing part. My Question is, if the email fails, I want to come back to the contact-us page and to display the information that the user has typed in, how to do it? When I come back to the contact-us page, all the information typed are initialized to blanks. Please throw some light on it.

    I am using HTML, PHP, CSS & JavaScript.

    thanks,

    Isaac
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    send the form to the same script and do the email stuff there. If there's an error, you can display the form again with the posted data already filled in. Otherwise, you can redirect to some kind of success page.

    Sending a form to another script makes validation rather complicated, because you have to store the data somewhere (like in cookie or the session) or display the same form in a different script. Both is unnecessary and ugly.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    This is the code


    This is the code I wrote:

    <html>
    <head>

    <script language="javascript">
    function validateForm()
    {

    //if wrong data return false;


    return true;
    }



    function showResponse()
    {
    var prmstr = window.location.search.substr(1);
    var prmarr = prmstr.split ("&");
    var params = {};
    for ( var i = 0; i < prmarr.length; i++)
    {
    var tmparr = prmarr[i].split("=");
    params[tmparr[0]] = tmparr[1];
    }
    if( params[tmparr[0]] == 1 )
    document.write("Thank you for contacting Us, We will get back to you soon");
    else
    if ( params[tmparr[0]] == 2 )
    {
    document.write("Message was not sent, error occurred, please try again later");
    }
    }



    </script>


    </head>





    <body>

    //this is to find if the page is kind of postback.
    <script language="javascript">
    showResponse();
    </script>



    <form action="/sendit.php" method="post" name="form1" onsubmit="return valiadateForm();">
    <input name="user_name" type="text" id="user_name" />
    <input type="submit" name="Submit" value="Submit"/>
    <input type="reset" name="Reset" value="Reset" />
    </form>


    </body>
    </html>





    sendit.php:

    [PHPNET="php code for email"]<?php


    //Build the message

    $name = $_POST["user_name"];
    $email = $_POST["user_email"];
    $phone = $_POST["user_phone"];
    $subj = $_POST["user_subj"];
    $mesg = $_POST["user_mesg"];

    $message = $message . "Name: " . "$name \r";
    $message = $message . "Email: " . "$email \r";
    $message = $message . "Phone Number: " . "$phone \r";
    $message = $message . "Subject: " . "$subj \r";
    $message = $message . "Message/Comments: " . "$mesg \r";

    $headers="From: " . $_POST["user_email"];



    if (mail("info@hotmail.com", $_POST["user_subj"], $message, $headers))
    {
    print ("Thank you for contacting us, we will respond to you shortly.");

    echo '<script type="text/javascript">';
    echo 'window.location = "/contact-form.php?send=1"; ';
    echo '</script>';

    }
    else
    {
    print ("There was a problem with your form, please go back and try again. Thank you");
    }


    ?>[/PHPNET]

IMN logo majestic logo threadwatch logo seochat tools logo