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">
<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 😕