Jump to content

[SOLVED] Error searching help


Recommended Posts

Hey I've had this form that used to work until very recently, I'm pretty sure I've not made any changes to the problem area since it used to work but for some reason it's causing problems. The problem is the PHP form validation from lines 26 to 165 of the code below:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<?php
session_start();

include "connect_details.php";

$Submit = $_POST['SubmitB'];

if($_POST['SubmitB'] == "Submit")
{
   $Campus = addslashes($_POST['Campus']);
   $CampusEmail = $_POST['CampusEmail'];
   $ConfirmEmail = $_POST['ConfirmEmail'];
   $TelNo =  $_POST['TelNo'];
   $Add1 = addslashes($_POST['Add1']);
   $Add2 = addslashes($_POST['Add2']);
   $Add3 = addslashes($_POST['Add3']);
   $Town = addslashes($_POST['Town']);
   $County = addslashes($_POST['County']);
   $PostCode = addslashes($_POST['PostCode']); 
   
   $Errors = "0";

// This section is causing errors when it used to work and works fine without it, but I need to keep the PHP validation.

   if(empty($Campus))
   {
      $Errors++;
      $Err1 = "<span class='errmsg'>! </span>";
      $MsgCampus = "<span class='errmsg'>Please Enter the Campus Name</span><br>";
   }
   else
   {
      $MsgCampus="";
      $Err1 = "";
   }

   if($CampusEmail !== $ConfirmEmail)
   {
      $Errors++;
      $Err2 = "<span class='errmsg'>! </span>";
      $MsgCampusEmail = "<span class='errmsg'>The Email Addresses do not match please re-confirm them</span><br>";         
   }
   else
   {
      if(empty($CampusEmail))
      {
         $Errors++;
         $Err2 = "<span class='errmsg'>! </span>";
         $MsgCampusEmail = "<span class='errmsg'>Please Enter a Campus Email Address</span><br>"; 
      }
      elseif(!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $CampusEmail)) 
      {   
         $Errors++;
         $Err2 = "<span class='errmsg'>! </span>";
         $MsgCampusEmail = "<span class='errmsg'>Please Enter a valid Campus Email Address</span><br>";          
      }
      else
      {
         $MsgCampusEmail="";
         $Err2 = "";
      }

      if(empty($ConfirmEmail))
      {
         $Errors++;
         $Err3 = "<span class='errmsg'>! </span>";
         $MsgConfirmEmail = "<span class='errmsg'>Please Enter a Confirmation Email Address</span><br>"; 
      }
      elseif(!preg_match('/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $ConfirmEmail)) 
      {   
         $Errors++;
         $Err3 = "<span class='errmsg'>! </span>";
         $MsgConfirmEmail = "<span class='errmsg'>Please Enter a valid Confirmation Email Address</span><br>";          
      }
      else
      {
         $MsgConfirmEmail="";
         $Err3 = "";
      }
   }

   if(empty($TelNo))
   {
      $Errors++;
      $Err4 = "<span class='errmsg'>! </span>";
      $MsgTelNo = "<span class='errmsg'>Please Enter the Telephone Number!</span><br>";   
   }
   elseif(is_numeric($TelNo) == false)
   {
      $Errors++;
      $Err4 = "<span class='errmsg'>! </span>";
      $MsgTelNo = "<span class='errmsg'>That is not a valid Telephone Number!</span><br>";        
   }
   else
   {
      $MsgTelNo="";
      $Err4 = "";
   }

   if(empty($Add1))
   {
      $Errors++;
      $Err5 = "<span class='errmsg'>! </span>";
      $MsgAdd1 = "<span class='errmsg'>Please Enter the Address Line 1</span><br>";
   }
   else
   {
      $MsgAdd1="";
      $Err5 = "";
   }

   if(empty($Add2))
   {
      $Errors++;
      $Err6 = "<span class='errmsg'>! </span>";
      $MsgAdd2 = "<span class='errmsg'>Please Enter the Address Line 2</span><br>";
   }
   else
   {
      $MsgAdd2="";
      $Err6 = "";
   }

   if(empty($Town))
   {
      $Errors++;
      $Err7 = "<span class='errmsg'>! </span>";
      $MsgTown = "<span class='errmsg'>Please Enter the Town</span><br>";
   }
   else
   {
      $MsgTown="";
      $Err7 = "";
   }

   if(empty($County))
   {
      $Errors++;
      $Err8 = "<span class='errmsg'>! </span>";
      $MsgCounty = "<span class='errmsg'>Please Enter the County</span><br>";
   }
   else
   {
      $MsgCounty="";
      $Err8 = "";
   }

   if(empty($PostCode))
   {
      $Errors++;
      $Err9 = "<span class=\"errmsg\">! </span>";
      $MsgPostCode = "<span class=\"errmsg\">Please Enter the Post Code</span><br>";
   }
   elseif((length($PostCode) !== 7) || (length($PostCode) !== 6))
   {
      $Errors++;
      $Err9 = "<span class='errmsg'>! </span>";
      $MsgPostCode = "<span class='errmsg'>Please enter a valid post code </span><br>";
   }
   else
   {
      $MsgPostCode="";
      $Err9 = "";
   }

// End of section

   if($Errors==0)
   { 
      $conn = mysql_connect($Host,$Username,$Password) or die(mysql_error());
      $db = mysql_select_db($Dbname, $conn);
      $rs1 = mysql_query("SELECT * FROM Campus",$conn) or die(mysql_error());
      $NumberOfRecords = mysql_num_rows($rs1);
      $CampusID = "Campus".($NumberOfRecords+1);

      $X = 1;

      while($OkCampusID == false)
      {
         $rs2 = mysql_query("SELECT CampusID FROM Campus WHERE CampusID='$CampusID'",$conn) or die(mysql_error());
      
         if(mysql_num_rows($rs2)==1)
         {
            $OkCampusID = false;
            $CampusID = "Campus".$X;
         }
         else
         {
            $OkCampusID = true;
         }
         $X++;
      }
  
      $rsNew = mysql_query("INSERT INTO Campus (CampusID, Campus, CampusEmail, TelNo, Add1, Add2, Add3, Town, County, PostCode) VALUES('$CampusID','$Campus','$CampusEmail','$TelNo','$Add1','$Add2','$Add3','$Town','$County','$PostCode')",$conn) or die(mysql_error());		

      if(mysql_affected_rows($conn) == 1)
      {
         $_SESSION['NewAction'] = "Added";
      }
      elseif(mysql_affected_rows($conn) == 0)
      {
         $_SESSION['NewAction'] = "NotAdded";      
      }
      mysql_close($conn);
  
      header("Location:display_campus_details.php?Action=Edit");
   }
}

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="keywords" content="Abingdon College, Witney College, clothing, Clothing Line, hats, bags, t-shirts, shirts, vests, jackets">
<meta name="description" content="Clothing Line">
<meta name="author" content="Adam Compton">
<title>Clothing Line - New Campus Form</title>
<link href="stylesheetCL.css" rel="stylesheet">
<?php require('jscript.inc');

if(($_SESSION['User_Type'] == "Student") || ($_SESSION['User_Type'] == ""))
{
   header("Location:../index.php");
}
?>

<script language='JavaScript' type='text/JavaScript'>
<!--
function IsNumeric(strString)
{
   var strValidChars = "0123456789-";
   var strChar;
   var Result = true;

   if (strString.length == 0) return false;

   for (i = 0; i < strString.length && Result == true; i++)
   {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
         Result = false;
      }
   }
   return Result;
}

function Validate(f)
{
   return (ValidateValues(f)==0 ? true : false );
}

function ValidateValues(f)
{
   var Errors = 0
   if((document.AddCampus.Campus.value+'').length<1)
   {
      Errors++
      document.getElementById('mySpan1a').style.display='block';
      document.getElementById('mySpan1a').innerHTML='Please Enter a Campus Name!';
      document.getElementById('mySpan1b').style.display='inline';
      document.getElementById('mySpan1b').innerHTML='! ';
   }
   else
   {
      document.getElementById('mySpan1a').style.display='none';
      document.getElementById('mySpan1a').innerHTML='';
      document.getElementById('mySpan1b').style.display='none';
      document.getElementById('mySpan1b').innerHTML='';
   }

   if(document.AddCampus.CampusEmail.value !== document.AddCampus.ConfirmEmail.value)
   {
      Errors++
      document.getElementById('mySpan2a').style.display='block';
      document.getElementById('mySpan2a').innerHTML='The Email Addresses do not match, please re-confirm them!';
      document.getElementById('mySpan2b').style.display='inline';
      document.getElementById('mySpan2b').innerHTML='! ';
      document.getElementById('mySpan2d').style.display='inline';
      document.getElementById('mySpan2d').innerHTML='! ';
   }
   else
   {
      if(document.AddCampus.CampusEmail.value =="")
      {
         Errors++
         document.getElementById('mySpan2a').style.display='block';
         document.getElementById('mySpan2a').innerHTML='Please Enter an Email Address!';
         document.getElementById('mySpan2b').style.display='inline';
         document.getElementById('mySpan2b').innerHTML='! ';
      }
      else if(!document.AddCampus.CampusEmail.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/))
      {
         Errors++
         document.getElementById('mySpan2a').style.display='block';
         document.getElementById('mySpan2a').innerHTML='Please Enter a Valid Email Address!';
         document.getElementById('mySpan2b').style.display='inline';
         document.getElementById('mySpan2b').innerHTML='! ';
      }
      else
      {
         document.getElementById('mySpan2a').style.display='none';
         document.getElementById('mySpan2a').innerHTML='';
         document.getElementById('mySpan2b').style.display='none';
         document.getElementById('mySpan2b').innerHTML='';
      }

      if(document.AddCampus.ConfirmEmail.value =="")
      {
         Errors++
         document.getElementById('mySpan2c').style.display='block';
         document.getElementById('mySpan2c').innerHTML='Please Enter an Confirmation Email Address!';
         document.getElementById('mySpan2d').style.display='inline';
         document.getElementById('mySpan2d').innerHTML='! ';
      }
      else if(!document.AddCampus.ConfirmEmail.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/))
      {
         Errors++
         document.getElementById('mySpan2c').style.display='block';
         document.getElementById('mySpan2c').innerHTML='Please Enter a Valid Confirmation Email Address!';
         document.getElementById('mySpan2d').style.display='inline';
         document.getElementById('mySpan2d').innerHTML='! ';
      }
      else
      {
         document.getElementById('mySpan2c').style.display='none';
         document.getElementById('mySpan2c').innerHTML='';
         document.getElementById('mySpan2d').style.display='none';
         document.getElementById('mySpan2d').innerHTML='';
      }
   }

   if((document.AddCampus.TelNo.value+'').length<0)
   {
      Errors++;
      document.getElementById('mySpan3a').style.display='block';
      document.getElementById('mySpan3a').innerHTML='Please Enter the Telephone Number!';
      document.getElementById('mySpan3b').style.display='inline';
      document.getElementById('mySpan3b').innerHTML='! ';

   }
   else if((document.AddCampus.TelNo.value+'').length<11)
   {
      Errors++;
      document.getElementById('mySpan3a').style.display='block';
      document.getElementById('mySpan3a').innerHTML='Please Enter a Valid Telephone Number!';
      document.getElementById('mySpan3b').style.display='inline';
      document.getElementById('mySpan3b').innerHTML='! ';
   }
   else if(IsNumeric(document.AddCampus.TelNo.value) == false)
   {
      Errors++;
      document.getElementById('mySpan3a').style.display='block';
      document.getElementById('mySpan3a').innerHTML='Please Enter a valid Telephone Number!';
      document.getElementById('mySpan3b').style.display='inline';
      document.getElementById('mySpan3b').innerHTML='! ';
   }
   else
   {
      document.getElementById('mySpan3a').style.display='none';
      document.getElementById('mySpan3a').innerHTML='';
      document.getElementById('mySpan3b').style.display='none';
      document.getElementById('mySpan3b').innerHTML='';
   }

   if((document.AddCampus.Add1.value+'').length<1)
   {
      Errors++;
      document.getElementById('mySpan4a').style.display='none';
      document.getElementById('mySpan4a').innerHTML='Please Enter Address Line 1!';
      document.getElementById('mySpan4b').style.display='inline';
      document.getElementById('mySpan4b').innerHTML='! ';
   }
   else
   {
      document.getElementById('mySpan4a').style.display='none';
      document.getElementById('mySpan4a').innerHTML='';
      document.getElementById('mySpan4b').style.display='none';
      document.getElementById('mySpan4b').innerHTML='';
   }

   if((document.AddCampus.Add2.value+'').length<1)
   {
      Errors++;
      document.getElementById('mySpan5a').style.display='none';
      document.getElementById('mySpan5a').innerHTML='Please Enter Address Line 2!';
      document.getElementById('mySpan5b').style.display='inline';
      document.getElementById('mySpan5b').innerHTML='! ';
   }
   else
   {
      document.getElementById('mySpan5a').style.display='none';
      document.getElementById('mySpan5a').innerHTML='';
      document.getElementById('mySpan5b').style.display='none';
      document.getElementById('mySpan5b').innerHTML='';
   }

   if((document.AddCampus.Town.value+'').length<1)
   {
      Errors++
      document.getElementById('mySpan6a').style.display='block';
      document.getElementById('mySpan6a').innerHTML='Please Enter the Town!';
      document.getElementById('mySpan6b').style.display='inline';
      document.getElementById('mySpan6b').innerHTML='! ';
   }
   else
   {
      document.getElementById('mySpan6a').style.display='none';
      document.getElementById('mySpan6a').innerHTML='';
      document.getElementById('mySpan6b').style.display='none';
      document.getElementById('mySpan6b').innerHTML='';
   }

   if((document.AddCampus.County.value+'').length<1)
   {
      Errors++;
      document.getElementById('mySpan7a').style.display='block';
      document.getElementById('mySpan7a').innerHTML='Please Enter the County!';
      document.getElementById('mySpan7b').style.display='inline';
      document.getElementById('mySpan7b').innerHTML='! ';
   }
   else
   {
      document.getElementById('mySpan7a').style.display='none';
      document.getElementById('mySpan7a').innerHTML='';
      document.getElementById('mySpan7b').style.display='none';
      document.getElementById('mySpan7b').innerHTML='';
   }

   if((document.AddCampus.PostCode.value+'').length<1)
   {
      Errors++;
      document.getElementById('mySpan8a').style.display='block';
      document.getElementById('mySpan8a').innerHTML='Please Enter the Post Code!';
      document.getElementById('mySpan8b').style.display='inline';
      document.getElementById('mySpan8b').innerHTML='! ';
   }
   else
   {
      document.getElementById('mySpan8a').style.display='none';
      document.getElementById('mySpan8a').innerHTML='';
      document.getElementById('mySpan8b').style.display='none';
      document.getElementById('mySpan8b').innerHTML='';
   }

   return Errors;
}


//-->
</script> 
</head>
<body>
<div class="MainContainer">
<?php require('header.inc');
require('menu.inc') ?>

<div class="DivMain">

<span class="head1">New Campus Form</span><br>
<span class="errmsg">All fields marked with an * are required</span><br><br>

<?php 
echo $MsgCampus;
echo $MsgCampusEmail;
echo $MsgConfirmEmail;
echo $MsgTelNo;
echo $MsgAdd1;
echo $MsgAdd2;
echo $MsgTown;
echo $MsgCounty;
echo $MsgPostCode;
?>

<span id="mySpan1a" class="mySpan1a"><br></span>
<span id="mySpan2a" class="mySpan2a"><br></span>
<span id="mySpan2c" class="mySpan2c"><br></span>
<span id="mySpan3a" class="mySpan3a"><br></span>
<span id="mySpan4a" class="mySpan4a"><br></span>
<span id="mySpan5a" class="mySpan5a"><br></span>
<span id="mySpan6a" class="mySpan6a"><br></span>
<span id="mySpan7a" class="mySpan7a"><br></span>
<span id="mySpan8a" class="mySpan8a"><br></span>

<br>

<form method="POST" action="add_campus_details_form.php" name="AddCampus" onSubmit="return Validate(this);">

<table style="width: auto; position: absolute;">

<tr><td><?php echo $Err1?><span id="mySpan1b" class="mySpan1b"></span><span class="head4">Campus Name:</span><span class="errmsg"> *</span></td>
<td width="300"><label><input type="text" size="33" name="Campus" maxlength="30" value="<?php echo $Campus?>"/></label>
</td></tr>

<tr><td><?php echo $Err2?><span id="mySpan2b" class="mySpan2b"></span><span class="head4">Campus Email:</span><span class="errmsg"> *</span></td>
<td width="300"><label><input type="text" size="55" maxlength="50" name="CampusEmail" value="<?php echo $CampusEmail?>"/></label>
</td></tr>

<tr><td><?php echo $Err3?><span id="mySpan2d" class="mySpan2d"></span><span class="head4">Confirm Email:</span><span class="errmsg"> *</span></td>
<td width="300"><label><input type="text" size="55" maxlength="50" name="ConfirmEmail" value="<?php echo $ConfirmEmail?>"/>
</label></td></tr><tr></tr>

<tr><td><?php echo $Err4?><span id="mySpan3b" class="mySpan3b"></span><span class="head4">Telephone No:</span><span class="errmsg"> *</span></td>
<td width="300"><label><input type="text" size="15" maxlength="15" name="TelNo" value="<?php echo $TelNo?>"/></label><span class="normal"> (No spaces)</span>
</td></tr>

<tr><td><?php echo $Err5?><span id="mySpan4b" class="mySpan4b"></span><span class="head4">Address 1:</span><span class="errmsg"> *</span></td>
<td width="300"><label><input type="text" size="33" name="Add1" maxlength="30" value="<?php echo $Add1?>"/></label>
</td></tr>

<tr><td><?php echo $Err6?><span id="mySpan5b" class="mySpan5b"></span><span class="head4">Address 2:</span><span class="errmsg"> *</span></td>
<td width="300"><label><input type="text" size="33" name="Add2" maxlength="30" value="<?php echo $Add2?>"/></label>
</td></tr>

<tr><td><span class="head4">Address 3:</span><br></td>
<td width="300"><label><input type="text" size="33" name="Add3" maxlength="30" value="<?php echo $Add3?>"/></label>
</td></tr>

<tr><td><?php echo $Err7?><span id="mySpan6b" class="mySpan6b"></span><span class="head4">Town:</span><span class="errmsg"> *</span></td>
<td width="300"><label><input type="text" size="33" name="Town" maxlength="30" value="<?php echo $Town?>"/></label>
</td></tr>

<tr><td><?php echo $Err8?><span id="mySpan7b" class="mySpan7b"></span><span class="head4">County:</span><span class="errmsg"> *</span></td>
<td width="300"><label><input type="text" size="20" name="County" maxlength="20" value="<?php echo $County?>"/></label>
</td></tr>

<tr><td><?php echo $Err9?><span id="mySpan8b" class="mySpan8b"></span><span class="head4">Post Code:</span><span class="errmsg"> *</span></td>
<td width="300"><label><input type="text" size="7" maxlength="7" name="PostCode" value="<?php echo $PostCode?>"/></label><span class="normal"> (No spaces)</span>
</td></tr>

<tr><td>  
<input type="submit" name="SubmitB" class="buttonS" style="overflow:visible; width:auto" value="Submit" onMouseOver="OverMouse(this)"; onMouseOut="OutMouse(this)"/>
<input type="reset" name="ResetB" class="buttonS" style="overflow:visible; width:auto" value="Reset" onMouseOver="OverMouse(this)"; onMouseOut="OutMouse(this)"/>
</td></tr>
<tr></tr>
<tr><td><a href="display_campus_details.php?Action=Edit">Back to Edit Campus Details List</a></td></tr>
</table>   
</form>  

</div>
</div>
</body>
</html>

 

If anyone can tell me why it's happening I'd be greatly appreciated.

Link to comment
Share on other sites

Okay the page loads fine with the form and everything, then when I press submit and I get past the JavaScript validation check the page reloads and it's just a blank white screen with no error messages, I spent a little while to determine it was the PHP validation section causing the problem but I can't see anything out of place, even compared to my other forms. All my form values are fine and work as they should be.

 

P.S. I have already used error_report(E_ALL); but it didn't turn up anything wrong.

Link to comment
Share on other sites

Turn on display_errors as well -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

 

You should be getting header related errors because in the posted code, sessions and header redirects will only work if output buffering is on in php.ini or a .htaccess file because you are outputting content to the browser before sending headers.

Link to comment
Share on other sites

Ah thanks I'm now getting:

Fatal error: Call to undefined function: length() in /usr/home/awccloth/public_html/admin/add_campus_details_form.php on line 158

 

which is part of this validation:

   if(empty($PostCode))
   {
      $Errors++;
      $Err9 = "<span class=\"errmsg\">! </span>";
      $MsgPostCode = "<span class=\"errmsg\">Please Enter the Post Code</span><br>";
   }
   elseif((length($PostCode) !== 7) || (length($PostCode) !== 6))
   {
      $Errors++;
      $Err9 = "<span class='errmsg'>! </span>";
      $MsgPostCode = "<span class='errmsg'>Please enter a valid post code </span><br>";
   }
   else
   {
      $MsgPostCode="";
      $Err9 = "";
   }

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.