开发者

How can I prevent my form being sent if a user writes misformated entries in my fields? ie. Letters in number fields

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>


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)....


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜