views:

87

answers:

3

What happens:

When I write two values in both text boxes, the page doesn't show the Congratulations message as it should. When I write only 1 value, the correct thing happens, which is not show the congratulations message.

What should happen:

If a user writes only 1 value, the form should still appear with any previously filled out fields still there. If a user writes values in all of the fields, the Congratulations should appear.

Edit - Finally got it working, in case any other newbies want to check it out:

<html>
<head>
    <?php
    $validForm = false;

    function getValue($field){
        if(isset($_GET[$field])){
            return htmlspecialchars(trim($_GET[$field]));
        }
        else{
            return "";
        }
    }

    function validateForm($value,$type){
        $field = $_GET[$value];

        //magic goes here.
        switch ($type){
            case 'required':
                if (!isset($field) || ($field=="")){
                    global $validForm;
                    $validForm = false;
                }
                else{
                    global $validForm;
                    $validForm = true;
                }
                break;
            case 'email':
                $regexp = "/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z-][0-9a-zA-Z-]+\.)+[a-zA-Z](2,6)$/";
                if(isset($field) && preg_match($regexp,$field)){
                    global $validForm;
                    $validForm = true;
                }
                else {
                    global $validForm;
                    $validForm = false;
                }
                break;
            case 'number':
                if(!isset($field) || ($field=="") || (!is_numeric($field))){
                    global $validForm;
                    $validForm = false;
                }
                else{
                    global $validForm;
                    $validForm = true;
                }
                break;
            default:
                die('Validacion desconocida.');
        }         
    }        
    ?>
</head>

<body>
    <?php validateForm('name','required'); ?>
    <?php validateForm('lastname','required'); ?>

    <?php if($validForm == false){ ?>
    <form action="class2.php" method="get">
        <dl>
            <dt>First Name:</dt>
            <dd><input type="text" value="<?php echo htmlspecialchars(getValue('name')) ?>" name="name" />                
            </dd>                

            <dt>Last Name:</dt>
            <dd><input type="text" value="<?php echo htmlspecialchars(getValue('lastname')) ?>" name="lastname" />                
            </dd>

            <br />                
            <dt>
                <input type="submit" value="enviar" name="validate"/>
            </dt>                
        </dl>
    </form>
    <?php
    } else {
    ?>

    <h1>Congratulations, you succesfully filled out the form!</h1>

    <?php }
    ?>
</body>

A: 

The issue is that you're calling validateForm() after you check the value of $validForm. When you check $validForm right after your body tag, it is always going to be false. It will get set to true (assuming the form is valid) by the time it gets down past the second field, but you're already in the first branch of the if statement at that point, so the "congratulations" message will never be displayed.

To fix, just move your validation calls to before you check the value of $validForm:

<body>
     <?php
    validateForm($_GET['name'],'required');
    validateForm($_GET['lastname'],'required');

    if($validForm == false){ ?>
    <form>

And so on.

eliah
I understand what you mean. That sounds logical, but it's still not showing what it's supposed to. :(
Serg
+2  A: 

there appears to be a problem with the $validForm variable in the validateForm function.

I think your assuming changes to $validForm inside the function change the same variable name outside the function. because you haven't set it as a global variable it won't do this for you.

You need to look at Variable scope in PHP.

http://php.net/manual/en/language.variables.scope.php

this will explain how you should handle this variable. you can return the value in the function..

e.g for that function just return the variable:

    function validateField($value,$type){


    //magic goes here.
    switch ($type){
        case 'required':
            if (!isset($value) || ($value== "")){
                $valid = false;
            }
            else{
                $valid = true;
            }
            break;
        case 'email':
            $regexp = "/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z-][0-9a-zA-Z-]+\.)+[a-zA-Z](2,6)$/";
            if(isset($value) && preg_match($regexp,$variable)){
                $valid = true;
            }
            else {
                $valid = false;
            }
            break;
        case 'number':
            if(!isset($value) || ($value=="") || (!is_numeric($value))){
                $valid = false;
            }
            else{
                $valid = true;
            }
            break;
        default:
            die('Validacion desconocida.');
    } 
    return $valid;
}        

That will solve the problem in the function

to get the variable out do :

$formValid = true;
if (!validateField($_GET['name'],'required'))
{
    $formValid = false;
}
if (!validateField($_GET['lastname'],'required'))
{
    $formValid = false;
}

if ($formValid)....

Derek Organ
What the...is this true? I'm used to C# so that's why I declared the variable right above the method so it's scope would be in all the methods below it. :x
Serg
Can you give code example of the global in my context? I can't seem to get it to work.
Serg
added some code, you had a good few things wrong.. that function validates a field not the whole form. call it for each field and then if all are ok then display success else show form again with fields. Also you you were reusing the $_GET var inside the function. there is no need because you already passed in the field value in the first place.
Derek Organ
PS i wrote that quickly, might be a syntax error or something but you get the idea.
Derek Organ
PS almost all programming languages have variable scope the same as this including c#, C, C++, Java ...
Derek Organ
A: 

2 things:

1) I don't think your $validForm variable is in scope with the function validateForm. It seems that you would need to call global on it inside the function global $validate form.

2) you call validateForm after you set it to false. you need to do the checks before you do the conditional.

easement
I think it's in scope because Komodo Edit shows it to me on intellisense.
Serg