Jump to content

validation


spanner206

Recommended Posts

hi i want to put a validation in my code so it will check all fields if they have been used  ive got the code pretty much sorted but they i think they is one problem with it on line 30 

 

<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 
<?
$con = mysqli_connect("","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO tbl_club_contacts (CompanyName, FirstName, Address1, Address2, Area, City);
VALUES ('nelsons', 'luke', '', 'IT', '5 HIGHFIELD ROAD', 'LITTLEOVER', 'DERBY')");
 
 
mysqli_close($con);
 
 
// define variables and set to empty values
$companynameErr = $FirstNameErr = $Address1Err = $Address2Err = $AreaErr = $CityErr = "";
$companyname = $FirstName = $Address1 = $Address2 = $Area = $City = "";
 
if ($_SERVER["REQUEST_METHOD"] == "POST")
 
$allValid = true;
if(validation fails);
{
$allValid = false;
}
if($allValid) {
// connect to db, create query, execute query
}
As for checking the values you could
if($_POST['companyname']==null || $_POST['companyname']=="") {
$allValid = false;
}
?>
<form action="insertaddleads.php" method="post">
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   CompanyName: <input type="text" name="companyname">
   <span class="error">* <?php echo $CompanyNameErr;?></span>
   <br><br>
   FirstName: <input type="text" name="firstname">
   <span class="error">* <?php echo $FirstNameErr;?></span>
   <br><br>
   Address 1: <input type="text" name="address1">
   <span class="error">* <?php echo $Address1Err;?></span>
   <br><br>
   Address 2: <input type="text" name="address2">
   <span class="error">* <?php echo $Address2Err;?></span>
   <br><br>
   Area: <input type="text" name="area">
   <span class="error">* <?php echo $AreaErr;?></span>
   <br><br>
   City: <input type="text" name="city">
   <span class="error">* <?php echo $CityErr;?></span>
   <br><br>
   
   <input type="submit" name="submit" value="Submit"> 
 
</form>
 
<?
echo $CompanyName;
echo "<br>";
echo $FirstName;
echo "<br>";
echo $Address1;
echo "<br>";
echo $Address2;
echo "<br>";
echo $Area;
echo "<br>";
echo $City;
echo "<br>";
?>
<?php
foreach($_POST as $fieldName=>$fieldValue) {
     if($fieldValue == '') {
          print "<div>$fieldName is blank</div>";
     }
}
?>
</body>
</html>
 
heres the insert just incase
 
<?php
$con=mysqli_connect("","","","");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
 
$sql="INSERT INTO tbl_club_contacts (CompanyName, FirstName, Address1, Address2, Area, City)
VALUES
('$_POST[companyname]','$_POST[firstname]','$_POST[address1]','$_POST[address2]','$_POST[area]','$_POST[city]')";
 
if (!mysqli_query($con,$sql))
  {
   die('Error: ' . mysqli_error($con));
  }
echo "record added";
 
mysqli_close($con);
?>
<html>
<body>
 
<form action="addleads2.php" method="post">
<input type="submit", value = "go back">
</form>
 
</body>
</html>
 
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.