gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:40:23 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com