Before submit a form to a server script, it is indicated to perform some minimal validations on the client side, like the number of characters in a field and the correct form of an e-mail address.
<form action="script.php" method="post"> Name: <input type="text" name="name" id="name" /><br /> E-mail: <input type="text" name="email" id="email" /><br /> <span id="gen"> Gender: <input type="radio" name="gen" value="man" />Man <input type="radio" name="gen" value="woman" />Woman </span><br /> Age, between: <select name="age" id="age"> <option>..select..</option> <option value="5-10">5-10</option> <option value="11-18">11-18</option> <option value="19-30">19-30</option> <option value="31-50">31-50</option> <option value="51-70">51-70</option> <option value="71+">71+</option> </select><br /> Message:<br /> <textarea name="msg" id="msg" rows="4" cols="33"></textarea><br /> <input type="submit" name="submit" value="Submit" /> </form>
<style type="text/css"><!-- .error { border:2px solid red; } --></style> <script type="text/javascript" src="jquery_1.6.1.js"></script> <script type="text/javascript"><!-- $(document).ready(function() { // when a form is submited, get its data and validate the fields $('form').submit(function() { // gets field values var name = $(this).find('input[name="name"]'); // Name var email = $(this).find('input[name="email"]'); // E-mail var gen = $(this).find(':radio:checked'); // Gender var age = $(this).find('select[name="age"]'); // Age var msg = $(this).find('#msg'); // Message // remove class "error" from al elements // sets a variable "error" used to deermine if submit or no the form $('*').removeClass('error'); var error = 0; var regx = /^([a-z0-9_\-\.])+\@([a-z0-9_\-\.])+\.([a-z]{2,4})$/i; // regexp for e-mail /* Validate the fields (adds class "error" to invalid fields and set error=1) */ // check "name" if(name.val().length<2){ name.addClass('error'); error = 1; } // check "email" if(!regx.test(email.val())){ email.addClass('error'); error = 1; } // check radio input "gen", if undefined (not checked), // add class "error" to element that includes radio inputs if(gen.val()==undefined){ $('#gen').addClass('error'); error = 1; } // check "age", if no selected (value of <select> is the value of first <option>, initially displayed) if(age.val()==age.find('option:first').val()){ age.addClass('error'); error = 1; } // check "msg" if(msg.val().length<5){ msg.addClass('error'); error = 1; } // if error is 0, submit the form // else alerts a message and blocks the submission by returning false if(error==0) { $(tihs).submit(); } else { alert('Please fill all fields with red border'); return false; // necesary to not submit the form } }); }); --></script>
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>jQuery Validate Submit</title> <style type="text/css"><!-- .error { border:2px solid red; } --></style> <script type="text/javascript" src="jquery_1.6.1.js"></script> <script type="text/javascript"><!-- $(document).ready(function() { // when a form is submited, get its data and validate the fields $('form').submit(function() { // gets field values var name = $(this).find('input[name="name"]'); // Name var email = $(this).find('input[name="email"]'); // E-mail var gen = $(this).find(':radio:checked'); // Gender var age = $(this).find('select[name="age"]'); // Age var msg = $(this).find('#msg'); // Message // remove class "error" from al elements // sets a variable "error" used to submit or no the form $('*').removeClass('error'); var error = 0; var regx = /^([a-z0-9_\-\.])+\@([a-z0-9_\-\.])+\.([a-z]{2,4})$/i; // regexp for e-mail /* Validate the fields (adds class "error" to invalid fields and set error=1) */ // check "name" if(name.val().length<2){ name.addClass('error'); error = 1; } // check "email" if(!regx.test(email.val())){ email.addClass('error'); error = 1; } // check radio input "gen", if undefined (not checked), // add class "error" to element that includes radio inputs if(gen.val()==undefined){ $('#gen').addClass('error'); error = 1; } // check "age", if no selected (value of <select> is the value of first <option>, initially displayed) if(age.val()==age.find('option:first').val()){ age.addClass('error'); error = 1; } // check "msg" if(msg.val().length<5){ msg.addClass('error'); error = 1; } // if error is 0, serialize form data and send the data string to server // else alert a message if(error==0) { var srl = $(this).serialize(); $.ajax({ type: 'post', url: 'script.php', data: srl, beforeSend: function() { // before send the request, displays a "Loading..." messaj in the element where the response will be placed $('#resp').html('Loading...'); }, timeout: 10000, // sets timeout for the request (10 seconds) error: function(xhr, status, error) { alert('Error: '+ xhr.status+ ' - '+ error); }, // alert a message in case of error success: function(response) { $('#resp').html(response); } }); } else { alert('Please fill all fields with red border'); } return false; // necesary to not open the page when form is submited }); }); --></script> </head> <body> <div id="resp">Here will be displayed the response from server.</div> <h4>Fill the form and submit data</h4> <form action="script.php" method="post"> Name: <input type="text" name="name" id="name" /><br /> E-mail: <input type="text" name="email" id="email" /><br /> <span id="gen"> Gender: <input type="radio" name="gen" value="man" />Man <input type="radio" name="gen" value="woman" />Woman </span><br /> Age, between: <select name="age" id="age"> <option>..select..</option> <option value="5-10">5-10</option> <option value="11-18">11-18</option> <option value="19-30">19-30</option> <option value="31-50">31-50</option> <option value="51-70">51-70</option> <option value="71+">71+</option> </select><br /> Message:<br /> <textarea name="msg" id="msg" rows="4" cols="33"></textarea><br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html>Demo:
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);