【问题标题】:PHP Variable Keeps Showing Blank on Second PagePHP变量在第二页上一直显示空白
【发布时间】:2017-08-25 20:38:35
【问题描述】:

我有一个 PHP 页面,它呈现一个 HTML 表单,该表单通过 POST 将数据发送到 PHP Page2,执行一些 MySQL 命令,根据结果,它将$status 设置为一些文本,以便我可以在初始提交时回显它页。不管我做什么,什么都没有。如果我删除顶部的$status='';,它会给出未定义的变量,所以它似乎再也看不到$status,除了开头的空白值。

Index.php(提交页面)

<form method="post" name="sn_upload" id="sn_upload" action="upload.php">
<?php include 'upload.php'; ?>
<div>
   <label for="model">Model: </label><select id="model" name="model" title="Model">
        <option value="Model A">Model A</option>
    </select><br><br>
    <label for="sn">Serial Number: </label><input type="text" id="sn" name="sn" placeholder="Serial Number" pattern="[a-zA-Z0-9]{11,13}"/><br><span class="error"><p id="sn_error" style="color:red;"></p></span><br>
    <?php echo $status; ?> //this shows nothing at all
    <input name="submit" type="submit" value="Submit" />


</div>
</form>

upload.php(MySQL 页面)

<?php
error_reporting(E_ALL);ini_set('display_errors',1);
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "database";
$status = "";
if (isset($_POST['submit'])) {
    // Create connection
    $con = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($con->connect_error) {
        die("Connection failed: " . $con->connect_error);
    } 

    $mod = mysqli_real_escape_string($con,$_POST['model']);
    $sn_num = mysqli_real_escape_string($con,$_POST['sn']);
    $check = $con->query("SELECT * FROM rma_product WHERE pro_sn = ('$sn_num') ");

    if ($check->num_rows == 0)
    {
        $sql = "INSERT INTO rma_product (m_type,pro_sn) VALUES ('$mod','$sn_num')";
    if ($con->query($sql) === TRUE) {
        $status = "Success";
        header('Location: index.php');
    } else {
        $status = "Failed";
        header('Location: index.php');
    }
}
else if ($check->num_rows >= 1)
    {
        $status = "Exists";

        header('Location: index.php');
    }

$con->close();
}
return $status;
?>

【问题讨论】:

  • 您在上面发布的“第 2 页”是否命名为 upload.php?如果是这样,您将表单发布到该页面将该页面包含在“第 1 页”中?
  • 您要发布到page2,然后在page1上显示从page2生成的变量?
  • 也很确定你不应该用括号将你的变量包装在你的 SELECT 语句中,我以前从未见过那个标记。
  • 加上HTML选择标签不应该是自动关闭的(即&lt;select id="model" name="model" title="Model" /&gt;然后有相应的关闭标签:&lt;/select&gt;....)
  • 仅仅因为它看起来有效并不意味着它是正确的和/或将永远有效。

标签: php redirect http-post forms


【解决方案1】:

在每种情况下,只要将非空字符串文字分配给 $status(即尝试插入记录或报告关联记录已存在时),就会调用 header()。这将告诉浏览器导航到另一个页面,基本上结束当前页面的执行。因此,在此之后,存储在$status 中的值不会持续存在。

        $status = "Success";
        header('Location: index.php');
    } else {
        $status = "Failed";
        header('Location: index.php');
    }
}
else if ($check->num_rows >= 1)
{
    $status = "Exists";
    header('Location: index.php');

因此,您要么需要弄清楚如何在不重定向的情况下设置该值,要么将值发送到重定向到的页面。选项包括将值附加到查询字符串(例如header('Location: index.php?status='.$status); 并检查$_GET['status'] 中的值以分配给index.php 中的$status),使用会话(如Nosajimiki 所述)等。

【讨论】:

  • 如果需要添加另一个 sn,我将重定向返回到提交页面
  • 是的,考虑到upload.php 中的代码,这是有道理的。我更新了解释以尝试更明确。
  • 这很有效,所以从帖子重定向后的 URL 显示 index.php?status=Exists 这很好,但仍然没有在表单页面上回显任何内容
  • 然后让 index.php 检查查询字符串:$status = isset($_GET['status'])?$_GET['status']:'';...
  • 我能够与if (strpos($_SERVER['REQUEST_URI'], "Exists") !== false){ echo "Found"; }一起工作,谢谢大家的帮助。
【解决方案2】:

您似乎正在尝试将 $status 用作会话变量。如果要在页面之间保留 PHP 变量,请创建会话并将其保存为会话变量

<?php
// Start the session
session_start();
...
$_SESSION["status"] = $status;
header('Location: index.php');
?>

然后您可以使用 $_SESSION["status"] 在其他后续页面上调用它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2021-07-18
    • 2014-01-15
    • 2017-05-23
    • 2015-01-20
    相关资源
    最近更新 更多