无法使用POST传递表单信息

时间:2011-07-10 13:35:49

标签: php html forms post input

我正在尝试使用POST的简单表单,但我无法将输入传递给页面。文件是“agenda.php”,代码如下(最后是表单部分):

<?php
/* includes lib */
include_once("lib.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<script type="text/javascript" src="lib.js"></script>
<link href="schedulerStyle.css" rel="stylesheet" type="text/css">
<title>Scheduler</title>
</head>
<body>
<div id="wrapper">
    <div id="box">
        <h2>Your Agenda</h2>
        <?php 
        if(isset($_SESSION['name']))
        {
            if(isset($_SESSION['meetingCode']))
            {
                $firstDate = getFirstDate($_SESSION['meetingCode']);
                if($firstDate != "")
                {
                    echo "<h3>Organizing meeting starting from ".$firstDate."</h3>";
                    ?>

                    <?php 
                }
                else 
                {
                    unset($_SESSION['name']);
                    unset($_SESSION['meetingCode']);
                    header("Location: agenda.php");
                }
            }
            else
            {
                unset($_SESSION['meetingCode']);
                header("Location: agenda.php");
            }
        }
        else 
        {
            if(!isset($_POST['name']) || $_POST['name'] == "")
            {
            ?>
            <form action="agenda.php" method="post" id="loginForm" 
                    name="loginForm">
                <h3>Insert your name</h3>
                <input type="text" title="Insert your name" maxlength="8" 
                    size="8" id="name" name="name">
                <h3>Insert meeting password</h3>
                <input type="password" title="Insert meeting password" 
                    maxlength="8" size="8" id="meetingPassword" 
                    name="meetingPassword">
                <br><br>
                <input title="Login" type="submit" value="Login">
                <input title="Reset fields" type="reset">
                <a href="index.php">
                <input title="Go to homepage" type="button" value="Back">
                </a>
            </form>
            <?php
            }
            else 
            {
                $meetingCode = checkUserIn($_POST['name'], $_POST['meetingPassword']);
                if(is_int($meetingCode) && $meetingCode > 0)
                {
                    $_SESSION['name'] = $_POST['name'];
                    $_SESSION['meetingCode'] = $meetingCode;
                }
                unset($_POST['name']);
                unset($_POST['meetingPassword']);
                header("Location: agenda.php");
            } 
        }
        ?>
    </div>
</div>
</body>
</html>

此代码的行为是:

  • 它始终显示表格
  • $ _ POST ['name']永远不会被设置

对于其他信息代码没有显示问我,我会更新它。感谢

供参考,lib.php如下:

<?php
$hostDB = "localhost";
$userDB = "root";
$passDB = "";
$dataDB = "scheduler";  

function printAlertScript($msg)
{
    echo "<script type=\"text/javascript\">
            <!--
            alert(\"$msg\");
            //-->
            </script>";
}

function createRandPassword8Chars()
{
    return substr(md5(rand()), 16, 8);
}

function createMeeting($meetingStartDate, $rangeN)
{
    /* opens database connection */
    $conn = new mysqli($GLOBALS['hostDB'], $GLOBALS['userDB'], 
                            $GLOBALS['passDB'], $GLOBALS['dataDB']);
    if (mysqli_connect_errno())
    {
        die("Connect failed: ".mysqli_connect_error());
    }
    $query = "SELECT MAX(meetingCode) FROM meetings;";
    if(!$result = @$conn->query($query))
    {
        die("Query error: " .$query."<br>");
    }
    if($result->num_rows == 0)
        $meetingCode = 0;
    else 
    {
        $item = $result->fetch_array(MYSQL_NUM);
        $meetingCode = (int)$item[0];
        $meetingCode++;
    }
    @$result->close();

    printAlertScript("Meeting code assigned: $meetingCode");

    $pass = createRandPassword8Chars();

    printAlertScript("Password assigned: $pass");
    $GLOBALS['meetingPassword'] = $pass;

    $range = $conn->real_escape_string($rangeN);

    while($range > 0)
    {
        $meetingDate = date('Y-m-d', $meetingStartDate); 
        $query = "INSERT INTO meetings VALUES('$meetingCode', 
            '$meetingDate', '".md5($pass)."');";
        $meetingStartDate = $meetingStartDate + (60 * 60 * 24);

        if(!$result = @$conn->query($query))
        {
            die("Query error: " .$query."<br>");
        }
        $range = $range - 1; 
    }

    /* closes connection */
    @$conn->close();

    return $meetingCode;
}

function addUser($meetingCode, $userN)
{
    /* opens database connection */
    $conn = new mysqli($GLOBALS['hostDB'], $GLOBALS['userDB'], 
                            $GLOBALS['passDB'], $GLOBALS['dataDB']);
    if (mysqli_connect_errno())
    {
        die("Connect failed: ".mysqli_connect_error());
    }
    $user = $conn->real_escape_string($userN);
    $query = "INSERT INTO invites VALUES('$meetingCode', '$user');";

    if(!$result = @$conn->query($query))
    {
        die("Query error: " .$query."<br>");
    }
}

function checkUserIn($nameN, $passwordN)
{
    /* opens database connection */
    $conn = new mysqli($GLOBALS['hostDB'], $GLOBALS['userDB'], 
                            $GLOBALS['passDB'], $GLOBALS['dataDB']);
    if (mysqli_connect_errno())
    {
        die("Connect failed: ".mysqli_connect_error());
    }
    $name = $conn->real_escape_string($nameN);
    $password = $conn->real_escape_string($passwordN);
    $query = "SELECT meetings.meetingCode FROM meetings, invites 
                WHERE password='".md5($password)."' AND user='$name'
                AND meetings.meetingCode = invites.meetingCode;";
    if(!$result = @$conn->query($query))
    {
        die("Query error: " .$query."<br>");
    }
    if($result->num_rows > 0)
    {
        $item = $result->fetch_array(MYSQL_NUM);
        $meetingCode = $item[0];
        $result->close();
        return $meetingCode;
    }
    return "";
}

function getFirstDate($meetingCode)
{
    /* opens database connection */
    $conn = new mysqli($GLOBALS['hostDB'], $GLOBALS['userDB'], 
                            $GLOBALS['passDB'], $GLOBALS['dataDB']);
    if (mysqli_connect_errno())
    {
        die("Connect failed: ".mysqli_connect_error());
    }
    $query = "SELECT MIN(meetingDate) FROM meetings
                WHERE meetingCode=$meetingCode;"; 
    if(!$result = @$conn->query($query))
    {
        die("Query error: " .$query."<br>");
    }
    if($result->num_rows > 0)
    {
        $item = $result->fetch_array(MYSQL_NUM);
        $firstDate = $item[0];
        $result->close();
        return $firstDate;
    }
    return "";
}

function getMeetingData($meetingCode)
{
    /* opens database connection */
    $conn = new mysqli($GLOBALS['hostDB'], $GLOBALS['userDB'], 
                            $GLOBALS['passDB'], $GLOBALS['dataDB']);
    if (mysqli_connect_errno())
    {
        die("Connect failed: ".mysqli_connect_error());
    }
    $query = "SELECT * FROM meetings, invites 
        WHERE user='$nameE' AND pass='".md5($passE)."' 
        AND meetings.meetingCode=invites.meetingCode;"; 
    if(!$result = @$conn->query($query))
    {
        die("Query error: " .$query."<br>");
    }

}

function getUserData($name, $pass)
{
    /* opens database connection */
    $conn = new mysqli($GLOBALS['hostDB'], $GLOBALS['userDB'], 
                            $GLOBALS['passDB'], $GLOBALS['dataDB']);
    if (mysqli_connect_errno())
    {
        die("Connect failed: ".mysqli_connect_error());
    }
    if(strlen($name) > 0 && strlen($name) <= 8)
    {
        $nameE = @$conn->real_escape_string($name);
        $passE = @$conn->real_escape_string($pass);
        /* searches for meetings where user is invited */
        $query = "SELECT * FROM meetings, invites 
            WHERE user='$nameE' AND pass='".md5($passE)."' 
            AND meetings.meetingCode=invites.meetingCode;"; 
        if(!$result = @$conn->query($query))
        {
            die("Query error: " .$query."<br>");
        }
        @$result->close();
    }
    /* closes connection */
    @$conn->close();

    return;
}
?>

另外,addUser.php,一个同一目录中的文件,并且具有相似的表单,工作正常:

<?php
/* includes lib */
include_once("lib.php");

global $meetingPassword;

/* if it is the first time tries to create the meeting */
if(isset($_POST['monthInput']) && isset($_POST['dayInput'])
    && isset($_POST['yearInput']) && isset($_POST['daysRange'])
    && isset($_POST['usernameInput']))
{
    $unixTime = mktime(0,0,0, $_POST['monthInput'], 
                        $_POST['dayInput'], $_POST['yearInput']);
    if($unixTime === false || $unixTime < time())
    {
        printAlertScript("Unable to use date");
        header("Location: index.php");
    }
    $meetingCode = createMeeting($unixTime, $_POST['daysRange']);
    if(isset($meetingCode) && is_int($meetingCode) && $meetingCode >= 0)
    {
        addUser($meetingCode, $_POST['usernameInput']);
        $_SESSION['name'] = $_POST['usernameInput'];
        $_SESSION['meetingCode'] = $meetingCode;
    }
    else 
        header("Location: index.php");
    unset($_POST['monthInput']); 
    unset($_POST['dayInput']);
    unset($_POST['yearInput']); 
    unset($_POST['daysRange']);
    unset($_POST['usernameInput']);
}
/* if someone has been invited */
if(isset($_POST['friendInput']) && isset($_SESSION['meetingCode']))
{
    addUser($_SESSION['meetingCode'], $_POST['friendInput']);
    unset($_POST['friendInput']);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<script type="text/javascript" src="lib.js"></script>
<link href="schedulerStyle.css" rel="stylesheet" type="text/css">
<title>Scheduler</title>
</head>
<body>
<div id="wrapper">
    <div id="box">
        <h2>Invite people</h2>
        <h3>Password is <?php echo $GLOBALS['meetingPassword']; ?></h3>
        <h3>Write username of a friend</h3>
        <form action="addUser.php" method="post" id="addForm" name="addForm">
            <input type="text" title="Insert a friend's name" maxlength="8" 
                size="8" id="friendInput" name="friendInput">
            <input type="submit" title="Add user to invite list" value="Add">
            <a href="agenda.php">
            <input type="button" title="I finished inviting" value="Done">
            </a>
        </form>
    </div>
</div>
</body>
</html>

另外,如果我尝试引用POST变量,我会

Notice: Undefined index: name in C:\xampp\htdocs\home\agenda.php

我在Windows 7上使用XAMPP 1.7.4 [PHP:5.3.5]

1 个答案:

答案 0 :(得分:1)

您是否检查过以确保lib.php没有干扰?我把这个脚本放在我的网站上:http://vapor.ne8us.com/stacks/agenda.php。这工作得很好,它在checkUser上打破,它应该。我猜测lib.php要么是干扰,要么是.js文件,或者你的服务器没有正确设置php。