php - 使用 JavaScript 和 PHP 的动态表单问题

标签 php javascript html forms error-handling

我在处理这些文件时遇到了问题,看看你们能否帮我解决 :) 我收到一个需要对象的错误..

index.php

<?php 
  //Start session
  session_start(); 
  //Require functions.php
  require("functions.php");
  //Require posts.php
  require("posts.php");
?>
<html>
<body>
<title>Log Into Booking System</title>
  <?php 
    //Call function to display login
    displayLogin();
  ?>
</body>
</html>

posts.php

<?php
if (isset($_POST['sublogin']))
{
    //Check that all fields were typed in and are not null
    if(!$_POST['user'] || !$_POST['pass'])
    {
        //Call function to display error
        printText("Please make sure all required fields are filled in.");
    }
    else
    {
        //Here is some database checking
    }
}
?>

函数.php

<?php   
function displayLogin()
{
    //Break php, then return to PHP
    ?> 
    <h1>Login</h1>
    <form action="" method="post">
    <table align="left" border="0" cellspacing="0" cellpadding="3">
    <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
    <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
    <tr><td><div id='error'></div></td></tr>
    <tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr> 
    </table>    
    </form>
    <?
}

function printText($txtmsg)
{
    ?>      
    <!-- Set up the displayError javascript function -->
    <script type="text/javascript">
        function displayError()
        {                   
            document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
        }
    </script>
    <!-- Call function to Display the error -->
    <script type="text/javascript">
        displayError();
    </script>
    <?
}
?>

我很确定

document.getElementById('error').innerHTML

给我带来了问题,我认为这可能与我如何调用它有关,就像它不知道它在寻找什么..

非常感谢你们,

珍妮

编辑:这里是 jQuerybeast 试图做的粘贴,http://pastebin.com/jj0RVMAd

解决方案:谁知道这么简单?您必须更改需要 posts.php 的位置的顺序,并确保您没有在 functions.php 中调用 javascript 函数,因为这将执行表单上方的 javascript,因此会出现空对象错误。

索引.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
<head>
<title>Log Into Booking System</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<?php 
//Start session
session_start(); 
//Require functions.php
require("functions.php");
?>
</head>
<body>
<?php 
//Call function to display login
displayLogin(); 
//Require posts.php
require("posts.php");
?>
</body>
</html>

posts.php

<?php
//POST: Main login page
if (isset($_POST['sublogin']))
{
    //Display error if feilds are not filled in
    if(!$_POST['user'] || !$_POST['pass'])
    {
        echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure all required fields are filled in.";</script>';
        return false;
    }
    else
    {
        //Some database minipulation here....
        //...
        if ($_POST['user'] != $databaseUsername)
        {
        echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure your username is correct.";</script>';
        return false;
        }
        elseif($_POST['pass'] != $databasePassword)
        {
        echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure your password is correct.";</script>';
        return false;           
        }
    }
    return true;
}

?>

函数.php

<?php


//Function: Display main.php login
function displayLogin()
{

    //Break php, then return to PHP
    ?> 
    <div style="float: left; padding: 10px; width:20%;">
    <fieldset>
    <legend>
    <h1>Login</h1>
    </legend>
    <form name = "mainlogin" action="" method="post">
    <table align="left" border="0" cellspacing="0" cellpadding="3">
    <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
    <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
    <tr><td colspan="2" align="left"><input type="checkbox" name="remember">
    <font size="2">Remember username and password</td></tr> 
    <tr><td><div id="error"></div></tr><td>
    <tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr> 
    </table>    
    </form>
    </fieldset>
    </div>

    <?php

}

?>

最佳答案

我注意到的第一件事是 Javascript 函数中的 PHP 回显字符串周围没有引号。

改变:

    function displayError()
    {                   
        document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
    }

收件人:

    function displayError()
    {                   
        document.getElementById('error').innerHTML = "<?echo $txtmsg;?>";
    }

关于php - 使用 JavaScript 和 PHP 的动态表单问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7018915/

相关文章:

javascript - 当一个类存在多个项目时,Jquery 定位

javascript - 我如何从 Meteorjs 中的多个文件访问一个集合?

javascript - 选择和选项值

php - SQL join into join 创建好数组

php - 从 Ubuntu 和 PHP 访问 Azure MSSQL 服务器

php - SQL 查询的语法错误

javascript - 根据#hash 添加/删除 CSS 类

html - 我应该使用 Bootstrap 吗?

javascript - 如何将div元素设置为始终从左起三分之一

php - 使用 PHP 连接到列式数据库