Multiple form validation

Discussion in 'PHP' started by Omzy, Dec 17, 2008.

  1. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #21
    deathshadow - i'm not sure what your code above does, but I have done the following now:

    function form1($values=array()) 
    {
    $nameclass="text";
    		
    if ($values['name'] == null)
    {
    	$nameclass="error";
    }
    //
    <input class="'.$nameclass.'" type="text" id="name" name="name" value="'.$_POST['name'].'" size="25"/>
    //
    }
    Code (markup):
    if ($_POST['process'] == 1)
    	{
    		if ($_POST['name'] == null || $_POST['company'] == null || $_POST['address1'] == null)
    		{
    			form1($_POST);
    		}
    		else
    		{
    			form2(('0'));
    		}
    	}
    Code (markup):

    This works fine and it assigns a different class attribue to the form input if the field is null. However, I am just repeating the code here and as I start validating more fields I will be repeating all the code - is there a better way of doing this? Can we pass variables to the function that lists the problematic fields? Ideally I want all the validation stuff done in the IF Statement at the bottom.
     
    Omzy, Dec 19, 2008 IP
  2. Omzy

    Omzy Peon

    Messages:
    249
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #22
    Bump...........
     
    Omzy, Dec 20, 2008 IP
  3. Sam-Lighty

    Sam-Lighty Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #23
    To check for an empty field you can use
    empty($_POST['name']

    Hope this helps!
     
    Sam-Lighty, Dec 21, 2008 IP