Self Submitting Form

Discussion in 'PHP' started by misslilbit02, Jun 7, 2007.

  1. #1
    Hi I have a self submitting form and I have a function that is a called to print out errors. Some of the code in the form is below. In the function I put the errors in an array. When I submit the form only two errors are printed out at a time. I want to know why is that if I'm constantly putting my errors in an array only two errors are printed at a time. The form is processing all the errors and will not let the user submit the form with errors but it won't print out all the errors all at once. Can someone help me figure out why only two errors are printed at a time?

    function validate_form()
    {
    if($_POST['Submit']=="Submit")
    {
    $errors=array();

    //Check for errors
    if(! strlen(trim($_POST['name'])))
    {
    $errors[] = "Please enter your name";
    }
    elseif(! strlen(trim($_POST['major'])))
    {
    $errors[] = "Please enter your major";
    }

    >>more and more error checking not included in this snippet<<

    return $errors;
    }

    <?php
    if(isset($_POST['Submit']) && $_POST['Submit']=="Submit")
    {
    $results;
    $results=validate_form();
    if(count($results) == 0)
    {
    process_form();
    }
    else{
    for($i=0; $i<count($results); $i++)
    {
    echo "$results[$i], ";
    }

    }
    }
    >>html code for the form<<
    ?>
     
    misslilbit02, Jun 7, 2007 IP
  2. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #2
    
    <?php
    	$errors = array();
    	
    	foreach($_POST as $key => $value)
    	{
    		if(empty($value) || !isset($value))
    			array_push($errors,$key." must be filled out");
    	}
    
    	if(!errors[0])
    	{
    		// form was successful
    	}
    	else
    	{
    		echo implode("<br />",$errors);
    	}
    ?>
    
    PHP:
    i would do something like that though this is just a rough outline of it to give you an idea.
     
    ansi, Jun 7, 2007 IP
  3. Nikolas

    Nikolas Well-Known Member

    Messages:
    1,022
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    150
    #3
    Maybe the elseif is your problem. For instance here :

    
    if(! strlen(trim($_POST['name'])))
    {
    $errors[] = "Please enter your name";
    }
    elseif(! strlen(trim($_POST['major'])))
    {
    $errors[] = "Please enter your major";
    } 
    
    PHP:
    If both are empty (BTW you could use empty() for this) only the first will show up as error. The right one would be :


    
    if(! strlen(trim($_POST['name'])))
    {
    $errors[] = "Please enter your name";
    }
    if(! strlen(trim($_POST['major'])))
    {
    $errors[] = "Please enter your major";
    } 
    
    PHP:
     
    Nikolas, Jun 7, 2007 IP