使用php从SQL DB获取值并在HTML5中使用它

时间:2019-01-30 21:31:36

标签: php mysql sql html5

我有一个HTML5文件,分别是一个简单的网页。

,我有一个php文件。 该文件包含一个函数getKursname(string) 在php文件中调用该函数时,该函数正常运行。 这一切都发生在XAMPP上。

我现在想更新html页面中的一些值和字符串。 这应该只在负载下发生。

这是html中的代码:

<div class="w3-bar" id="myNavbar">
    <a class="w3-bar-item w3-button w3-hover-black w3-hide-medium w3-hide-large w3-right" href="javascript:void(0);" onclick="toggleFunction()" title="Toggle Navigation Menu">
      <i class="fa fa-bars"></i>
    </a>
    <a href="#home" class="w3-bar-item w3-button"><i class="myfont icon-heart"></i> HOME</a>
    <a href="#about" class="w3-bar-item w3-button w3-hide-small"><i class="myfont icon-users"></i> &UumlBER UNS</a>
    <a href="#portfolio" class="w3-bar-item w3-button w3-hide-small"><i class="myfont icon-th"></i> KURSE</a>
    <a href="#contact" class="w3-bar-item w3-button w3-hide-small"><i class="myfont icon-mail-alt"></i> KONTAKT</a>

    </a>
  </div>

我想要有innerhtml(例如KONTAKT) 从数据库中读取。

php函数返回一个字符串。

我可以在php中使用getKursname(string)调用该函数。

我的想法是:

<a href="#contact" class="w3-bar-item w3-button w3-hide-small"><i class="myfont icon-mail-alt"></i>getKursname("kurs1")</a>

但这不起作用...:-(

先谢谢 德克

1 个答案:

答案 0 :(得分:0)

假设您具有类似于以下的目录结构:

www/
  index.html
  functions.php

,您想在将index.html发送到浏览器之前注入内容。

在这种情况下,解决方案是将index.html重命名为index.php,然后在需要函数输出的位置编写

<?php include_once 'functions.php'; echo getKursname("kurs1"); ?>

(或require_once而不是include_once,并且您只需在使用之前包含一次,即可加载函数定义)

但是,标准是将标头中包含:

index.php

<?php 
     require_once 'functions.php';
?><html>
  <head><!-- ... --></head>
  <body><!-- ... other stuff -->
     <?=getKursname("kurs1");?>
     <!-- rest of page -->
  </body></html>

澄清一下:<?=[something] ?><?php echo [something] ?>的缩写,并且<?php和下一个?>之间的所有内容都是由php评估的,其他所有内容(例如html代码)都只是按原样打印。