【问题标题】:I have to refresh my php page after form submission to execute the code and display output提交表单后我必须刷新我的 php 页面才能执行代码并显示输出
【发布时间】:2018-07-07 07:58:46
【问题描述】:

表单提交代码

<form action="search.php" method="POST" id="form2">
  <input type="text" name="Sr_no" required="true" placeholder="Enter Serial Number"><br>
  <input type="submit" value="Check Status" name="ss">
</form>

我的搜索.php

<?php

    $connect=mysqli_connect("localhost","root", "" ,"mydatabase");
    $Sr_no = $_POST["Sr_no"];

    $status_check = "SELECT verified FROM r_concession_forms WHERE id = '$Sr_id' LIMIT 1";
    $s_check = $connect->query($status_check);
    $s_value = $row = $s_check->fetch_assoc();

    if($s_value["verified"] == "0")
    {
        echo "<script>
            alert('Not Issued/Verified');
            window.location.href='index.php';
        </script>" ;
    }
    elseif($s_value["verified"] == "1"){
        echo "<script>
            alert('Issued and Verified');
            window.location.href='index.php';
            </script>";
    }
    else{
        echo "<script>
            alert('No Such Form Exists');
            window.location.href='index.php';
        </script>";
    }
?>

输出: 我的输入首先检查数据库中是否存在“id”

没有如预期的警告框,但URL显示search.php,但没有显示警告框

刷新页面后,提示框开始工作

页面的设计是通过JavaScript实现的

【问题讨论】:

    标签: javascript php html css forms


    【解决方案1】:

    我认为您在查询中使用了之前未定义的变量 $Sr_id。还要先检查表格是否已发布。

    刷新页面时代码工作正常,因为刷新页面时没有发布数据,因此查询返回空值并显示“不存在此类表单”。但是当你发布页面时,我猜你在查询中给出的发布值是错误的。

    <?php
    if(!empty($_POST['ss'])) {
        $connect=mysqli_connect("localhost","root", "" ,"mydatabase");
        if(!empty($_POST['Sr_no'])) {
            $Sr_no = $_POST["Sr_no"];
    
            $status_check = 'select verified FROM r_concession_forms WHERE id = '.$Sr_no.' LIMIT 1';
            $s_check = $connect->query($status_check);
            $s_value = $row = $s_check->fetch_assoc();
    
            if($s_value["verified"] == "0")
            {
                echo "<script>
                    alert('Not Issued/Verified');
                    window.location.href='index.php';
                </script>" ;
            }
            elseif($s_value["verified"] == "1"){
                echo "<script>
                    alert('Issued and Verified');
                    window.location.href='index.php';
                    </script>";
            }
            else{
                echo "<script>
                    alert('No Such Form Exists');
                    window.location.href='index.php';
                </script>";
            }
        }
    }
    ?>
    

    另外,我个人不喜欢在 PHP 之间使用脚本代码,正如您所提到的。尝试将代码分开以便更好地理解。

    【讨论】:

    • 是的,感谢您的意见,我会记住的。我只是指出了他问题的原因。我没有修改他的代码中的任何其他内容。正如你所说,我现在会更新回复。
    猜你喜欢
    • 2023-02-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多