jdm95lude Posted March 12, 2008 Share Posted March 12, 2008 All i'm trying to do is when you click the submit button it checks to see if the text boxs are empty. But its not working. Driving me crazy I know it has to be a simple fix but I can't get it. <?php if (empty($_GET['FName'])) { echo "<p class='special'>You must complete the form in full.</p>"; } ?> <form action="AddForm.php" method="get" enctype="application/x-www-form-urlencoded"> <div class="content">First Name: <input type="text" maxlength="20" name="Fname" id="Fname" size="15" value="<?php if (empty($_GET['Fname'])) echo $_GET['Fname'] ?>" /></div> <div class="content">Last Name: <input type="text" maxlength="20" name="LName" id="LName" size="15" value="<?php if (empty($_GET['LName'])) echo $_GET['LName'] ?>" /></div> <div class="content">Address: <input type="text" maxlength="20" name="Address" id="Address" size="15" value="<?php if (empty($_GET['Address'])) echo $_GET['Address'] ?>" /></div> <div class="content">City: <input type="text" maxlength="20" name="City" id="City" size="15" value="<?php if (empty($_GET['City'])) echo $_GET['City'] ?>" /></div> <div class="content">State: <input type="text" maxlength="20" name="State" id="State" size="15" value="<?php if (empty($_GET['State'])) echo $_GET['State'] ?>" /></div> <div class="content">Zip: <input type="text" maxlength="20" name="Zip" id="Zip" size="15" value="<?php if (empty($_GET['Zip'])) echo $_GET['Zip'] ?>" /></div> <div class="content">Phone: <input type="text" maxlength="20" name="Phone" id="Phone" size="15" value="<?php if (empty($_GET['Phone'])) echo $_GET['Phone'] ?>" /></div> <div class="submit"><input type="submit" value="Update Entry" /></div> </form> Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/ Share on other sites More sharing options...
ikmyer Posted March 12, 2008 Share Posted March 12, 2008 $_GET['FName'] should be $_POST['Fname'] Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490728 Share on other sites More sharing options...
jdm95lude Posted March 12, 2008 Author Share Posted March 12, 2008 On which part in the if statement in the form or the if statement outside of the form? I want it to only display the message when the button is clicked and right now it displays it right when the page is loaded. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490732 Share on other sites More sharing options...
revraz Posted March 12, 2008 Share Posted March 12, 2008 Not sure why he said that since you are using GET as your method, but what you are missing is a check to see if SUBMIT is set. Do that before your IF EMPTY statement. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490734 Share on other sites More sharing options...
ikmyer Posted March 12, 2008 Share Posted March 12, 2008 Sorry, didn't check your form.. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490735 Share on other sites More sharing options...
jdm95lude Posted March 12, 2008 Author Share Posted March 12, 2008 Not sure why he said that since you are using GET as your method, but what you are missing is a check to see if SUBMIT is set. Do that before your IF EMPTY statement. How would I go about doing that? Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490739 Share on other sites More sharing options...
johnska7 Posted March 12, 2008 Share Posted March 12, 2008 You should wrap the top part into a check to see if the form has been submitted in general, ie. <? if($_POST['Submit'] == "Update Entry") { if (empty($_GET['FName'])) { echo "<p class='special'>You must complete the form in full.</p>"; } } else { ?> <form action="AddForm.php" method="get" enctype="application/x-www-form-urlencoded"> <div class="content">First Name: <input type="text" maxlength="20" name="Fname" id="Fname" size="15" value="<?php if (empty($_GET['Fname'])) echo $_GET['Fname'] ?>" /></div> <div class="content">Last Name: <input type="text" maxlength="20" name="LName" id="LName" size="15" value="<?php if (empty($_GET['LName'])) echo $_GET['LName'] ?>" /></div> <div class="content">Address: <input type="text" maxlength="20" name="Address" id="Address" size="15" value="<?php if (empty($_GET['Address'])) echo $_GET['Address'] ?>" /></div> <div class="content">City: <input type="text" maxlength="20" name="City" id="City" size="15" value="<?php if (empty($_GET['City'])) echo $_GET['City'] ?>" /></div> <div class="content">State: <input type="text" maxlength="20" name="State" id="State" size="15" value="<?php if (empty($_GET['State'])) echo $_GET['State'] ?>" /></div> <div class="content">Zip: <input type="text" maxlength="20" name="Zip" id="Zip" size="15" value="<?php if (empty($_GET['Zip'])) echo $_GET['Zip'] ?>" /></div> <div class="content">Phone: <input type="text" maxlength="20" name="Phone" id="Phone" size="15" value="<?php if (empty($_GET['Phone'])) echo $_GET['Phone'] ?>" /></div> <div class="submit"><input type="submit" name="Submit" value="Update Entry" /></div> </form> <? } ?> Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490741 Share on other sites More sharing options...
revraz Posted March 12, 2008 Share Posted March 12, 2008 Replace POST with GET. I would recommend though you change your METHOD to POST and change all your GETs to POST. Unless you want all the data being passed via a URL. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490744 Share on other sites More sharing options...
jdm95lude Posted March 12, 2008 Author Share Posted March 12, 2008 Ok Got it its working but still not full. Now it displays even if there is something in the text fields? <?php if (isset($_POST['Submit'])== "Update Entry") if (empty($_POST['FName']) || empty($_POST['LName']) || empty($_POST['Address']) || empty($_POST['City']) || empty($_POST['State']) || empty($_POST['Zip']) || empty($_POST['Phone'])) { echo "<p class='special'>You must complete the form in full.</p>"; } ?> <form action="AddForm.php" method="post" enctype="application/x-www-form-urlencoded"> <div class="content">First Name: <input type="text" maxlength="20" name="Fname" id="Fname" size="15" value="<?php if (empty($_POST['Fname'])) echo $_POST['Fname'] ?>" /></div> <div class="content">Last Name: <input type="text" maxlength="20" name="LName" id="LName" size="15" value="<?php if (empty($_POST['LName'])) echo $_POST['LName'] ?>" /></div> <div class="content">Address: <input type="text" maxlength="20" name="Address" id="Address" size="15" value="<?php if (empty($_POST['Address'])) echo $_POST['Address'] ?>" /></div> <div class="content">City: <input type="text" maxlength="20" name="City" id="City" size="15" value="<?php if (empty($_POST['City'])) echo $_POST['City'] ?>" /></div> <div class="content">State: <input type="text" maxlength="20" name="State" id="State" size="15" value="<?php if (empty($_POST['State'])) echo $_POST['State'] ?>" /></div> <div class="content">Zip: <input type="text" maxlength="20" name="Zip" id="Zip" size="15" value="<?php if (empty($_POST['Zip'])) echo $_POST['Zip'] ?>" /></div> <div class="content">Phone: <input type="text" maxlength="20" name="Phone" id="Phone" size="15" value="<?php if (empty($_POST['Phone'])) echo $_POST['Phone'] ?>" /></div> <div class="submit"><input type="submit" name="Submit" id="Submit" namevalue="Update Entry" /></div> </form> Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490746 Share on other sites More sharing options...
revraz Posted March 12, 2008 Share Posted March 12, 2008 Either change name="Fname" to name="FName" or $_POST['FName'] to $_POST['Fname'] Variables are case sensitive. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490757 Share on other sites More sharing options...
johnska7 Posted March 12, 2008 Share Posted March 12, 2008 Gotta have it all wrapped around and If then else, like so <?php if (isset($_POST['Submit'])== "Update Entry") { if (empty($_POST['FName']) || empty($_POST['LName']) || empty($_POST['Address']) || empty($_POST['City']) || empty($_POST['State']) || empty($_POST['Zip']) || empty($_POST['Phone'])) { echo "<p class='special'>You must complete the form in full.</p>"; } } else { ?> <form action="" method="post" enctype="application/x-www-form-urlencoded"> <div class="content">First Name: <input type="text" maxlength="20" name="Fname" id="Fname" size="15" value="<?php if (empty($_POST['Fname'])) echo $_POST['Fname'] ?>" /></div> <div class="content">Last Name: <input type="text" maxlength="20" name="LName" id="LName" size="15" value="<?php if (empty($_POST['LName'])) echo $_POST['LName'] ?>" /></div> <div class="content">Address: <input type="text" maxlength="20" name="Address" id="Address" size="15" value="<?php if (empty($_POST['Address'])) echo $_POST['Address'] ?>" /></div> <div class="content">City: <input type="text" maxlength="20" name="City" id="City" size="15" value="<?php if (empty($_POST['City'])) echo $_POST['City'] ?>" /></div> <div class="content">State: <input type="text" maxlength="20" name="State" id="State" size="15" value="<?php if (empty($_POST['State'])) echo $_POST['State'] ?>" /></div> <div class="content">Zip: <input type="text" maxlength="20" name="Zip" id="Zip" size="15" value="<?php if (empty($_POST['Zip'])) echo $_POST['Zip'] ?>" /></div> <div class="content">Phone: <input type="text" maxlength="20" name="Phone" id="Phone" size="15" value="<?php if (empty($_POST['Phone'])) echo $_POST['Phone'] ?>" /></div> <div class="submit"><input type="submit" name="Submit" id="Submit" namevalue="Update Entry" /></div> </form> <?php } ?> What this winds up saying is "If the form has been submitted, do the check and print out the error messages ELSE print out the form to be filled out." If you need it to put a message at the top of the form when fields are missing but still print out the form, that's something else. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490762 Share on other sites More sharing options...
jdm95lude Posted March 12, 2008 Author Share Posted March 12, 2008 Ok one last thing. How can I have it validate to where it will not go to a different page if it does not validate properly? Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490767 Share on other sites More sharing options...
revraz Posted March 12, 2008 Share Posted March 12, 2008 Another page or a blank page? Looks like now it's staying on the same page. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490771 Share on other sites More sharing options...
jdm95lude Posted March 12, 2008 Author Share Posted March 12, 2008 Another page. I'm doing an assignment and this form is supposed to wright information to a .txt file (which I haven't done yet) validates that you at least entered something then goes to a page saying it was success full and has the basic layout of the current page. Its supposed to go to AddRecord.php <?php if (isset($_POST['Submit'])== "Update Entry") { if (empty($_POST['Fname']) || empty($_POST['LName']) || empty($_POST['Address']) || empty($_POST['City']) || empty($_POST['State']) || empty($_POST['Zip']) || empty($_POST['Phone'])) { echo "<p class='special'>You must complete the form in full.</p>"; } } ?> <form action="AddRecord.php" method="post" enctype="application/x-www-form-urlencoded"> <div class="content">First Name: <input type="text" maxlength="20" name="Fname" id="Fname" size="15" value="<?php if (empty($_POST['Fname'])) echo $_POST['Fname'] ?>" /></div> <div class="content">Last Name: <input type="text" maxlength="20" name="LName" id="LName" size="15" value="<?php if (empty($_POST['LName'])) echo $_POST['LName'] ?>" /></div> <div class="content">Address: <input type="text" maxlength="20" name="Address" id="Address" size="15" value="<?php if (empty($_POST['Address'])) echo $_POST['Address'] ?>" /></div> <div class="content">City: <input type="text" maxlength="20" name="City" id="City" size="15" value="<?php if (empty($_POST['City'])) echo $_POST['City'] ?>" /></div> <div class="content">State: <input type="text" maxlength="20" name="State" id="State" size="15" value="<?php if (empty($_POST['State'])) echo $_POST['State'] ?>" /></div> <div class="content">Zip: <input type="text" maxlength="20" name="Zip" id="Zip" size="15" value="<?php if (empty($_POST['Zip'])) echo $_POST['Zip'] ?>" /></div> <div class="content">Phone: <input type="text" maxlength="20" name="Phone" id="Phone" size="15" value="<?php if (empty($_POST['Phone'])) echo $_POST['Phone'] ?>" /></div> <div class="submit"><input type="submit" name="Submit" id="Submit" namevalue="Update Entry" /></div> </form> Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490774 Share on other sites More sharing options...
revraz Posted March 12, 2008 Share Posted March 12, 2008 Well your ACTION dictates where it will go when you submit your form. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490781 Share on other sites More sharing options...
jdm95lude Posted March 12, 2008 Author Share Posted March 12, 2008 Sorry about that I don't know what I was thinking. Its been a long day. Thanks for all the help. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490786 Share on other sites More sharing options...
jdm95lude Posted March 12, 2008 Author Share Posted March 12, 2008 Oh I know what I was wanting to know is when I click submit it takes it to AddRecord.php even if the form isn't filled out correctly. So how can I make it where it only goes to AddRecord.php if there are no validation errors. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490788 Share on other sites More sharing options...
BlueSkyIS Posted March 12, 2008 Share Posted March 12, 2008 So how can I make it where it only goes to AddRecord.php if there are no validation errors. Javascript Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-490802 Share on other sites More sharing options...
johnska7 Posted March 13, 2008 Share Posted March 13, 2008 Here is a complete working copy of what it seems you'd like <?php if (isset($_POST['Submit'])== "Update Entry") { $missing = array(); foreach($_POST as $key=>$value) { if(empty($_POST[$key])) { array_push($missing, $key); } } if(!empty($missing)) { $missing = implode(", ", $missing); echo "<p class='special'>You must suppy a value for: ".$missing.".</p>"; } else { echo "<script type=\"text/javascript\">window.location=\"addform.php\"</script>"; } } ?> <form action="" method="post" enctype="application/x-www-form-urlencoded"> <div class="content">First Name: <input type="text" maxlength="20" name="Fname" id="Fname" size="15" value="<?php if (isset($_POST['Fname'])) echo $_POST['Fname'] ?>" /></div> <div class="content">Last Name: <input type="text" maxlength="20" name="LName" id="LName" size="15" value="<?php if (isset($_POST['LName'])) echo $_POST['LName'] ?>" /></div> <div class="content">Address: <input type="text" maxlength="20" name="Address" id="Address" size="15" value="<?php if (isset($_POST['Address'])) echo $_POST['Address'] ?>" /></div> <div class="content">City: <input type="text" maxlength="20" name="City" id="City" size="15" value="<?php if (isset($_POST['City'])) echo $_POST['City'] ?>" /></div> <div class="content">State: <input type="text" maxlength="20" name="State" id="State" size="15" value="<?php if (isset($_POST['State'])) echo $_POST['State'] ?>" /></div> <div class="content">Zip: <input type="text" maxlength="20" name="Zip" id="Zip" size="15" value="<?php if (isset($_POST['Zip'])) echo $_POST['Zip'] ?>" /></div> <div class="content">Phone: <input type="text" maxlength="20" name="Phone" id="Phone" size="15" value="<?php if (isset($_POST['Phone'])) echo $_POST['Phone'] ?>" /></div> <div class="submit"><input type="submit" name="Submit" id="Submit" namevalue="Update Entry" /></div> </form> The extra array code at the top will see what the person is missing from their submission and tell them exactly what fields they still need to fill out and it's dynamic, so if you add more fields, it will pick them up. Link to comment https://forums.phpfreaks.com/topic/95855-form-validation/#findComment-491176 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.