angelali Posted May 13, 2012 Share Posted May 13, 2012 I have created many forms and validated in my life in PHP, but today, I just made a form, and the validations are not working at all... In other words, the error messages are not being displayed either... The form is very long, here both the PHP and HTML codes below: The PHP: <?php //Form validations and data save to database if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['remail']) && isset($_POST['rpass']) && isset($_POST['rsecurity']) && isset($_POST['rsanswer']) && isset($_POST['rfirst']) && isset($_POST['rlast']) && isset($_POST['rimage']) && isset($_POST['rcountry']) && isset($_POST['rwebsite']) && isset($_POST['rabout']) && isset($_POST['rsector']) && isset($_POST['rsub']) && isset($_POST['rexperience'])&& isset($_POST['rwork']) && isset($_POST['ragree']) && isset($_POST['rcapt'])) { //Assigning variables, inserting security against SQL injection and XSS attacks and trim certain text fields $remail = mysql_real_escape_string(strip_tags(trim($_POST['remail']))); $rpass = mysql_real_escape_string(strip_tags(trim($_POST['rpass']))); $rsecurity = mysql_real_escape_string(strip_tags($_POST['rsecurity'])); $rsanswer = mysql_real_escape_string(strip_tags($_POST['rsanswer'])); $rfirst = mysql_real_escape_string(strip_tags($_POST['rfirst'])); $rlast = mysql_real_escape_string(strip_tags($_POST['rlast'])); $rcountry = mysql_real_escape_string(strip_tags($_POST['rcountry'])); $rwebsite = mysql_real_escape_string(strip_tags($_POST['rwebsite'])); $rabout = mysql_real_escape_string(strip_tags($_POST['rabout'])); $rsector = mysql_real_escape_string(strip_tags($_POST['rsector'])); $rsub = mysql_real_escape_string(strip_tags($_POST['rsub'])); $rexperience = mysql_real_escape_string(strip_tags($_POST['rexperience'])); $rwork = mysql_real_escape_string(strip_tags($_POST['rwork'])); $ragree = strip_tags($_POST['ragree']); $rcapt = strip_tags($_POST['rcapt']); //Profile image $rimage = mysql_real_escape_string(strip_tags($_FILES['rimage']['name'])); $size = $_FILES['rimage']['size']; $max = 1048576; $extension = strtolower(substr($rimage, strpos ($rimage, '.') +1)); //Validation if (empty($remail) || empty($rpass) || empty($rsecurity) || empty($rsanswer) || empty($rfirst) || empty($rlast) || empty($rcountry) || empty($rabout) || empty($rsector) || empty($rsub) || empty($rexperience) || empty($rwork) || empty($ragree) || empty($rcapt)) { echo '<p class="error">Fields marked with an asterisk are required!</p>'; } else if (!filter_var($remail, FILTER_VALIDATE_EMAIL)) { echo '<p class="error">Email address seems not valid!</p>'; } elseif ($size > $max) { echo '<p class="error">Your image must not exceed 1MB</p>'; } else if ($extension !== 'jpg' && $extension !== 'jpeg' && $extension !=='gif' && $extension !=='png') { echo '<p class="error">Only images in JPG, JPEG, GIF and PNG are acceptable!</p>'; } else if (!filter_var($rwebsite, FILTER_VALIDATE_URL)) { echo '<p class="error">Website URL seems not valid, do include HTTP or HTTPS in your link!</p>'; } else if (str_word_count($rabout) > 250) { echo '<p class="error">The number of words in all textareas is 250!</p>'; } else if (str_word_count($rwork) > 250) { echo '<p class="error">The number of words in all textareas is 250!</p>'; } else if (!($rcapt === '30')) { echo "<p class='error'>Wrong captcha answer! Try again!</p>"; } } } ?> The HTML: <form action="register.php" method="post" enctype="multipart/form-data" novalidate> <table width="1100"> <tr> <td><label>Your email address: <span class="ads">(Will also be displayed on profile page)</span> <span class="required">*</span></label></td> <td><input type="email" name="remail" required size="39"/></td> </tr> <tr> <td><label>Your password: <span class="required">*</span></label></td> <td><input type="password" name="rpass" required size="39"/></td> </tr> <tr> <td><label>Security question: <span class="ads">(In case you forget your password)</span> <span class="required">*</span></label></td> <td> <select required name="rsecurity"> <option value="">Please select a question</option> <option value="Mobile number">What is your first mobile humber?</option> <option value="School name">What is your school name?</option> <option value="School year">Which year you completed college?</option> <option value="Hobby">What is your best hobby?</option> <option value="Computer">What is your best quote?</option> </select> </td> </tr> <tr> <td><label>Answer: <span class="required">*</span></label></td> <td><input type="text" name="rsanswer" required size="39"/></td> </tr> <tr> <td><label>Your first name: <span class="required">*</span></label></td> <td><input type="text" name="rfirst" required size="39"/></td> </tr> <tr> <td><label>Your last name: <span class="required">*</span></label></td> <td><input type="text" name="rlast" required size="39"/></td> </tr> <tr> <td><label>Your profile picture: <span class="ads">(An avatar will appear in case you do not upload any profile pic)</span></label></td> <td><input type="file" name="rimage" size="38"/><br/> <span class="adss">Only .jpg, .jpeg, .gif, .png formats and not more than 1MB in size!</span> </td> </tr> <tr> <td><label>Your country: <span class="required">*</span></label></td> <td> <select required name="rcountry"> <option value="">Please select a country</option> <option value="Afghanistan">Afghanistan</option> <option value="Åland Islands">Åland Islands</option> <option value="Albania">Albania</option> <option value="Algeria">Algeria</option> <option value="American Samoa">American Samoa</option> <option value="Andorra">Andorra</option> <option value="Angola">Angola</option> <option value="Zimbabwe">Zimbabwe</option> </select> </td> </tr> <tr> <td><label>Your website: <span class="ads">(Include HTTP or HTTPS)</span></label></td> <td><input type="url" name="rwebsite" size="39"/></td> </tr> <tr> <td><label>Tell us about you: <span class="ads">(In 250 words, you can describe what kind of person you are)</span> <span class="required">*</span></label></td> <td><textarea required cols="50" rows="8" name="rabout" id="worda"></textarea></td> </tr> <tr> <td><label>Select category of sector: <span class="required">*</span></label></td> <td> <select required name="rsector"> <option value="">Please select a computer sector</option> <option value="Technical">Technical</option> <option value="Systems Administration">Systems Administration</option> <option value="Network Administration">Network Administration</option> <option value="Database Development">Database Development</option> <option value="Web Development">Web Development</option> <option value="Multimedia">Multimedia</option> <option value="Security">Security</option> <option value="Software Engineering">Software Engineering</option> <option value="Mobile Devices">Mobile Devices</option> <option value="Robotics">Robotics</option> <option value="Telecommunication">Telecommunication</option> <option value="Information Systems">Information Systems</option> <option value="Marketing">Marketing</option> <option value="Office">Office</option> <option value="Others">Others</option> </select> </td> </tr> <tr> <td><label>Specify a sub-category: <span class="ads">(Example: System administrator...)</span> <span class="required">*</span></label></td> <td><input type="text" name="rsub" required size="39"/></td> </tr> <tr> <td><label>Years of experiences in this sector: <span class="ads">(Also for personal experiences if you do not have working experiences)</span> <span class="required">*</span></label></td> <td> <select required name="rexperience"> <option value="">Please select years of experiences</option> <option value="0 to 1 year">0 to 1 year</option> <option value="1 to 2 years">1 to 2 years</option> <option value="2 to 3 years">2 to 3 years</option> <option value="3 to 4 years">3 to 4 years</option> <option value="4 to 5 years">4 to 5 years</option> <option value="5 years and above">5 years and above</option> </select> </td> </tr> <tr> <td><label>Working/Personal experiences details: <span class="ads">(In 250 words, write about your experiences)</span> <span class="required">*</span></label></td> <td><textarea required cols="50" rows="8" name="rwork" id="worde"></textarea></td> </tr> <tr> <td><label>Accept the terms and agreements: <span class="required">*</span></label></td> <td><input type="checkbox" value="Accept" name="ragree" required/> <span class="terms">Read the terms and agreements</span></td> </tr> <tr> <td><label>Are you human? Solve this: 25 - 10 * 2: <span class="required">*</span></label></td> <td><input type="text" required size="5" name="rcapt"/></td> </tr> <tr> <td></td> <td><input type="submit" value="Create profile account"/></td> </tr> </table> </form> NOTE: I used HTML5 and I set it to 'novalidate' in the form so that I can test the PHP. Only the PHP is not working... As you see in the codes, I performed validations only to test if everything is ok but it seems it is not working as I am not receiving any error message. For example, some fields are required and if a user does not fill them, a message will be displayed, but when I test them by not filling anything, no error message is shown. So, its not working it seems. I tested the codes, and Im not finding any mistake... Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/ Share on other sites More sharing options...
xyph Posted May 13, 2012 Share Posted May 13, 2012 You're requiring $_POST['rimage'] to be set. It will never be set. Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345135 Share on other sites More sharing options...
angelali Posted May 13, 2012 Author Share Posted May 13, 2012 'rimage' is for a input type file to upload image... So, I removed it? Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345136 Share on other sites More sharing options...
angelali Posted May 13, 2012 Author Share Posted May 13, 2012 *I mean I remove the $_POST(['rimage']) and put isset($_files(['rimage'])? Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345137 Share on other sites More sharing options...
xyph Posted May 13, 2012 Share Posted May 13, 2012 Well, it's not marked as a required field on your form... I shouldn't be reminding you of your own requirements Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345138 Share on other sites More sharing options...
angelali Posted May 13, 2012 Author Share Posted May 13, 2012 Can you explain me what you mean? Yes, its not a required field, I mean can choose to not upload his pic, but I put isset as I dont uset isset($_post['submit']); Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345139 Share on other sites More sharing options...
xyph Posted May 13, 2012 Share Posted May 13, 2012 You shouldn't check that it's set until you attempt to upload the image. Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345145 Share on other sites More sharing options...
angelali Posted May 13, 2012 Author Share Posted May 13, 2012 I removed the image code itself to test, still not work...very weird problem for me seriously..never i encountered this... Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345147 Share on other sites More sharing options...
xyph Posted May 13, 2012 Share Posted May 13, 2012 Not work doesn't help us debug anything. When I remove the isset($_POST['rimage']), the script starts checking for valid fields. Perhaps you should print_r($_POST) to see what your post data actually contains. Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345150 Share on other sites More sharing options...
angelali Posted May 13, 2012 Author Share Posted May 13, 2012 I did remove it as I told you, and did not work... here is the print result: Array ( [remail] => [rpass] => [rsecurity] => [rsanswer] => [rfirst] => [rlast] => [rcountry] => [rwebsite] => [rabout] => [rsector] => [rsub] => [rexperience] => [rwork] => [rcapt] => ) Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345151 Share on other sites More sharing options...
xyph Posted May 13, 2012 Share Posted May 13, 2012 You should check what is set in $_POST, and what you're checking for in your long IF statement. It's not working because you've told it not to, with the information you've provided it. Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345154 Share on other sites More sharing options...
angelali Posted May 13, 2012 Author Share Posted May 13, 2012 Wait, now I'm not understand you... You told me to remove isset post right? I did make a form like this before, same procedure, and never I met a problem... Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345155 Share on other sites More sharing options...
angelali Posted May 13, 2012 Author Share Posted May 13, 2012 I removed all the 'isset', and the form is being validated.. but tell me one thing, in all tutorials, and myself I have used isset($_post['xxx']) in forms to check, and they worked perfectly... Now Im getting Undefined Index in 'ragree' when I did declare the variables and also did write it ell... Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345156 Share on other sites More sharing options...
xyph Posted May 13, 2012 Share Posted May 13, 2012 That's because you're not actually looking at your code. From the information provided in Reply #9, it took me less than 10 seconds to go through your IF conditional and find out why it's working the way you've told it to. Keep in mind, the way you've PROGRAMMED this to work may be different than the way you EXPECT it to work. Accuracy is important, and if you don't program it to behave the way you want, unwanted behaviour WILL happen. Before you say "it doesn't work," you should actually check and see HOW it works. Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345157 Share on other sites More sharing options...
xyph Posted May 13, 2012 Share Posted May 13, 2012 You cant assume a client will send every empty element along with the POST request. In this case, the empty check box isn't being sent at all. This was made apparent when you gave me your print_r($_POST) return. The general issue here is you can never trust a client to send the right information. You always have to verify, and assume it's bad until you've determined otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345158 Share on other sites More sharing options...
angelali Posted May 13, 2012 Author Share Posted May 13, 2012 My head is heavy right now, to close this problem here...just tell me what to change thats all... If I have understand well, we dont use 'isset' for fields which are not required to fill? Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345159 Share on other sites More sharing options...
angelali Posted May 13, 2012 Author Share Posted May 13, 2012 Oh now i understand, yes, you are right.... thank you... Quote Link to comment https://forums.phpfreaks.com/topic/262481-validations-in-forms-are-not-working-help/#findComment-1345160 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.