【问题标题】:validate form with checkform session?使用 checkform 会话验证表单?
【发布时间】:2014-01-16 06:53:03
【问题描述】:

如何验证表单值?

让我的代码运行起来

  1. 在输入创建用户中填写用户,如“aaaa”。并点击“检查用户”按钮

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

  3. 点击“确定”按钮

为什么检查表会显示“请输入用户名”。 ??

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'];
?>

【问题讨论】:

    标签: php forms validation session


    【解决方案1】:

    那是因为您正在访问会话变量上的未定义常量,就像这样..

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

    应该是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 2013-10-22
      • 1970-01-01
      相关资源
      最近更新 更多