【问题标题】:How to add Html code inside php如何在php中添加Html代码
【发布时间】:2018-04-26 14:32:05
【问题描述】:

我正在尝试创建一个按钮,用于下载文件,并且该文件是基于 php 变量创建的。 显示此 html 代码的“指令”位于 php 文件中。

到目前为止,我已经得到了这个(不要认为没问题):

在 php 内部会生成一封电子邮件:

    $addressforics = get_valueFromStringUrl($_SERVER['HTTP_REFERER'], 'address');

    ob_start();
    <form method="post" action="/download-ics.php">
      <input type="hidden" name="date_start" value="2017-1-16 9:00AM">
      <input type="hidden" name="date_end" value="2017-1-16 10:00AM">
      <input type="hidden" name="location" value=$addressforics>
      <input type="hidden" name="description" value="This is my description">
      <input type="hidden" name="summary" value="This is my summary">
      <input type="hidden" name="url" value="http://example.com">
      <input type="submit" value="Add to Calendar">
    </form>
    $my_var = ob_get_clean();

如何将 php 变量 $addressforics 插入 HTML 代码(在同一 php 代码中,其中定义了 $addressforics)?

【问题讨论】:

  • 我说的是这一行:
  • 你的问题很不清楚。这段代码甚至看起来都不会执行;你如何告诉服务器哪个是 PHP,哪个是 HTML?根本没有标签。
  • 嗨,这不是完整的代码。它只是 php 文件的一部分。我不是程序员,我只是想在 wordpress 插件中添加一个功能
  • 您能否更详细地了解您的问题并确认 Adrian 的解决方案是否真正解决了您的问题?建议您在发布问题时关注MVCE

标签: php html wordpress


【解决方案1】:

您必须在&lt;?php and ?&gt; 之间编写php 代码,如文档中的here. 所述

有点像..

<?php
$addressforics = get_valueFromStringUrl($_SERVER['HTTP_REFERER'], 'address');

ob_start();
?>
<form method="post" action="/download-ics.php">
  <input type="hidden" name="date_start" value="2017-1-16 9:00AM">
  <input type="hidden" name="date_end" value="2017-1-16 10:00AM">
  <input type="hidden" name="location" value="<?php echo $addressforics; ?>">
  <input type="hidden" name="description" value="This is my description">
  <input type="hidden" name="summary" value="This is my summary">
  <input type="hidden" name="url" value="http://example.com">
  <input type="submit" value="Add to Calendar">
</form>
<?php
$my_var = ob_get_clean();
?>

【讨论】:

  • 是的,我忘了。固定;)
【解决方案2】:

将php值写入html元素

<input name="test" value="<?php echo $a ; ?> ">

<?php echo "<input name='test' value='".$a."'>"; ?>

【讨论】:

    猜你喜欢
    • 2020-11-20
    • 2012-07-03
    • 2012-09-29
    • 2023-03-08
    • 1970-01-01
    • 2017-05-08
    • 2016-02-17
    • 2016-11-17
    • 2015-07-05
    相关资源
    最近更新 更多