spanner206 Posted November 7, 2013 Share Posted November 7, 2013 hi all ive been trying to do this validation thing for ages now where i want to check if the field is empty and if so ask them to fill it and then once its filled go to the next page but ive been having problems with this i had loads of errors but i got it down to one which im confused to hell about and even once i get this error out the way i still don't no if it does validate please help. <!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") 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 border = "0"> <tr> <td>companyname:</td> <td><input type="text" name="companyname"> <span class="error">* <?php echo $companynameErr;?></span></td> </tr> <tr> <td>FirstName:</td> <td><input type="text" name="firstname"> <span class="error">* <?php echo $FirstNameErr;?></span></td> </tr> <tr> <td>Address 1:</td> <td><input type="text" name="address1"> <span class="error">* <?php echo $Address1Err;?></span></td> </tr> <tr> <td>Address 2:</td><td><input type="text" name="address2"> <span class="error">* <?php echo $Address2Err;?></span></td> </tr> <tr> <td>Area:</td><td><input type="text" name="area"> <span class="error">* <?php echo $AreaErr;?></span></td> </tr> <tr> <td>City:</td> <td><input type="text" name="city"> <span class="error">* <?php echo $CityErr;?></span></td> </tr> <tr> <td><input type="submit" name="submit" value="Submit"></td> </tr> </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 $companyname=>$companyname) { if($fieldValue == '') { print "<div>$companyname is blank</div>"; } } ?> </body> </html> this is the second page <?php $con=mysqli_connect("localhost","root","","nib"); 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[test]','$_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> and then the error code ive been getting ( ! ) Notice: Undefined index: companyname in C:\wamp\www\AddLeads\addleads2.php on line 30 Quote Link to comment https://forums.phpfreaks.com/topic/283683-error-help/ Share on other sites More sharing options...
Ch0cu3r Posted November 7, 2013 Share Posted November 7, 2013 Check that $_POST['companyname'] exists using isset before checking its value. To check a variable's value does not contain anything use empty if(isset($_POST['companyname']) && emtpy($_POST['companyname'])){ $allValid = false; } Quote Link to comment https://forums.phpfreaks.com/topic/283683-error-help/#findComment-1457355 Share on other sites More sharing options...
spanner206 Posted November 7, 2013 Author Share Posted November 7, 2013 cheers Ch0cu3r thats the error gone but its not validating it which is an absolute ball ake any ideas Quote Link to comment https://forums.phpfreaks.com/topic/283683-error-help/#findComment-1457356 Share on other sites More sharing options...
Ch0cu3r Posted November 7, 2013 Share Posted November 7, 2013 Your form is submitting to insertaddleads.php is this the second page? When the form is submitted the form data will sent there, not to the first page (form code). You need to do the form validation in the page where the form is being submitted to. Quote Link to comment https://forums.phpfreaks.com/topic/283683-error-help/#findComment-1457358 Share on other sites More sharing options...
spanner206 Posted November 7, 2013 Author Share Posted November 7, 2013 so would i move the validation to insertaddleads.php?? Quote Link to comment https://forums.phpfreaks.com/topic/283683-error-help/#findComment-1457360 Share on other sites More sharing options...
Ch0cu3r Posted November 7, 2013 Share Posted November 7, 2013 so would i move the validation to insertaddleads.php?? yes Quote Link to comment https://forums.phpfreaks.com/topic/283683-error-help/#findComment-1457362 Share on other sites More sharing options...
spanner206 Posted November 7, 2013 Author Share Posted November 7, 2013 right forget it how could i move it all onto one page cause when i do it errors a lot Quote Link to comment https://forums.phpfreaks.com/topic/283683-error-help/#findComment-1457371 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.