所以我有这样的代码php:
$fname="dor";
$fname=$_POST["fname"];
我想将 $fname 从 php 文件传递给 html 文件
在我的 html 文件中,我想在屏幕上打印“dor”
怎么做?
但是当我运行它时,我得到了这个错误:
Undefined index: fname
我应该怎么办?tnx 帮手 :)
您可以包含一个 PHP 文件 -
索引.php:
<?Php
include 'variable.php';
if(!$fname) // if $fname does not exists then create it with no value
$fname = null;
?>
<form action="myphpfile.php" method="POST">
Name: <input type="text" name="fname" value="$fname" />
<input type="submit" />
变量.php:
<?Php
$fname = 'Dor';
?>
myphpfile.php:
<?Php
print $_POST['fname']; // will print the value of fname after submitting the form
?>
您实际上可以使用它。
Name: <input type="text" name="fname" value=<?= $fname ?> />
无法将数据从 PHP 传递到 HTML 文件。
如果您的问题是使用 html 标记显示 php 变量值,您应该创建一个 php 文件并执行以下操作:
<?php
$fname = "test";
?>
<div><?php echo $fname; ?></div>