Nandini Posted August 20, 2008 Share Posted August 20, 2008 Hi i have a registration form. There is two categories. One is peer, contains three text fileds named as peer[] like '<input type="text" name="peer[]">' etc like this 3 fields. Another one is user, this is also contain three text fields named as user[] like '<input type="text" name="user[]">' etc like this 3 fields. I want to insert either peer values or user values or both. But dont accept if atleast one of them not entered. So i want to validate the form. Because of the text fields names as peer[] like array validations are not done. Here is my code. pls check it and give advice pls. <? extract($_POST); $error_colour = "red"; $lang_peeruser = "Enter peer or user details\n"; if ((empty($_POST['peer'])) || (empty ($_POST['user']))) { $error = "1"; $info_error .= "".$lang_peeruser . ""; } if ($error == "1") { $info_notice = "<span style=\"color: " . $error_colour . "; font-weight: bold;\">" . $lang_error . "</span><br>"; if (empty ($submit)) { $info_error = ""; $info_notice = $lang_notice; } ?> <form method="post" action=""> <table border="1" align="center"> <tr align="left" valign="top"> <td colspan="2"><?php echo "<font color=red>".$info_notice.$info_error. "</font>" ?></td> </tr> <tr><td colspan="4" align="center">Outgoing</td></tr> <tr><td valign="top">Peer:</td> <td><input type="text" name="peer[]" /><br /> <input type="text" name="peer[]" /><br /> <input type="text" name="peer[]" /> </td></tr> <tr><td colspan="4" align="center">Incoming</td></tr> <tr><td valign="top">User:</td> <td><input type="text" name="user[]" /><br /> <input type="text" name="user[]" /><br /> <input type="text" name="user[]" /> </td></tr> <tr><td colspan="2" align="center"> <input type="submit" name="submit" value="submit" /> </td></tr> </table> </form> } else { /////////////database here if(isset($_POST['peer'])) { $value = $_POST['peer']; $n = count($peer); $i = 0; while ($i < $n) { echo $peer[$i]." \r\n"; //////print entered details $i++; } } } ?> Link to comment https://forums.phpfreaks.com/topic/120472-solved-php-form-validation/ Share on other sites More sharing options...
Fadion Posted August 20, 2008 Share Posted August 20, 2008 If i get the idea right, you want to accept the input only if all 3 peers or all 3 users are entered. You can always give the text inputs different names (peer1, peer2, peer3), but for your approach, you can use count() to check how many elements the array has. You know it will have 3 elements so: <?php $peers = array_values(array_filter($_POST['peers'])); //two array functions to remove empty elements. use: $peers = array_filter($_POST['peers']); if you want to preserve keys $users = array_values(array_filter($_POST['users'])); if(count($peers) == 3 or count($users) == 3){ //validate only if one of the arrays (peers or users) has 3 elements //the form validates correctly //the rest of your code } ?> Link to comment https://forums.phpfreaks.com/topic/120472-solved-php-form-validation/#findComment-620906 Share on other sites More sharing options...
Nandini Posted August 20, 2008 Author Share Posted August 20, 2008 Thanq very much. No one reply me till now. You r the first one. I will try ur code. Thanq Link to comment https://forums.phpfreaks.com/topic/120472-solved-php-form-validation/#findComment-620915 Share on other sites More sharing options...
Nandini Posted August 20, 2008 Author Share Posted August 20, 2008 Hi GuiltyGear. I tried ur code. I got following error. Warning: array_filter() [function.array-filter]: The first argument should be an array in /var/www/admin/trunks/sample.php on line 15 Warning: array_values() [function.array-values]: The argument should be an array in /var/www/admin/trunks/sample.php on line 15 Warning: array_filter() [function.array-filter]: The first argument should be an array in /var/www/admin/trunks/sample.php on line 16 Warning: array_values() [function.array-values]: The argument should be an array in /var/www/admin/trunks/sample.php on line 16 Link to comment https://forums.phpfreaks.com/topic/120472-solved-php-form-validation/#findComment-620917 Share on other sites More sharing options...
Fadion Posted August 20, 2008 Share Posted August 20, 2008 Change the first two lines to: <?php $peers = array_values(array_filter($_POST['peer'])); $users = array_values(array_filter($_POST['user'])); ?> Your textfields were named 'peer' and 'user' while i renamed them 'peers' and 'users'. Link to comment https://forums.phpfreaks.com/topic/120472-solved-php-form-validation/#findComment-620919 Share on other sites More sharing options...
Nandini Posted August 20, 2008 Author Share Posted August 20, 2008 Thanx gu. You are very supportive on me. Total validations are working for me. But when arrive form the same errors are occuring. When i click submit button they gone out. Again i click the url for this form the form will display along these errors. Link to comment https://forums.phpfreaks.com/topic/120472-solved-php-form-validation/#findComment-620932 Share on other sites More sharing options...
Fadion Posted August 20, 2008 Share Posted August 20, 2008 You need to check first if the submit button is clicked, which means that the form itself has been submitted. Just add an if() which contains all your form validation code: <?php if(isset($_POST['user'])){ //if this and that //if this and that } //your html forms and stuff ?> Link to comment https://forums.phpfreaks.com/topic/120472-solved-php-form-validation/#findComment-620936 Share on other sites More sharing options...
Nandini Posted August 21, 2008 Author Share Posted August 21, 2008 Thanq very much Gear. its working for me now. thanq Link to comment https://forums.phpfreaks.com/topic/120472-solved-php-form-validation/#findComment-621804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.