2014-01-29 27 views
-1

我需要發佈在同一頁上的表單中插入的值。如果成功,我會包含另一個頁面,如果不成功,用戶必須再次輸入表單中的字段。我該怎麼做呢?我已經設置了action=""並使用`if(isset($ _ POST ['submit'])){在同一頁上發帖

這裏有什麼問題?請幫助:(`

otp.php

 <form method="POST" action="" onSubmit="return validate(this)" > 
      <input type="button" value="Click for OTP" onclick="openotp()" /> <br/> <br/> 

       <table id="table"> 
        <tr> 
         <td class="alt"><label for="otp">Enter the 6-digit iBanking OTP </label></td> 
         <td><input type="password" name="otp" maxlength="6"></td> 
        </tr> 
       </table> 
      <br/> 
      <input type="submit" name="submit" value="Click to submit OTP"> 
     </form> 
      </center> 

      <?php 
     if(isset($_POST['submit'])){ 
      $otp = $_POST['otp']; 

      $query = "SELECT otp FROM user where user_id='$user_id'"; 
      $result = mysqli_query($link, $query) or die(mysqli_error($link)); 
      $row = mysqli_fetch_array($result); 
      $rand = $row['otp']; 

if ($otp == $rand) { 
$query = "SELECT * FROM user WHERE user_id='$user_id' AND otp='$otp'"; 
$result = mysqli_query($link, $query) or die(mysqli_error($link)); 
      include "index.php"; 

} else { 
    echo "<script>alert('OOPS');</script>"; 
}  
     } 

     ?> 
     </div> 
+1

什麼*不*當您嘗試使用這種事情發生?表單是否發佈到預期頁面?頁面上的代碼是否可以運行?它產生的價值是什麼? – David

+0

究竟是什麼問題?從我所看到的你至少需要檢查一個帖子,如果它已經成功發佈,不包括表格。 – jeroen

+0

如果你沒有'validate()'函數,那麼在你的'onSubmit =「return validate(this)」'和'openotp()'函數中取出它。在那裏沒有功能支持它,會破壞你的代碼。 –

回答

0

如果我理解正確的話,所有你需要做的是改變你的邏輯了一下,也許是這樣的:

<?php 
    $form_posted = isset($_POST['submit']; 

    if($form_posted){ 
     // Do queries and validate post, if successfull, set 
     // the success variable to true. 
     $success = true; 
    } 

    if ($success) { 
     // Yay! Success, load the index 
     include "index.php"; 
    } else { 
     if ($form_posted) { 
      // Form was posted but failed 
      echo "<script>alert('OOPS');</script>"; 
     } else { 
      // Echo form HTML here since the form was not posted yet: 
      echo '<form method="POST" action="" onSubmit="return validate(this)" >'; 
      . 
      . 
      . 
      // Alternatively add the form to another .php file and include it here, like so: 
      include 'otp_form.php'; 
     } 
    } 
?> 
0

,如果你在你的site.php輸入字段填寫,通過行動= 「site.php」 叫site.php, 也許有額外的標誌like action =「site.php?submitted = true」

請求此標誌並檢查您的POST變量,如果正確,則重定向到下一頁,如果不是,則通知用戶並再次顯示輸入字段。 ..

0

在你的情況下,你的行動必須是頁面本身

action="<?php echo $_SERVER['PHP_SELF']; ?>"

+0

你好,爲什麼不能動作=「」因爲我在同一頁面上有一個類似POST的頁面,但我使用action =「」 ,它工作得很好。對不起,我在這方面很新。 – user3210617

+0

在HTML5中,action =「」打破驗證 – Sean

0

爲什麼你不使用$_SERVER['PHP_SELF']

<form method="POST" action="<?php $_SERVER['PHP_SELF'] ?>" onSubmit="return validate(this)" > 
+0

我試過了,它不起作用。 – user3210617

0

因爲你發佈到同一個頁面,你應該首先測試爲POST變量。如果設置,則可以評估發佈的數據。否則,沒有POST數據顯示錶單。

接下來,您可能希望更改index.php頁面的名稱,因爲這是網站(或文件夾)的默認頁面名稱。據推測,這頁是index.php頁面中,你可能包括像index.inc.php

<?php 
    if(isset($_POST['otp'])){ 
     $otp = $_POST['otp']; 

     $query = "SELECT otp FROM user where user_id='$user_id'"; 
     $result = mysqli_query($link, $query) or die(mysqli_error($link)); 
     $row = mysqli_fetch_array($result); 
     $rand = $row['otp']; 

     if ($otp == $rand) { 
      $query = "SELECT * FROM user WHERE user_id='$user_id' AND otp='$otp'"; 
      $result = mysqli_query($link, $query) or die(mysqli_error($link)); 

      include "index.inc.php"; 

     } else { 
      echo "<script>alert('OOPS');</script>"; 
     }  
    }else{ 
?> 

    <html> 
    <head> 
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

      <script type="text/javascript"> 
       $(document).ready(function() { 

        //jQuery/javascript code in here 

       }); //END $(document).ready() 
      </script> 
     </head> 
    <body> 
     <form method="POST" action="" onSubmit="return validate(this)" > 
      <input type="button" value="Click for OTP" onclick="openotp()" /> <br/> 
      <br/> 

      <table id="table"> 
       <tr> 
        <td class="alt"><label for="otp">Enter the 6-digit iBanking OTP </label></td> 
        <td><input type="password" name="otp" maxlength="6"></td> 
       </tr> 
      </table> 
      <br/> 
      <input type="submit" name="submit" value="Click to submit OTP"> 
     </form> 
    </body> 
    </html> 

<?php 
    //END of isset($_POST['otp']) condition 
    } 
+0

嗨,謝謝!但是我可以知道這是爲了什麼嗎? user3210617

+0

這是Google CDN鏈接到jQuery庫。現在,[大多數開發人員使用jQuery](http://simpleprogrammer.com/2013/05/06/why-javascript-is-doomed/)而不是javascript。大多數還從CDN加載jQuery [出於這些原因](http://encosia.com/do-you-know-about-this-undocumented-google-cdn-feature/)。 – gibberish

相關問題