Jump to content

Validations in forms are NOT working! (HELP)


angelali

Recommended Posts

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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] => )

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.