Jump to content

Recommended Posts

http://pastehtml.com/view/199g5im.txt

 

It was made from a tutorial but I modified the "from:" part to show the actual input of the user in the email header. I tested it and it works perfectly. I'm curious if anyone sees any flaws or something that can be improved.

 

This is my first php form folks :)

Link to comment
https://forums.phpfreaks.com/topic/201521-simple-feedback-form-what-do-you-think/
Share on other sites

Hey, nice job. One thing that you absolutely positively can't be without is validation. I've got a ton of sites that I've developed and even the least popular with say less than 100 visitors a month get their forms spammed by bots and people who don't take the time to fill everything in correctly.

 

So the whole point of validation is to make sure that the form is filled in the way you want it to be, so you want to start by adding a check to make sure that the return email address is correctly formatted (for example check there is a @ sign and at least one period [.]) and that the subject and message are filled in.

 

Then if you want to make it really user friendly add javascript validation on top of your PHP validation, this will give the user a nice experience when they insert data incorrectly.

 

Good luck and have fun.

 

PS The functions you want are strpos (to determine if a character exists, the function returns true if there is and false if not) and strlen (determines the string length, for subject and message fields).

Rather than strpos, check if it is a vaild email, with php's built in email validation:

 

http://php.net/manual/en/function.filter-var.php

 


$email=$_POST['emailfield'];

if(filter_var($email, FILTER_VALIDATE_EMAIL)!==false){

//process

}else{

//error

}//end if

 

Once you have that in, you should lay some javascript over the form to make it nicer on the user.

 

This js is much simpler than it could be, but any errors it misses, would still be captured by your php, which should be the lead anyway:

 


<script type="text/javascript">

function isEmail(emailAddr){
    
    var reg = new RegExp("^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");
    return reg.test(emailAddr);

}//end function

function validateForm(){

    var email=document.getElementById('emailfield').value;
    
    if(!isEmail(email)){
    
        alert('The entered email is invalid!');
        return false;
        
    }else{
    
        return true;
        
    }//end if

}//end function

</script>
            
<form action="/index.php" method="post" onsubmit="return validateForm();">

<input type="text" name="emailfield" id="emailfield">

</form>

 

Like I said, very simple, but you get the idea.

 

[/code]

 

Here is a tutorial for some jquery vaildation: http://www.reynoldsftw.com/2009/03/live-email-validation-with-jquery/

 

All it really does is add some more complex regex (still you'll need the php validation), and I believe it works with keypresses rather than the form submission.

 

At any rate, jQuery is amazing, and my framework of choice, but in the example I provided you, probably the only thing that you would change to jQuery is:

 


var email=document.getElementById('emailfield').value;

//to

var email=$("#emailfield").val(); //edit, forgot the "#"

 

If you are going to go the jQuery route, it is really important that you understand the underlying javascript, as all jQuery is, is an "interface" used to automate / speed up common javascript stuff.

 

It is really awesome when it comes to stuff like DOM traversal.

 

Edit #2: I guess if you wanted, you could also bind the event to the form using jQuery, so there is no junk in your HTML if the user has no JS. This would require adding an id to your form:

 


$(document).ready(function(){

$("#myform").submit(function(){

validateForm();

});

});

Seventheyejosh I was checking some jquery form tutorials and there are so many variation. I did some jquery things before like content sliders for wordpress by copying, pasting, and a lot of guesswork. I'll have to dedicate some time to really learn Jquery. It would be awesome to know how to integrate flash actionscript with javascript. Thanks for the tips and link! I'll see if I can integrate it into the form :)

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.