使用checkform会话验证表单?

时间:2014-01-16 06:53:04

标签: php forms validation session

如何验证表单值?

执行我的代码

  1. 在输入创建用户中填写“aaaa”之类的用户。然后单击“检查用户”按钮

  2. 在输入中填写姓名和姓氏

  3. 点击“确定”按钮

  4. 为什么checkform wiill会显示“请输入用户名”。 ??

    test_1.php

        <?PHP
        session_start();
        session_destroy();
    ?>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
    <script type="text/javascript" src="http://www.jeasyui.com/easyui/jquery.easyui.min.js"></script>
        <script type="text/javascript">
            $(function(){
                $('#ff').form({
                    success:function(data){
                        $.messager.alert('Info', data, 'info');
                    }
                });
            });
        </script>
    </head>
    
    <form id="ff" name="form_check_user" method="post" action="test_2.php" ENCTYPE = "multipart/form-data">
    Create User : <input type="text" name="user"/>
        <input type="submit" name="check_user" value="check user" />
    </form>
    
    <div style=" border-top: 1px solid #eee; width: 39%;"></div>
    <br>
    
    <form name="finished" method="post" action="" ENCTYPE = "multipart/form-data" onsubmit="return checkform(this);">
        Name : <input type="text" name="name"/>
        Last Name : <input type="text" name="last_name"/>
    <br>
        <input type="submit" name="submit" value="OK" />
    </form>
    
    
        <script language="JavaScript" type="text/javascript">
        <!--
        function checkform ( form )
        {
          if (form.name.value == "") {
            alert( "Please enter your name." );
            form.name.focus();
            return false ;
          }
          if (form.last_name.value == "") {
            alert( "Please enter your last name." );
            form.last_name.focus();
            return false ;
          }
          if ("<?php echo $_SESSION[check];?>"  == "") {
            alert( "Please enter User name." );
            form.last_name.focus();
            return false ;
          }
        return true ;
        }
        //-->
        </script>
    

    test_2.php

    <?PHP
        session_start();
         $_SESSION["check"] = $_POST['user'];
    ?>
    

1 个答案:

答案 0 :(得分:1)

这是因为您正在访问会话变量的未定义常量,如此..

if ("<?php echo $_SESSION[check];?>"  == "") { // See it is just check , you need to wrap it around quotes.

应该是

if ("<?php echo $_SESSION['check'];?>"  == "") {