dongpian4954 2010-02-19 16:22
浏览 59
已采纳

如果用户在我的字段中写入格式错误的条目,如何防止发送表单? 即。 数字字母中的字母

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>

  • 写回答

3条回答 默认 最新

  • ds42774 2010-02-19 16:33
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂