Jump to content

Help with form validation


jackie11

Recommended Posts

Hi everyone

I have been trying to create some simple form validation but what I have created does not seem to be working, no error messages are dispalying and it just dispalys the page enven if he fields are not entered correctly

any suggestions?

Thanks

Jackie


[code]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>form_display.php</title>
  <link rel="stylesheet" href="etc/TJConsultants.css">
</head>
<body>

<br>

<div id="wrapper">
<div id="header">
<div style="text-align: center;"><br>

</div>
<br>
</div>
<div id="centercolumn">
<div style="text-align: center;"><br>
</div>


<div style="text-align: center;">
<hr style="width: 100%; height: 2px;"><font style="color: rgb(21, 27, 84);" size="+3">Employee Details<br>
</font>
<hr style="width: 100%; height: 2px;"><font style="color: rgb(21, 27, 84);" size="+3">
</font>

<?php

if (isset($_POST['submit'])) {

$errors = array();

if (!preg_match('|^[a z]+$|i', $_POST['Surname'])) $errors[] = "Surname can contain only letters!";

if(empty($_POST['Surname'])){echo "username is empty";}

if (!preg_match('|^[a z]+$|i', $_POST['Forename'])) $errors[] = "Forename can contain only letters!";

if (!preg_match('|^[0-9]+$|', $_POST['Office_location'])) $errors[] = "Office Location contain only digits!";

if (!preg_match('|^[0-9]+$|', $_POST['Telephone'])) $errors[] = "Telephone can contain only digits!";

if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $_POST['Email'])) $errors[] = "You must provide a valid email address!";

if (!preg_match('|^[0-9]+$|', $_POST['DOB'])) $errors[] = "DOB must  contain only digits!";

  if (count($errors) < 1) {

  } else {

$msg = "<p class=\"error\">The following errors have occurred: " . implode('<br />', $errors) . "</p>\n";
  }
}

echo isset($msg) ? $msg : '';

?>

<?php $Employee_ID = $_POST['Employee_ID'];
$Surname = $_POST['Surname'];
$Forename = $_POST['Forename'];
$Job_title = $_POST['Job_title'];
$Office_location = $_POST['Office_location'];
$Telephone = $_POST['Telephone'];
$Email = $_POST['Email'];
$Expertise = $_POST['Expertise'];
$Hobbies = $_POST['Hobbies'];
$DOB = $_POST['DOB'];


$target_path = "../page_g/etc/";
$target_path = $target_path . basename( $_FILES['Picture']['name']);

if(move_uploaded_file($_FILES['Picture']['tmp_name'],
$target_path)) {echo "";
} else{
    echo "There was an error uploading your picture file, please try again!";
}
   
?>

<table align="center" cellspacing="15">

  <tbody>

    <tr>

<td style="text-align: center";>
      <img src= "<?php echo $target_path; ?> "picture="" border="0" height="100" width="90"></a></td>
<td style="text-align: center;"></td>
    </tr>

  </tbody>
</table>

<br>
   
<table style="text-align: left; margin-left: auto; margin-right: auto; width: 490px;
height: 495px;" border="1" cellpadding="2" cellspacing="2">

  <tbody>

    <tr>
      <td width="25%" style=" text-align: left; vertical-align: middle; font-family: Tahoma; color: rgb(0, 0, 102);">Employee ID:</td>
      <td width="75%"><?php echo $Employee_ID ?></td>
    </tr>

    <tr>
      <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Surname:</td>
      <td width="75%"><?php echo $Surname ?></td>
    </tr>

 
    <tr>
      <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Forename:</td>
        <td width="75%"><?php echo $Forename ?></td>
    </tr>

    <tr>
      <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Job Title:</td>
    <td width="75%"><?php echo $Job_title ?></td>
    </tr>

    <tr>
    <td style="font-family: Tahoma; color: rgb(0, 0, 102);">DOB:</td>
<td width="75%":=""><?php echo $DOB ?></td>
    </tr>


    <tr>
      <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Telephone:</td>
      <td width="75%":=""><?php echo $Telephone ?></td>
    </tr>
   
    <tr>
      <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Email:</td>
      <td width="75%"><?php echo $Email ?></td>
    </tr>


  <tr>
      <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Office Location:</td>
      <td width="75%"><?php echo $Office_location ?></td>
    </tr>

       
<tr>
      <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Expertise:</td>
      <td width="75%"><?php echo $Expertise ?></td>
    </tr>

<tr>
      <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Hobbies:</td>
      <td width="75%"><?php echo $Hobbies ?></td>
</tr>

   
  </tbody>
</table>

<BR>

<hr style="width: 100%; height: 2px;">

<br>

<div style="text-align: left;"><a href="index.html"><img style="border: 0px solid ;
width: 94px; height: 46px;" alt="Home" src="etc/Home_button.JPG"></a><br>

<hr style="width: 100%; height: 2px;">
</div>

</div>

</div>

<div id="footer">
<p style="text-align: left;"></p>

</div>

</div>

</body>
</html>
[/code]
Link to comment
Share on other sites

Hi

Thanks that has worked great, the only thing is, is there any way that if an error is detected then you will be redirected to another page with the results displayed on that page, because at the moment the errors display at the top of the page and the table with blank fields appear below it?


Any suggestions

Regards

Jackie
Link to comment
Share on other sites

I'd suggest using the same page for the error display. Just create an "error" variable and set to true if you find an error during validation. Then create a switch to show the page normally if the error variable is false and to show the error page if the error variable is true. hope that makes sense.
Link to comment
Share on other sites

Ok, I took a closer look at your code, and you already have all the pieces to do this. you just need top put it all together.

You have an array called $errors. So after the validation lines add a switch to check the length of $errors. If it equals > 0, then display the page as you want it when there are errors. Else, display the page as you want for no errors.

[code]<?php
if (count($errors)>0) {
  //Display the erros page
} else {
  //display the no error page
}
?>[/code]
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.