spanner206 Posted November 6, 2013 Share Posted November 6, 2013 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> Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/ Share on other sites More sharing options...
cyberRobot Posted November 6, 2013 Share Posted November 6, 2013 Variable names are case sensitive. Try using $companyname The same goes for the other variables. Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/#findComment-1457187 Share on other sites More sharing options...
spanner206 Posted November 6, 2013 Author Share Posted November 6, 2013 just changed them all to lower case and its still the same Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/#findComment-1457189 Share on other sites More sharing options...
spanner206 Posted November 6, 2013 Author Share Posted November 6, 2013 ive solved two of them on my own but now its just row 37 Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/#findComment-1457190 Share on other sites More sharing options...
cyberRobot Posted November 6, 2013 Share Posted November 6, 2013 The corresponding form filed is lower case: <input type="text" name="companyname"> The POST variable needs to match. Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/#findComment-1457193 Share on other sites More sharing options...
spanner206 Posted November 6, 2013 Author Share Posted November 6, 2013 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> Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/#findComment-1457204 Share on other sites More sharing options...
cyberRobot Posted November 6, 2013 Share Posted November 6, 2013 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/manual/en/function.isset.php Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/#findComment-1457206 Share on other sites More sharing options...
trq Posted November 6, 2013 Share Posted November 6, 2013 spanner206, can you please use the <code></code> tags like everybody else when posting code? Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/#findComment-1457207 Share on other sites More sharing options...
spanner206 Posted November 6, 2013 Author Share Posted November 6, 2013 sorry trq and yh it appears before i submit the form so ile have ago at using isset() Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/#findComment-1457209 Share on other sites More sharing options...
cyberRobot Posted November 6, 2013 Share Posted November 6, 2013 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. Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/#findComment-1457210 Share on other sites More sharing options...
spanner206 Posted November 6, 2013 Author Share Posted November 6, 2013 ok then Link to comment https://forums.phpfreaks.com/topic/283649-variables-errors/#findComment-1457212 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.