Jump to content
spanner206

Variables errors

Recommended Posts

ive got 3 errors which i have no clue what the problems are all 3 of them are related to near enough the same thing 1 of them is CompanyNameerr variable and the other to is CompanyName its confusing the hell out of me cause im pretty sure ive declared these variable.

( ! ) Notice: Undefined index: CompanyName in C:\wamp\www\AddLeads\addleads2.php on line 37

 

( ! ) Notice: Undefined variable: CompanyNameErr in C:\wamp\www\AddLeads\addleads2.php on line 45

 

 

( ! ) Notice: Undefined variable: CompanyName in C:\wamp\www\AddLeads\addleads2.php on line 68

 

 
and heres the code i used
 
<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 
<?
$con = mysqli_connect("localhost","root","","nib");
// 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;
{
$allValid = false;
}
if($allValid) {
// connect to db, create query, execute query
}
 
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>
 

Share this post


Link to post
Share on other sites

Variable names are case sensitive. Try using $companyname

 

The same goes for the other variables.

Edited by cyberRobot

Share this post


Link to post
Share on other sites

The corresponding form filed is lower case:

<input type="text" name="companyname">

The POST variable needs to match.

Share this post


Link to post
Share on other sites

yh i changed them but its still showing the error

 
<!DOCTYPE HTML> 
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body> 
<?
$con = mysqli_connect("localhost","root","","nib");
// 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;
{
$allValid = false;
}
if($allValid) {
// connect to db, create query, execute query
}
 
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"]);?>"> 
<table>
   <tr>
<td>CompanyName: <input type="text" name="companyname"></td> 
<td><span class="error">* <?php echo $companynameErr;?></span></td>
  </tr>
   <tr>
<td>FirstName: <input type="text" name="firstname"></td>
<td> <span class="error">* <?php echo $FirstNameErr;?></span></td>
   </tr>
   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>

Share this post


Link to post
Share on other sites

spanner206, can you please use the <code></code> tags like everybody else when posting code?

Share this post


Link to post
Share on other sites

spanner206, can you please use the <code></code> tags like everybody else when posting code?

 

Just to clarify, that should be


tags. Surrounding your code with those tags makes the post much easier to follow.  :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.