Jump to content


Photo

Variables errors

variables errors php

  • Please log in to reply
10 replies to this topic

#1 spanner206

spanner206
  • Members
  • PipPipPip
  • Advanced Member
  • 83 posts

Posted 06 November 2013 - 11:49 AM

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>
 


#2 cyberRobot

cyberRobot
  • Moderators
  • Much to Learn
  • 2,790 posts

Posted 06 November 2013 - 01:26 PM

Variable names are case sensitive. Try using $companyname

 

The same goes for the other variables.


Edited by cyberRobot, 06 November 2013 - 01:28 PM.


#3 spanner206

spanner206
  • Members
  • PipPipPip
  • Advanced Member
  • 83 posts

Posted 06 November 2013 - 01:32 PM

just changed them all to lower case and its still the same



#4 spanner206

spanner206
  • Members
  • PipPipPip
  • Advanced Member
  • 83 posts

Posted 06 November 2013 - 01:35 PM

ive solved two of them on my own but now its just row 37 



#5 cyberRobot

cyberRobot
  • Moderators
  • Much to Learn
  • 2,790 posts

Posted 06 November 2013 - 01:38 PM

The corresponding form filed is lower case:

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

The POST variable needs to match.



#6 spanner206

spanner206
  • Members
  • PipPipPip
  • Advanced Member
  • 83 posts

Posted 06 November 2013 - 02:23 PM

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>


#7 cyberRobot

cyberRobot
  • Moderators
  • Much to Learn
  • 2,790 posts

Posted 06 November 2013 - 02:30 PM

Is the error being display before or after the form is submitted? If it's before, $_POST isn't defined yet. To avoid error regarding an undefined variable, you could use isset().

http://php.net/manua...ction.isset.php



#8 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 06 November 2013 - 02:32 PM

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

#9 spanner206

spanner206
  • Members
  • PipPipPip
  • Advanced Member
  • 83 posts

Posted 06 November 2013 - 02:40 PM

sorry trq and yh it appears before i submit the form so ile have ago at using isset()



#10 cyberRobot

cyberRobot
  • Moderators
  • Much to Learn
  • 2,790 posts

Posted 06 November 2013 - 02:55 PM

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

 

Just to clarify, that should be [code][/code] tags. Surrounding your code with those tags makes the post much easier to follow.  :)



#11 spanner206

spanner206
  • Members
  • PipPipPip
  • Advanced Member
  • 83 posts

Posted 06 November 2013 - 03:01 PM

ok then :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users