使用javascript在html中生成php [英] use javascript to produce php in html

查看:42
本文介绍了使用javascript在html中生成php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 javascript 在 html 标签内创建 php 脚本.它的想法是,当用户单击按钮时,将触发 php 脚本的类型以生成并确定从数据库访问的内容.

I am trying to create php script inside html tags by using javascript. The idea of it is, that when the user clicks a button, that will trigger the type of php script to produce and determine what is accessed from the database.

我遇到的问题是,每当执行 javascript 时,编写的 php 就会像 <!--?php [code] ?--> 那样注释掉.虽然我故意在 javascript 上写的是这个 <?php [code] ?>

The problem I am having is whenever the javascript is executed, the written php comes out commented out like so <!--?php [code] ?-->. While what I have intentionally on the javascript is this <?php [code] ?>

如您所见,标签已从脚本更改为注释 html 标签.

So as you can see the tags are changed from script to comment html tags.

javascript 代码

function determineDisplay(display_id) {
    var phpO = "<?php echo ";
    var phpF = "read_home('" + display_id + "', '";
    var phpE = "'); ?>";

    var type = [];
    type[0] = "content_desc";
    type[1] = "content_img";
    var imgpath = "images/; ";
    type[2] = "content_comp";

//The alerts are to check what is inserted afterwords.

    document.getElementById(type[0]).innerHTML = phpO + phpF + type[0] + phpE;
alert(document.getElementById(type[0]).innerHTML);
    document.getElementById(type[1]).src = phpO + imgpath + phpF + type[1] + phpE;
alert(document.getElementById(type[1]).src);
    document.getElementById(type[2]).innerHTML = phpO + phpF + type[2] + phpE;
alert(document.getElementById(type[2]).innerHTML);
}

html代码(html有一个默认的php,但是通过使用javascript,它被改变了.

html code(The html has a default php, but by using the javascript, it is changed.

<table width="696px" border="1">
    <tr>
        <td><font size="-1" id="content_desc">
            <?php read_home('a', 'content_desc'); ?>
        </font></td>
        <td>
        <img id="content_img" src='<?php echo "images/"; read_home('a', 'content_img'); ?>' />
        </td>
    </tr>

    <tr><td colspan="2" align="center"><font size="-1" id="content_comp">
        <?php read_home('a', 'content_comp'); ?>
    </font></td></tr>
</table>

我试图用这个来完成的是,根据用户单击的按钮,生成 read_home() php 函数的第一个参数.

What I am trying to accomplish with this is that depending on the button the user clicks, the first perameter of the read_home() php function is produced.

**html buttons using javascript for php**
    <input type="button" id="s" class="support" onclick="determineDisplay('s');"/>
    <br />
    <input type="button" id="a" class="about" onclick="determineDisplay('a');"/>
    <br />
    <input type="button" id="m" class="mission" onclick="determineDisplay('m');" />
    <br />
    <input type="button" id="v" class="vission" onclick="determineDisplay('v');" />
    <br />

推荐答案

解决方案是使用 ajax.当您使用 javascript 时,它的处理是在浏览器端完成的,而您正在尝试基于浏览器端呈现生成 php,这是不可能的,因为 php 是服务器端预处理器.

The solution for this is to use ajax. When you use javascript, it's processing is done on browser side whereas you are trying to produce php based on browser side rendering that's not possible since php is server side PreProcessor.

正如@waldol1 所说,您可以使用 Ajax 并调用异步请求与服务器/数据库进行交互.

As @waldol1 said you can use Ajax and call async requests for interacting with Server/Database.

这篇关于使用javascript在html中生成php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆