POST在同一页面上

时间:2014-01-29 20:01:58

标签: php

我需要发布在同一页面中插入的值。如果成功,我会包含另一个页面,如果不成功,用户必须再次输入表单上的字段。我该怎么做呢?我已设置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> 

5 个答案:

答案 0 :(得分:0)

如果您在site.php填写输入字段,请通过action =“site.php”调用site.php, 也许有一个额外的标志,如action =“site.php?submitted = true”。

请求此标志并检查您的POST变量。如果正确,重定向到下一页,如果没有,请通知用户并再次显示输入字段......

答案 1 :(得分:0)

在您的情况下,您的操作必须是页面本身

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

答案 2 :(得分:0)

为什么你没有使用$_SERVER['PHP_SELF']

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

答案 3 :(得分: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';
        }
    }
?>

答案 4 :(得分: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
    }