在同一页面上以HTML显示PHP脚本

时间:2018-07-26 06:36:49

标签: javascript php html

我对任何前端都有近0的经验和知识,并且我对PHP也有0的经验。但是我想自己研究。

这次,我担心的是,我如何才能使我单击按钮并在同一页面上显示输出?

代码如下:

<?php
    echo "hello world";
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Sample</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <div class="tab">
          <button class="tablinks" onclick="openTab(event, 'Manual')">Manual</button>
        </div>

        <div id="Manual" class="tabcontent">
            <table>
                <tr><td>
                    <h4>Targeted IP/s:</h4>
                    <tbody id="online"></tbody>
            </td></tr>
          </table>
        </div>

        <script>
        // Call PHP code here
        // var res = sample.php;
        $("#online").html(res);
        </script>
    </body>
</html>

我在这里的目标基本上是单击按钮,

<button class="tablinks" onclick="openTab(event, 'Manual')">Manual</button>

并在上述按钮下方显示以上内容。不在新页面或其他页面中。

1 个答案:

答案 0 :(得分:0)

使用jquery,您至少有两种方法可以实现此目的:$.ajax$.get 这两种方法都使用ajax异步加载内容或在后端实现不同的操作。在您的情况下,$.get就足够了。在显示它之前先显示内容。这是实现它的一个好方法。但是,如果您的目标是在加载整个页面之前直接在您的javaScript中编写PHP脚本的内容,那么也可以,但是可以代替 var res=sample.php只会在您的页面中写入sample.php

您必须使用类似

<?php
include('sample.php');
?>

直接在标签中

或者如果sample.php使用return语句返回值

<?php
$res=include('sample.php');
?>
var res=<?php echo $res ?>

并在您的opentab函数中使用

$("#online").html(res);
相关问题