ihatephp Posted December 17, 2008 Share Posted December 17, 2008 let's say i have a registration form to fill in. I have to fill in all the fields. If some fields are not filled in , there will be an error saying " please fill in all required fields". How do i go about doing it? Quote Link to comment Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 Checking the $_POST variables in the script receiving it and checking against what should/should not be allowed. Quote Link to comment Share on other sites More sharing options...
phpfreakjav Posted December 17, 2008 Share Posted December 17, 2008 use isset Quote Link to comment Share on other sites More sharing options...
ihatephp Posted December 17, 2008 Author Share Posted December 17, 2008 Any example with this isset or post that i can follow cos i'm really weak at php... thanks Quote Link to comment Share on other sites More sharing options...
dmccabe Posted December 17, 2008 Share Posted December 17, 2008 Ok lets say your form is like this: <form> <input type="text" name="testbox" value="<?php echo $_POST['testbox']; ?>"> <input type="submit" value="submit" name="submit"> </form> Then your PHP will be like this if (isset($_POST['submit'])) { //This checks to see if the submit button has been clicked if (isset($_POST['testbox'])) { //This checks to see if the "testbox" input field contains something $testbox = $_POST['testbox']; if (strlen($testbox) < 1) { //Checks to see if the length of the value in $testbox is less that 1 character echo "You must enter something in test box"; } } else { echo "Test box must have an value"; } } Quote Link to comment Share on other sites More sharing options...
ihatephp Posted December 17, 2008 Author Share Posted December 17, 2008 Is it the same as using javascript, though i tried it but it did not work cos i'm not very sure of javascript.. function validate_required(field,alerttxt) { with (field) { if (value==null||value=="") { alert(alerttxt);return false; } else { return true; } } } Quote Link to comment Share on other sites More sharing options...
ihatephp Posted December 17, 2008 Author Share Posted December 17, 2008 That code worked half, was a great help! Meaning the error message and all that showed up but it was after i have pressed the submit button. It should be...the error message should pop up when i press the submit button. any idea? Quote Link to comment Share on other sites More sharing options...
dmccabe Posted December 17, 2008 Share Posted December 17, 2008 php is a server side language, so it cant check the values until the page is reloaded after clicking submit. You would need Javascript to check it as soon as the button is pressed and without reloading the page Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 17, 2008 Share Posted December 17, 2008 Although JavaScript is a great userfriendly way to do some pre-validation, you should ALWAYS include validation server-side as well. Not everyone has JS enabled and someone could easily bypass any JavaScript validation to do some malicious things to your site. There's no problem in adding javascript valiadtion as long as it is on top of the server-side validation. Quote Link to comment Share on other sites More sharing options...
ihatephp Posted December 17, 2008 Author Share Posted December 17, 2008 I used these instead , not the javascript as i know nuts about it.. What i'm trying to achieve now is to keep the contents of all the required fields in the form after it has been filled wrongly ? and there should be a pop up error box saying " this field is not entered"... Ok lets say your form is like this: <form> <input type="text" name="testbox" value="<?php echo $_POST['testbox']; ?>"> <input type="submit" value="submit" name="submit"> </form> Then your PHP will be like this if (isset($_POST['submit'])) { //This checks to see if the submit button has been clicked if (isset($_POST['testbox'])) { //This checks to see if the "testbox" input field contains something $testbox = $_POST['testbox']; if (strlen($testbox) < 1) { //Checks to see if the length of the value in $testbox is less that 1 character echo "You must enter something in test box"; } } else { echo "Test box must have an value"; } } Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 17, 2008 Share Posted December 17, 2008 That code should do that. But, here is an example page that will combine the PHP and JavaScript validation. You can use it as a guide. Either turn off JS or take out the onsubmit trigger from the form to test the PHP validations. <?php //Create an array to hold the errors $errors = array(); //Create post values as var. This is useful in case //you need to run stripslashes or some other process. $name = $_POST['name']; $phone = $_POST['phone']; $email = $_POST['email']; //Validate the form data if (isset($_POST)) { if (empty($name)) { $errors[] = 'Name cannot be empty.'; } if (empty($phone)) { $errors[] = 'Phone cannot be empty.'; } if (empty($email)) { $errors[] = 'Email cannot be empty.'; } if (!count($errors)) { //Process the form data include('processing_page.php'); //Reload page to clear POST values (in case user refreshes page) header('Location: '.$_SERVER[php_SELF]) } else { $errorMsg = "The following errors occured:<br />\n"; $errorMsg = "<ul>\n"; foreach($errors as $error) { $errorMsg .= "<li>{$error}</li>"; } $errorMsg .= "</ul>\n"; } } ?> <html> <head> <script type="text/javascript"> //Add javascript prototype to trim string String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,''); } function validate(formObj) { //Create an array to hold all the errors errors = new Array(); //Perform each validation if (formObj['name'].value.trim()=='') { errors[errors.length] = 'Name cannot be empty.'; } if (formObj['phone'].value.trim()=='') { errors[errors.length] = 'Phone cannot be empty.'; } if (formObj['email'].value.trim()=='') { errors[errors.length] = 'Email cannot be empty.'; } //If errors dispay message and return false if (errors.length != 0) { var errorMsg = 'The following errors occured:\n'; for (var i=0; i<errors.length; i++) { errorMsg += '\n - ' + errors[i]; } alert(errorMsg); return false; } //No errors, return true return true; } </script> </head> <body> Please enter your data. All fields are required. <br /> <?php echo $errorMsg; ?> <br /> <form name="test" onsubmit="return validate(this);" method="POST" action=""> Name: <input type="text" name="name" value="<?php echo $name; ?>" /><br /> Phone: <input type="text" name="phone" value="<?php echo $phone; ?>" /><br /> Email: <input type="text" name="email" value="<?php echo $email; ?>" /><br /> <br /> <button type="submit">Proceed</button> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
ihatephp Posted December 18, 2008 Author Share Posted December 18, 2008 Thanks for the example as it was a really great help! I'd like to clarify smtg..For radio buttons..i put this but it din work, though it worked fine for all input type as text box. <input type="radio" name="gender" value="<?php echo $gender; ?>" /> if (formObj[gender].value.trim()=='') { errors[errors.length] = Gender cannot be empty.'; } Quote Link to comment Share on other sites More sharing options...
ihatephp Posted December 18, 2008 Author Share Posted December 18, 2008 Is there any1 who can help me wif this cos i cant move on being stuck on this too long.. :-\ I 'd like to use radio button but is this the correct code? for it din work for mine... if (formObj[gender].value.trim()=='') { errors[errors.length] = Gender cannot be empty.'; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 18, 2008 Share Posted December 18, 2008 if (formObj[gender].value.trim()=='') { errors[errors.length] = Gender cannot be empty.'; } Well, first off I will state that you should just pre-select one of the radio options since the user can only change the selected option and cannot unselect all of the options. But, there are some cases where you do not want to pre-select an option. The problem is that a radio GROUP is an array of elements - each with it's own value (however, if there is only one element in the group then it is not an array). You need to determine which element is CHECKED and return the value of that element. Add this function to your page: function radioGroupValue(groupObj) { //Check if radio group has multiple options (i.e. is an array) if (groupObj.length) { //Radio group has multiple options //Iterrate through each option for (var i=0; i<groupObj.length; i++) { //Check if option is checked if (groupObj[i].checked) { //Return value of the checked radio button return groupObj[i].value; } } } else { //Radio group only has one option if (groupObj.checked) { return groupObj.value; } } //No ption was selected return false; } Then change your validation to this: if (radioGroupValue(formObj['gender'])!==false) { errors[errors.length] = Gender cannot be empty.'; } Quote Link to comment 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.