Tech-Funda

…in touch with tomorrow









Building Feedback Form with php

Posted by admin on Dec-10-2007

Here we are going to create a feedback form for you site to allow users contact you.
The script is very simple and this article is for php begginers. we have to follow following steps for that.

Design a Feedback Form
The first thing we need to do is to design the feedback form. following piece code will build a form for you. here i am going to save it in html file say, feedback.html.

<form  name=“frmFeedback” method=“post” action=“sendmail.php”>
<fieldset class=“repeat”>
<legend>Feedback…</legend>
<label class=“preField”>
Name:
<input name=“name” type=“text” />
</label>
<label class=“preField”>Email:
<input name=“email” type=“text” />
</label>
<label class=“preField”>Message:<br />
<textarea name=“message” rows=“8″ cols=“40″>
</textarea></label>
<input type=“button” value=“Submit” onClick=“return vldFrm_Feedback();”/>
</fieldset>
</form>

Basically the form asks a visitor for his Name, email address and message, and presents him with a button which he can click to submit the contents of the form.
When the form is submitted, it is “posted” (see the “method” attribute of the tag) to “sendmail.php”.

Improve look of feedback form
i have apply CSS on above code to improve its look. See look of form below…

You can get greate ideas to apply CSS on forms at http://www.formassembly.com/
and the CSS code is as followes…

<style type=“text/css” media=“all” >
form {
        padding: 10px;
        font-family: ‘Trebuchet MS’, ‘Lucida Grande’, Verdana, Arial, Sans-Serif;
        background-color:#F8F8F6;
        background-image: url(formBg.png);
        border: 1px dotted #878177;     
}
fieldset {
        margin: 20px 0;
        padding: 15px 10px;
        background-color:#F4F4F2;
        border: 3px double #878177;     
}
legend {
        padding: 2px 5px;
        color: #1C1C1C;
        background-color: #FFD;
        border: 1px solid #878177;
}
label.preField {
        display: block;
        padding: 2px;
        margin: 0.4em 4px 0 0;
        font-weight: bold;
}

</style>

Validating form Input
As user fill form and send it, we are going to validate input to confirm that user not sending a blank form.
on onClick event of submit button we are going to call Javascript function vldFrm_Feedback() to validate our feedback form.
following code will do it for you…

<script type=“text/javascript”>
function vldFrm_Feedback(){
        var result = true;
        var msg=“”;
       
        if (document.frmFeedback.name.value==“”) {
                        msg+=“Your Name please !\n;
                        result = false;
                        }
        if (document.frmFeedback.email.value==“”) {
                        msg+=“Your Email please !\n;
                        result = false;
                        }
        if (document.frmFeedback.message.value==“”) {
                        msg+=“Your Message is Required !”;
                        result = false;
                        }
       
        if(msg==“”){
        return result;
        }{
        alert(msg)
        return result;
        }
}
</script>

Process the form using PHP
Now all that remains is to code “sendmail.php”. This is made extremely easy by the facilities available in PHP. Type the following code into a file named “sendmail.php”.

<?php
$Name = $_POST[‘name’] ;
$email = $_POST[‘email’] ;
$message = $_POST[‘message’] ;

$message1=“You have new feedback from”.$Name.“<br /><br />”.$message.“”;

mail( “youremail@example.com”, “Feedback Form…”, $message1, “From: $email” );
echo‘Thanks You for your feedback…<a href="feedback.html" title="Back to Feedback form…">Back</a>’
?>

Here you change youremail@example.com with email on which you want to get user feedback…

Source code
Download Source code for ‘building Feedback Form with php’
Download


  1. nnn Said,

    good

  2. trieu van Said,

    thank

  3. marian Said,

    muy beuno, grx

    nice, thank you!

  4. mathabo Said,

    how do i change the look on the form to be like yours.I tried editing the CSS in front page but i just cant .i need it excacly like yours please HElp!!

  5. dle Said,

    thanks for your posting.

  6. admin Said,

    Hi Mathabo,
    You Download Source file and edit the Form as per your requirement.
    If have any problem let me know.

    Regards
    Chetankumar Akarte

  7. Faisal Said,

    I have designed a form in Flash and want to link it through asp in got mails using feedback form. Can you send me a script

  8. Faisal Said,

    Hi,
    I have a feedback form in flash and my server does not support php. and i want to get mails for it. So can you send me coding which can create link in flash and asp.

    Kind REgards
    Faisal

  9. chris Said,

    I can get the form to send me a conformation that some added feedback, I can’t however get the feedback to be posted on the webpage? Did I misinterpret what this source code does?

  10. Chetankumar Akarte Said,

    Hi Chris,

    You go through the Source Code available for download. and PHP help to understand what the code does. so you can get more clear idea about the working of above code.

    Regards
    Chetankumar Akarte

  11. yoyo Said,

    Thank for guide us! (”,)

Add A Comment

*
To prove that you're not a bot, enter this code
Anti-Spam Image