Jump to content

PLEASE Help with Data Validation


ladieballer2004

Recommended Posts

OK so whoever helps me is gonna have to break it down to me. I'm completely new at this....

I have an web form that is gonna need a series of data validation. Example. Make sure required fields have been entered.  Number fields are in fact numbers. The email address is valid. Date is in YYYY-MM-DD...etc.  I want to keep the validation data on the same page as the web form. (unless it is easier to do otherwise) If the data is valid it passed to insert2.php to be given to the database.

 

Here is the form I have:

<body>
<?

if ($_POST)

{ foreach ($_POST as $k => $v)
{$v= trim($v);
$$k=$v;
}

//create empty error variable
$error = "";

//check for data in required fields
if (($FirstName=="")||($LastName=="")||($Email=="")||($Class=="")||($Residence=="")||($HighSchool=="")||
($Email=="")||($Hobbies=="")||($Legacy=="")||($ShirtSize==""))
{$error = "Please fill in all required fields.<br/>";}

//validate Legacy Decision
if(($Legacy=="Yes")	&& ($Sorority==""))
{$error = "<b>Your a Legacy! Which Sorority?</b><br/>";}

// validate $Age
if (is_numeric ($Age)== FALSE)
{$error = "Please enter a valid age (numbers only!)<br/>";}

// validate $ULVID
if (is_numeric ($ULVID)== FALSE)
{$error = "Please enter  valid ID (numbers only!)<br/>";}

// validate $Email
if((strpos ( $Email, "@")===FALSE)||
   (strpos ( $Email,".") ===FALSE)||
   (strpos ( $Email, " ")!=FALSE)||
   (strpos ( $Email, "@") > strrpos( $Email, ".")))
   {$error .="Please enter a valid email address <br/>";}
   
   //clean and validate $tel
   	if (is_numeric ($PhoneNumber)== FALSE)
{$error = "Please enter a valid telephone number<br/>";}

// clean $Answer1 and add <br/>
$Answer1= stripslashes ($Answer1);
$Answer1= strip_tags ($Answer1);
$Answer1=nl2br($Answer1);
// clean $Answer2 and add <br/>
$Answer2= stripslashes ($Answer2);
$Answer2= strip_tags ($Answer2);
$Answer2=nl2br($Answer2);
if ($error !="")
{echo "$error <P> Please hit the back button to try again.";}
?>

<form  method="POST" action="insert2.php">
  <table width="99%" border="20" cellpadding="20" cellspacing="10" bordercolor="#006633" bgcolor="#FFFFFF">
    <tr>
      <td width="31%"><label for="FirstName">First Name</label>
      <input type="text" name="FirstName" id="FirstName" /></td>
      <td width="40%"><label for="LastName">Last Name</label>
      <input type="text" name="LastName" id="LastName" /></td>
      <td width="29%" colspan="2" rowspan="2">                    
                  </td>
    </tr>
    <tr>
      <td><label for="Age">Age</label>
        <input type="text" name="Age" id="Age" /></td>
      <td><label for="Email">Email</label>
        <input type="text" name="Email" id="Email" /></td>
    </tr>
    <tr>
      <td><label for="PhoneNumber">Phone</label>
        (###-###-####)
          <input type="text" name="PhoneNumber" id="PhoneNumber" /></td>
      <td><label for="CumGpa">Cumulative GPA (high school or college):</label>
        <input type="text" name="CumGpa" id="CumGpa" /></td>
      <td width="29%" colspan="2"><label>Shirt Size
          <input type="text" name="ShirtSize" id="ShirtSize" />
      </label></td>
    </tr>
  </table>
  <table width="99%" border="20" cellpadding="20" cellspacing="10" bordercolor="#006633">
    <tr>
      <td width="28%">
        <label for="Class">Class
        <input type="text" name="Class" id="Class" />
        <br />
      (Freshman, Sophmore, Junior, Senior)</label></td>
      <td width="72%" rowspan="2"><label for="ULVGpa">ULV Spring Semester GPA (if applicable):</label>
      <input type="text" name="ULVGpa" id="ULVGpa" />
      <label></label></td>
    </tr>
    <tr>
      <td>
        <label>Residence
        <input type="text" name="Residence" id="Residence" />
        </label>
        <label for="Residence"><br />
        (On Campus, Off Campus)</label></td>
    </tr>
    <tr>
      <td rowspan="2">
        <label>Legacy
        <input type="text" name="Legacy" id="Legacy" />
        </label>
        <label for="Legacy"><br />
                    (Yes, No)</label></td>
      <td>(Legacy means that you have immediate family members who are part of a Greek-letter organization.) </td>
    </tr>
    <tr>
      <td><label>If so, Which ones?
          <input type="text" name="Sorority" id="Sorority" />
      </label>        <label for="Sorority"><br />
      (Alpha Omicron Pi, Phi Sigma Sigma, Sigma Kappa, Iota Delta)</label></td>
    </tr>
  </table>
  <table width="99%" border="20" cellpadding="20" cellspacing="10" bordercolor="#006633">
  <td colspan="4" bordercolor="#006633" border="20" cellpadding="20" cellspacing="10"> <h3>Other involvement during high school, college (clubs, sports, work, volunteer, etc.):</h3> </td>
  </tr>
  <tr>
    <td width="25%"><h3 align="center">Activity</h3></td>
    <td width="23%"><h3 align="center">Position</h3></td>
    <td width="25%"><h3 align="center">Start Date</h3></td>
    <td width="27%" height="60"><h3 align="center">End Date</h3></td>
  </tr>
  <tr>
    <td height="63"><div align="center">
      <input name="Activity[]" type="text" id="Activity[]" size="15" />    
    </div></td>
    <td height="63"><div align="center">
      <input name="Position[]" type="text" id="Position[]" size="15" />    
    </div></td>
    <td height="63"><div align="center">
      <input name="StartDate[]" type="text" id="StartDate[]" size="15" />    
    </div></td>
    <td height="63"><div align="center">
      <input name="EndDate[]" type="text" id="EndDate[]" size="15" />    
    </div></td>
  </tr>
  <tr>
    <td height="63"><div align="center">
      <input name="Activity[]" type="text" id="Activity[]" size="15" />    
    </div></td>
    <td height="63"><div align="center">
      <input name="Position[]" type="text" id="Position[]" size="15" />    
    </div></td>
    <td height="63"><div align="center">
      <input name="StartDate[]" type="text" id="StartDate[]" size="15" />    
    </div></td>
    <td height="63"><div align="center">
      <input name="EndDate[]" type="text" id="EndDate[]" size="15" />    
    </div></td>
  </tr>
  <tr>
    <td height="63"><div align="center">
      <input name="Activity[]" type="text" id="Activity[]" size="15" />    
    </div></td>
    <td height="63"><div align="center">
      <input name="Position[]" type="text" id="Position[]" size="15" />    
    </div></td>
    <td height="63"><div align="center">
      <input name="StartDate[]" type="text" id="StartDate[]" size="15" />    
    </div></td>
    <td height="63"><div align="center">
      <input name="EndDate[]" type="text" id="EndDate[]" size="15" />    
    </div></td>
  </tr>
  </table>
    

  
  <table width="99%" border="20" cellpadding="20" cellspacing="10" bordercolor="#006633">
    
    <tr>
      <td colspan="5"><label for="HighSchool">Previous High School (s) Attended:</label>
      <textarea name="HighSchool" cols="100" rows="0" id="HighSchool"></textarea></td>
    </tr>
    <tr>
      <td colspan="5"><label for="College">Other College (s) Attended (if applicable):</label>
      <textarea name="College" cols="95" id="College"></textarea></td>
    </tr>
    <tr>
      <td height="106" colspan="5"><label for="Hobbies">Hobbies/Interests:</label>
      <textarea name="Hobbies" cols="115" id="Hobbies"></textarea></td>
    </tr>
    
    <tr>
     
    </tr>
    <tr>
      <td colspan="5"><h3>What qualities, abilities or traits can you offer a sorority if selected as member?
        <textarea name="Answer1" cols="120" rows="4" id="Answer1"></textarea>
      </h3></td>
    </tr>
    <tr>
      <td height="63" colspan="5"><h3>What do you hope a sorority can give to you in return?
        <textarea name="Answer2" cols="120" rows="4" id="Answer2"></textarea>
        </h3>
      <label for="Answer1"></label></td>
    </tr>
    <tr>
      <td height="63" colspan="5"><h3>Permission Statement: By submitting my ID number, I grant permission for the ULV Greek Advisor to verify my </h3>
        <h3>eligibility for Greek membership at the University of La Verne.  I understand that my information will be used only</h3>
        <h3> to verify my eligibility. </h3>
      <p>
            <label for="ULVID">ULV ID #: </label>
            <input type="text" name="ULVID" id="ULVID" />
            <label for="Submit"> </label>
                
        <label for="Submit"></label>
        <input type="submit" name="Submit" id="Submit" value="Submit" />
      </p></td>
    </tr>
  </table>
  <p> </p>
</form>

</body>
</html>

Here is my insert2.php

<?php

$con = mysql_connect("localhost","user","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("CpaApp", $con);

$sql="INSERT INTO ApplicantInfo (ULVID, FirstName, LastName, Age, Email, PhoneNumber, Class,ShirtSize, Residence,CumGpa, ULVGpa,Legacy, Sorority,HighSchool, College, Hobbies, Answer1, Answer2)
VALUES ('$_POST[uLVID]', '$_POST[FirstName]','$_POST[LastName]','$_POST[Age];','$_POST[Email]',
'$_POST[PhoneNumber]','$_POST[Class]','$_POST[shirtSize]','$_POST[Residence]','$_POST[CumGpa]','$_POST[uLVGpa]','$_POST[Legacy]','$_POST[sorority]','$_POST[HighSchool]','$_POST[College]','$_POST[Hobbies]','$_POST[Answer1]','$_POST[Answer2]')";

if (!mysql_query($sql,$con))
  {
/* (Line 17)*/  echo"Error: Registration NOT Complete" ;
  }
/* (Line 19)*/   header("Location:regcomplete.php");

mysql_close($con)

?>

 

Without the validation code my form works properly. But I know that without it my database is at risk.

 

This is the error I'm currently receiving:

Error: Registration NOT Complete

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/TrialSite/ValidationSite/insert2.php:17) in /Applications/XAMPP/xamppfiles/htdocs/TrialSite/ValidationSite/insert2.php on line 19

 

Please Help! Thanks in Advance  :confused:

Link to comment
https://forums.phpfreaks.com/topic/171262-please-help-with-data-validation/
Share on other sites

It would be better if you asked which fields you need validation for and in which format.

 

For example

 

"Need to make sure date field is in this format: YYYY-MM-DD",

"Need to make sure phone number field only has numbers!".

 

You will probably have to use regular expressions. I am not good at this so i'll leave it to someone else.

 

However, few tips..

 

if you do require a field to be only number, use

is_numeric($_POST['PhoneNumber'])
// if whatever was entered in the PhoneNumber field was only numbers,
//this will return true. False otherwise

 

 

To check for email, use

filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL);
// returns true if email address entered is valid

 

For the YYYY-MM-DD format, you can use a drop down menu. Use strlen() and is_numeric() to, for example, verify that YYYY is 4 characters and all numbers.

 

 

 

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/TrialSite/ValidationSite/insert2.php:17) in /Applications/XAMPP/xamppfiles/htdocs/TrialSite/ValidationSite/insert2.php on line 19

 

At the very top of your script, before the <html.. and -everything-, put this:

<?php

ob_start();

?>

 

 

and at the very bottom, after everything else, put

<?php

ob_end_flush();

?>

Archived

This topic is now archived and is closed to further replies.

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