簡體   English   中英

將php變量數據從一頁傳遞到另一頁

[英]Passing php variable data from one page to another

我已經將值從說a.php的頁面傳遞給b.php。 該值已保存在變量中。 我試圖將該值傳遞到另一個頁面,例如c.php以保存在db中,但是傳遞的值為NULL。

我試圖使用會話,cookie和其他表單傳遞方法。 A.php還具有其他值和有效的字段。 我只需要解決這個問題。 謝謝。

a.php只會

<form action="b.php" method="post">
<input type="text" name="varname">
<input type="submit">
//then there is rest of the other fields and codes on the page

b.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "edg_dsh";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$a_num = $_POST['varname']; //value sent by a.php
?>
<html>
<head></head>
<body>
<p> <?php echo $a_num ?> This is the value a has passed </p>

<form action="c.php" method="post">
<input type="text" name="b_var" value="<?php $a_num ?>" placeholder="<?php echo $a_num ?>" readonly/>
<input type="text" name="name"/>
<input type="submit">
</form>
</body>
</html>

c.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "edg_dsh";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$b_var=$_POST["b_var"];
$name=$_POST["name"];

if($b_var==NULL)
{
    echo "Field is empty";
}
else{
$sql = "INSERT INTO testing (var, name) VALUES ('$b_var', '$name')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}

$conn->close();

?>

我需要$ b_var具有從a.php傳遞的值,而不是null。

在b.php中

替換此行

<input type="text" name="b_var" value="<?php $a_num ?> placeholder="<?php echo $a_num ?> readonly>

<input type="text" name="b_var" value="<?php echo $a_num; ?>" placeholder="<?php echo $a_num; ?>" readonly>

並嘗試。

您錯過了該文本框值中的回聲

嘗試這個

<input type="text" name="b_var" value="<?php echo $a_num ?>" placeholder="<?php echo $a_num ?>" readonly>

您的HTML <input標簽有問題:

1)您尚未在value echo

2)您尚未關閉valueplaceholder屬性。

3)另外,建議:HTML <input標簽是自動關閉的,因此請在末尾放置/>而不是>

更正的HTML:

<input type="text" name="b_var" value="<?php echo $a_num;?>" placeholder="<?php echo $a_num ?>: readonly/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM