2016-10-08 16 views
-2

なぜフォームが送信されず、次にindex.phpページにリダイレクトされるのか困惑しています。私はPHPを初めて使い、Userオブジェクトを使用しようとしています。ここに私がこれまで持っているものがあります。PHPサーバー側からのデータ提出

<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()  

>

私はここで間違って何をしているのですか?私が提出するたびに、私はCreateAccount.phpにリダイレクトされ、空白になります。ご協力いただきありがとうございます。

+0

http://php.net/manual/en/function.error-reporting.php –

+0

'storeData()'は何を行い、どこに機能がありますか? –

+1

ユーザーオブジェクトを作成した後で終了するため、exit()を削除してもう一度やり直してください。validate()には到達せず、ページは空白になります。 –

答えて

-1

私はこれを動作させました。私は間違ったことをしていましたが、実際には、エラーを解決してコードを移動する時間を費やしました。

PHPのWebページにPHPコードが必要ないことが判明しました。クラスオブジェクトがないためでした。ここに私がやったことだ - コードスニペット:バック(CreateAccount.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 

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のWebページからPHPファイル(CreateAccount.php)に移動しました。問題が解決され、未定義のインデックスエラーが表示されませんでした。

exit()については、元のコードで使用しましたが、必要ではないが便宜上exit()の有無にかかわらず動作します。

-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.. 
+1

OPコードをコピーして貼り付けるときには、変更した内容の説明と、これが問題を解決する理由を追加する必要があります。また、他の人が投稿した回答やコメントも読んでおけば便利です。 – Franco

+0

Vural Acarはすでにそこのコメントにこう言っています。 –

+0

確かに、私のPHPSTORM IDEは、私がここに投稿する前にコードをテストしています。だから、答えははいです。私はそれをテストしてうまくいきます。 – Dennisrec

関連する問題