Drezard Posted September 24, 2006 Share Posted September 24, 2006 Hello, I was wondering. If I had this script:[CODE]<form>Name: <input type:'text' name:'name'></form>if (!isset($_POST['name'])) {// Run code'}?>[/CODE]Some websites (im not sure if its HTML, PHP or Javascript) when you dont input values into a form then submit they highlight the bit of the form that you didnt fill in then in writing under it put "Please insert you name". And the form is still on the page. How do you do that in PHP.- Cheers, Daniel Quote Link to comment https://forums.phpfreaks.com/topic/21828-html-forms/ Share on other sites More sharing options...
invincible_virus Posted September 24, 2006 Share Posted September 24, 2006 why would you like to do it using PHP, when this can be easily done using client side scripting (javascript) ?? Quote Link to comment https://forums.phpfreaks.com/topic/21828-html-forms/#findComment-97471 Share on other sites More sharing options...
SharkBait Posted September 24, 2006 Share Posted September 24, 2006 It can be done either way.PHP would have to wait till the form is submitted and the server would have to check for you, whereas Javascript would do it before any of the information was sent to the server. To do it with PHP would take a bit more code than I believe it would with javascript. You would have to set up an array of values that were incorrect/missing etc and check them on the refresh of the page. To me it's a bit hard to explain but it can be done.[code]<?phpif(isset($_POST['submit'])) { // user submitted form if(isset($_POST['name'])) { // do your stuff here }} // Show form to user?><form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <input type="text" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name'];?>" ?> <input type="submit" value="Submit" name="submit" /></form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/21828-html-forms/#findComment-97473 Share on other sites More sharing options...
Drezard Posted September 24, 2006 Author Share Posted September 24, 2006 how would u do it with javascript then? Quote Link to comment https://forums.phpfreaks.com/topic/21828-html-forms/#findComment-97477 Share on other sites More sharing options...
Drezard Posted September 24, 2006 Author Share Posted September 24, 2006 plz help.- Cheers, Daniel Quote Link to comment https://forums.phpfreaks.com/topic/21828-html-forms/#findComment-97517 Share on other sites More sharing options...
fenway Posted September 25, 2006 Share Posted September 25, 2006 You simply need to write a routine that goes though a list of "required" fields, and validates each according to input type. Quote Link to comment https://forums.phpfreaks.com/topic/21828-html-forms/#findComment-97950 Share on other sites More sharing options...
KevinM1 Posted September 26, 2006 Share Posted September 26, 2006 A simple JavaScript form validator I created (external .js file):[code]var W3CDOM = (document.createElement && document.getElementsByTagName);function init(){ if (!W3CDOM) return; var inputform = document.getElementById('inputform'); inputform.onsubmit = validate;}function validate(evt){ evt = (evt) ? evt : ((event) ? event : null); if(evt){ var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); if(elem){ var name__not_empty, name, pass_not_empty, pass, email__not_empty, email; name_not_empty = isNotEmpty(elem.username); name = isName(elem.username); pass_not_empty = isNotEmpty(elem.password); pass = len16(elem.password); email_not_empty = isNotEmpty(elem.email); email = validEmail(elem.email); if(name_not_empty && name && pass_not_empty && pass && email_not_empty && email){ return true; } else { return false; } } }}function isNotEmpty(thing){ str = thing.value; re = /.+/; if(!str.match(re)){ alert(thing.id + " is empty!"); return false; } else { return true; }}function isName(thing){ str = thing.value; re = /^[a-zA-Z]+([ a-zA-Z-]+)*$/; if(!str.match(re)){ alert("'" + thing.value + "'" + " is not a name!"); return false; } else { return true; }}function len16(thing){ str = thing.value; re = /\b.{16}\b/; if(!str.match(re)){ alert("Your password is not 16 characters long!"); return false; } else { return true; }}function validEmail(thing){ str = thing.value; re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/; if(!str.match(re)){ alert("You did not enter a valid e-mail address!"); return false; } else { return true; }}window.onload = init;[/code]This isn't supposed to cure all your problems. Your form will most likely be different than mine, so you'd need to modify the validator's code to match what's in your form. But, at the very least, this should give you some idea on how to approach your problem. Quote Link to comment https://forums.phpfreaks.com/topic/21828-html-forms/#findComment-99058 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.