我很困惑为什么表单不会发送然后被重定向到index.php页面。我是PHP的新手,并尝试使用User对象。这是我到目前为止所拥有的。
<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):
<?php
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');
}
}
}
exit()
&GT;
我在这里做错了什么?每次我提交时,它都会将我重定向到CreateAccount.php并显示为空白。谢谢你的帮助。
答案 0 :(得分:-1)
我让这个工作。我做错了,的确,我花了好几个小时来解决错误并移动代码。
事实证明我在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;
}
}
然后回到我上面的原始代码片段(CreateAccount.php),我将代码改为现在的样子:
<?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');
}
?>
请注意,我将变量从PHP网页移动到PHP文件(CreateAccount.php),问题解决了,并且没有收到未定义的索引错误。
关于我在原始代码中使用的exit(),它可以使用或不使用exit(),即使它不是必需的,但是为了方便起见。
答案 1 :(得分:-2)
<?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..