【问题标题】:pass variables from php file to html file将变量从 php 文件传递​​到 html 文件
【发布时间】:2014-02-03 15:26:43
【问题描述】:

所以我有这样的代码 php:

$fname="dor";
$fname=$_POST["fname"];

我想将 $fname 从 php 文件传递​​到 html 文件

在我的 html 文件中,我想在屏幕上打印“dor”

怎么做?

但是当我运行它时,我得到了这个错误:

Undefined index: fname

我该怎么办? tnx 帮手 :)

【问题讨论】:

  • 从 PHP 到 HTML 文件?这不可能。除非你说反了。
  • $fname=$_POST["fname"]; 你是从form 获得fname

标签: php html pass-data


【解决方案1】:

你可以包含一个 PHP 文件 -

index.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
?>

【讨论】:

  • 你需要&lt;?php echo $fname ?&gt;
【解决方案2】:

你可以实际使用它。

Name: <input type="text" name="fname" value=<?= $fname ?> />

【讨论】:

  • 我的问题有误,你能再看看吗? – Dor Cohen 刚刚编辑
  • var_dump($_POST) 的输出是什么?
【解决方案3】:

无法将数据从 PHP 传递到 HTML 文件。

如果您的问题是使用 html 标记显示 php 变量值,您应该创建一个 php 文件并执行以下操作:

<?php
$fname = "test";
?>
<div><?php echo $fname; ?></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 2016-10-14
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多