【问题标题】:Pass variable from PHP to HTML and from HTML to another PHP file将变量从 PHP 传递到 HTML 并从 HTML 传递到另一个 PHP 文件
【发布时间】:2015-03-24 18:58:44
【问题描述】:

所以我有一个小问题。从 GetFbId.php 我得到 Id 变量到 CheckFbId.php 跟随 session_start(); $id = $_SESSION['id']; 。检查成功后(数据库中没有用户)它重定向到RegForm.html,如下:header('Location: RegForm.html');我需要以某种方式将Id变量传递给RegForm.html并从这里(注册完成后)它应该传递所有数据+ Id变量为RegInsert.php

总结 RegInsert.php 成功将数据插入数据库,除了 FB 用户 ID 应取自: GetFbId.php > CheckFbId.php > RegForm.html > RegInsert.php.

我的问题是我无法成功地将该变量从 php 传递到 html 以及从 html 传递到 php。有什么帮助吗?

CheckFbId.php 看起来像:

<?php 
session_start();
$id = $_SESSION['id']; 
$mysqli = new mysqli("localhost","xxx","xxxx","xxxx");

$query = mysqli_query($mysqli, "SELECT * FROM users WHERE FB_Id = '".$id."'");

if (!$mysqli->set_charset("utf8")) 
{
    printf("Error loading character set utf8: %s\n", $mysqli->error);
}     
if ($query->num_rows) {
    echo "Already  registered";
} 
else {
    $_SESSION['fb_id'] = $id; 
    header("Location: registration.html?id='$id'");
}    
$mysqli->close();
?>

RegForm.html 看起来像:

      <form class="form"  method="post" action="register.php">
        <h2>Registracijos forma</h2><hr/>
        <label>First Name: </label>
        <input type="text" name="first_name" id="first_name">

        <label>Last Name: </label>
        <input type="text" name="last_name" id="last_name">

        <label>City: </label>
        <input type="text" name="city" id="city">

        <label>Email: </label>
        <input type="text" name="email" id="email">

        <input type="submit" name="register" id="register" value="Register">
      </form>   

RegInsert.php 看起来像:

<?php
$first_name = $_POST['first_name']; 
$last_name = $_POST['last_name']; 
$city = $_POST['city']; 
$email = $_POST['email']; 
$fb_id = $_POST['id'];

$mysqli = new mysqli("localhost","xxxx","xxxx","xxxx");


    if ($stmt = $mysqli->prepare("INSERT INTO users (FB_Id, First_Name, Last_Name, City, Email) VALUE (?,?,?,?,?) ")) 
    {
        if (!$mysqli->set_charset("utf8")) 
        {
            printf("Error loading character set utf8: %s\n", $mysqli->error);
        } 
        $stmt->bind_param('sssss', $fb_id, $first_name, $last_name, $city, $email);
        $stmt->execute();
        if ($stmt->error != '') 
        {
           echo ' error:'.$stmt->error;
        } else 
        {
           echo 'success';
        }
        $stmt->close();
    } else 
    {
       echo 'error:'.$mysqli->error;
    }
$mysqli->close();
?>

【问题讨论】:

  • 将其保留在会话中,例如$_SESSION['checked_id']。或者将您的 html 表单页面更改为 php 文件。
  • 我真的不知道如何在 HTML 中使用它并将它发送到另一个 PHP。
  • 所以不要,保留在会话中。
  • 我不完全确定您想对 .html 文件做什么。您想将会话数组显示/传递给.html 文件吗?如果是这样;你不能。好吧,您可以,但这意味着指示 Apache 将 .html 文件视为 PHP。或者,使用 Ajax。
  • 请注意,如果您将其传递给您的 html / php 表单文件,则无论如何您都必须在RegInsert.php 中再次检查它,因为您无法信任用户输入;用户可以在表单页面中更改它。

标签: php html variables session post


【解决方案1】:

你可以像下面这样传递 id 变量

header("Location: RegForm.html?id='$id'");

并且可以在 RegForm.html 页面中的隐藏输入中获取此变量,并且可以在任何页面中传递(您可以将 RegForm.html 更改为 RegForm.php )

【讨论】:

  • 不是很安全,你必须依赖像javascript这样的客户端技术来解析它,因为你不能在html文件中使用php。
  • 我只是问他是否可以改变而不是改变。
  • 在你的情况下,CheckFbId.php 我用过:header("Location: RegForm.html?id='$id'"); html 形式我用过:&lt;input type="hidden" name="id" id="id"&gt;RegInsert.php 我用过$fb_id = $_POST['id']; 并插入FB用户 ID = 0
  • 我也尝试在RegInsert.php 中使用$fb_id = $_SESSION['id'];,但在这种情况下FB User Id = NULL
  • 您是否在 GetFbId.php 中设置了 $_SESSION['id']?如果你不是,当然 id 变量将为空。我问是因为您没有发布该页面的代码。
猜你喜欢
  • 1970-01-01
  • 2011-07-14
  • 2014-02-03
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-11
相关资源
最近更新 更多