Jump to content

Form Validation


jdm95lude

Recommended Posts

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

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.