通过PHP服务器端提交数据
我对表单为什么不发送然后被重定向到index.php页面感到困惑。我对PHP非常陌生,并尝试使用User对象。这是我到目前为止所拥有的通过PHP服务器端提交数据,php,html,client-server,database-update,Php,Html,Client Server,Database Update,我对表单为什么不发送然后被重定向到index.php页面感到困惑。我对PHP非常陌生,并尝试使用User对象。这是我到目前为止所拥有的 <main> <header> <h1>Create New Account</h1> </header> <section id="formEntry"> <fo
<main>
<header>
<h1>Create New Account</h1>
</header>
<section id="formEntry">
<form id="createacct" name="createacct" action="CreateAccount.php" method="post" onsubmit="return validateForm()">
<fieldset>
<legend>Enter your information</legend>
<table>
<tr>
<td class="fieldName">First Name</td>
<td class="inputField"><input type="text" id="firstname" name="firstname" form="createacct" size="30" maxlength="50" onchange="firstNameUpdated()"></td>
<td id="firstnamemsg"></td>
</tr>
<tr>
<td class="fieldName">Last Name</td>
<td class="inputField"><input type="text" id="lastname" name="lastname" form="createacct" size="30" maxlength="50" onchange="lastNameUpdated()"></td>
<td id="lastnamemsg"></td>
</tr>
<tr>
<td class="fieldName">Email</td>
<td class="inputField"><input type="email" id="emailAddress" name="emailAddress" form="createacct" size="30" maxlength="50" onchange="emailUpdated()"></td>
<td id="emailmsg"></td>
</tr>
<tr>
<td class="fieldName">Select a username</td>
<td class="inputField"><input type="text" id="username" name="username" form="createacct" size="30" maxlength="50" onchange="usernameUpdated()"></td>
<td id="usernamemsg"></td>
</tr>
<tr>
<td class="fieldName">Enter your password</td>
<td class="inputField"><input type="password" id="password" name="password" form="createacct" size="30" maxlength="50" onchange="pwdUpdated()"></td>
<td id="passwordmsg"></td>
</tr>
<tr>
<td class="fieldName">Confirm password</td>
<td class="inputField"><input type="password" id="confirmpwd" name="confirmpwd" form="createacct" size="30" maxlength="50" onchange="confirmUpdated()"></td>
<td id="confirmpwdmsg"></td>
</tr>
<tr>
<td id="saveMsg"></td>
<td colspan="2" class="submitButton">
<input type="reset" value="Clear all fields" class="styleButton" onclick="clearFields(this)">
<input type="submit" value="Create New Account" class="styleButton" onclick="return createNewAccount()">
</td>
</tr>
</table>
</fieldset>
</form>
</section>
<?php
include 'CreateAccount.php';
$user = new User();
$user->first = trim($_POST["firstname"]);
$user->last = trim($_POST["lastname"]);
$user->emailaddress = trim($_POST["emailAddress"]);
$user->username = trim($_POST["username"]);
$user->password = trim($_POST["password"]);
$user->confirmpwd = trim($_POST["confirmpwd"]);
$isValid = $user->validate();
?>
</main>
创建新帐户
输入您的信息
名字
姓
电子邮件
选择用户名
输入您的密码
确认密码
以及PHP文件(CreateAccount.PHP):
我做错了什么?每次我提交时,它都会将我重定向到CreateAccount.php并显示为空白。谢谢你的帮助 我让它工作起来了。我做的都是错的,事实上,我花了好几个小时来解决错误,并将代码四处移动
<?php
# In your CreateAccount.php,
# update your code like this
Class User {
public $first;
public $last;
public $emailaddress;
public $username;
public $password;
public $confirmpwd;
// validation function
function validate() {
if (($this->first != null || $this->first != '') &&
($this->last != null || $this->last != '') &&
($this->emailaddress != null || $this->emailaddress != '') &&
($this->username != null || $this->username != '') &&
($this->password != null || $this->password != '') &&
($this->confirmpwd != null || $this->confirmpwd != '') &&
($this->password === $this->confirmpwd)) {
storeData($this->first, $this->last, $this->emailaddress, $this->username, $this->password);
header('Location: TestPage.php');
} else {
header('Location: CreateNewAccount.php');
}
}
}
#notice that the exit code is been removed and hopefully this should work..
事实证明,我不需要PHP网页中的PHP代码,因为我没有类对象。下面是我所做的-代码片段:
<?php
class User {
public $first = '';
public $last = '';
public $emailaddress = '';
public $username = '';
public $password = '';
public $confirmpwd = '';
function validate() {
if ($this->first === null || $this->first === '') {
return false;
}
if ($this->last === null || $this->first === '') {
return false;
}
if ($this->emailaddress === null || $this->emailaddress === '') {
return false;
}
if ($this->username === null || $this->username === '') {
return false;
}
if ($this->password === null || $this->password === '') {
return false;
}
if ($this->confirmpwd === null || $this->confirmpwd === '') {
return false;
}
if ($this->password !== $this->confirmpwd) {
return false;
}
return true;
}
}
请注意,我将变量从PHP网页移动到了PHP文件(CreateAccount.PHP),问题得到了解决,并且没有得到向我抛出的未定义索引错误
关于我在原始代码中使用的exit(),它可以使用exit(),也可以不使用exit(),尽管这不是必需的,但出于方便的目的 storeData()
的作用是什么?它的函数在哪里?请删除exit()并重试,因为您在创建用户对象后退出,将永远无法访问validate(),页面将为空。facepalming我自己。是的,那个出口在这里就可以了。您的脚本正在退出,没有处理任何内容您正在将数据发布到CreateAccount.php,它只有一个类定义,没有其他内容。没有调用函数,您希望发生什么?也许您想将表单的操作更改为“”,以便它发布到您实际使用用户类的当前页面?当您复制和粘贴操作代码时,您必须添加对更改内容的解释,以及这将解决问题的原因。另外,如果你也阅读了其他人发布的答案和评论,这也会很方便。是的,当然,但我的PHPSTORM IDE通常在我发布代码之前测试代码,所以仅供参考,答案是肯定的,我已经测试过它,它工作正常。我正在使用NetBeans IDE并删除exit(),就像我上面所说的那样,没有做到这一点。它只是显示了错误列表。
<?php
class User {
public $first = '';
public $last = '';
public $emailaddress = '';
public $username = '';
public $password = '';
public $confirmpwd = '';
function validate() {
if ($this->first === null || $this->first === '') {
return false;
}
if ($this->last === null || $this->first === '') {
return false;
}
if ($this->emailaddress === null || $this->emailaddress === '') {
return false;
}
if ($this->username === null || $this->username === '') {
return false;
}
if ($this->password === null || $this->password === '') {
return false;
}
if ($this->confirmpwd === null || $this->confirmpwd === '') {
return false;
}
if ($this->password !== $this->confirmpwd) {
return false;
}
return true;
}
}
<?php
include 'User.php';
$user = new User();
$user->first = trim($_POST["firstname"]);
$user->last = trim($_POST["lastname"]);
$user->emailaddress = trim($_POST["emailAddress"]);
$user->username = trim($_POST["username"]);
$user->password = trim($_POST["password"]);
$user->confirmpwd = trim($_POST["confirmpwd"]);
$isValid = $user->validate();
if ($isValid) {
storeData($first, $last, $emailaddress, $username, $password); //ignore the line
header('Location: index.php');
} else {
header('Location: CreateNewAccount.php');
}
?>