php 在html页面中调用PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1794931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:52:06  来源:igfitidea点击:

Calling PHP in html page

phphtml

提问by

Let say, i can only use html codes in a page ( call it index.html ).

比方说,我只能在页面中使用 html 代码(称为 index.html )。

So now, i want to check something using php ( call it userCheck.php ).

所以现在,我想使用 php 检查一些东西(称之为 userCheck.php )。

So it is possible i want the html to communicate with php files.

所以有可能我希望 html 与 php 文件进行通信。

For example, something like

例如,像

when users access index.html, it will check the userCheck.php, so userCheck says yes, then can proceed view it or else it will redirect to userCheck.php.

当用户访问 index.html 时,它会检查 userCheck.php,所以 userCheck 说是,然后可以继续查看它,否则它会重定向到 userCheck.php。

Is it possible something like using javascript or ajax? I'm newbie on those two.

是否有可能使用 javascript 或 ajax?我是这两个的新手。

Thank you

谢谢

采纳答案by miku

You can use javascript to fetch some data into your html from your php.

您可以使用 javascript 从您的 php 中获取一些数据到您的 html 中。

Example using jQuery(put this in your html):

使用jQuery 的例子(把它放在你的 html 中):

<script type="text/javascript" charset="utf-8"> <!-- javascript in html -->
    $(document).ready(function(){ // start here, if DOM is loaded

    // request data from php script ...
    // we expect the userCheck.php script to actually 'return' something, 
    // some data ...
    $.get("userCheck.php", function(data){
            alert("Data Loaded: " + data);
    });

    // or with some params to the php
    // e.g. userCheck.php can handle username and favcolor parameters
    $.get("userCheck.php", 
            {"username" : "lazy", "favcolor" : "FFFFFF" },          
            function(data){ alert("Data Loaded: " + data);
    });



});
</script>

If this is all completely new to you, please do yourself a favor and read some introductional books, which will give you valueable insights - some random book tips from O'Reilly, Google Tech Talk on jQuery

如果这对您来说是全新的,请帮自己一个忙并阅读一些介绍性书籍,它们会给您带来有价值的见解 -来自 O'Reilly 的一些随机书籍提示Google Tech Talk on jQuery

回答by Tim Post

You want to set up an Apache Handler so that files with a .html extension are parsed with php. Do this via a simple .htaccessfile located in your web root:

您想设置一个 Apache 处理程序,以便使用 php 解析扩展名为 .html 的文件。通过.htaccess位于 Web 根目录中的简单文件执行此操作:

AddHandler application/x-httpd-php .html

You can now use PHP inside of the .html pages. No need for additional AJAX hackery, or dealing with NoScript users or JS feeble browsers.

您现在可以在 .html 页面中使用 PHP。不需要额外的 AJAX 技巧,也不需要处理 NoScript 用户或 JS 弱浏览器。

回答by Krzysztof Bujniewicz

If your server doesn't allow php files, then you cannot do it directly. You may want to place your php file on another server, supporting php, and call it with ajax, though. But you must make sure that your php server provider allows that.

如果您的服务器不允许 php 文件,那么您不能直接这样做。不过,您可能希望将 php 文件放在另一台支持 php 的服务器上,并使用 ajax 调用它。但是您必须确保您的 php 服务器提供商允许这样做。

回答by Zoe

JavaScript can be used to call and communicate with a HTML page via JSON.

JavaScript 可用于通过 JSON 调用 HTML 页面并与之通信。

Here's an example using JQuery and the select-chain plug-in to populate a drop-down box based on the selection of a previous drop-down..

下面是一个使用 JQuery 和 select-chain 插件根据上一个下拉列表的选择来填充下拉框的示例。

HTML page:

HTML页面:

$('#selectbox1').selectChain({
        target: $('#selectbox2'),
        url: 'selectlist.php', 
        data: { ajax: true }
    }).trigger('change');

selectlist.php

选择列表.php

if (@$_REQUEST['ajax']) {
$json = array();
    if ((isset($_REQUEST['selectbox1'])) && ($_REQUEST['selectbox1'] != '')){   
        $results = mysql_query('SELECT * FROM table WHERE id="'.$_REQUEST['selectbox1'].'"') or die ("Error: ".mysql_error());
        while($row = mysql_fetch_array($results)) {
            $json[] = '{"id" : "' . $row['id'] . '", "label" : "' . $row['name'] . '"}';
        }
    }
    echo '[' . implode(',', $json) . ']';
}

回答by carillonator

Not sure exactly what you're after, but can you just name your page index.php, write all your html in it, and where needed, insert your PHP code as <?php ...code... ?>? The PHP runs server-side and can conditionally present different HTML depending on whatever your factors are.

不确定您到底在追求什么,但是您可以将您的页面命名为 index.php,在其中写入所有 html,并在需要的地方插入您的 PHP 代码<?php ...code... ?>吗?PHP 在服务器端运行,并且可以根据您的因素有条件地呈现不同的 HTML。

回答by lo_fye

UFOman said "server allows php but because the program i used can only output html pages" It sounds to me like what you really need is to understand how to combine html pages with php code. The simplest (yet proper) way to do what you want is like this:

UFOman 说“服务器允许使用 php,但因为我使用的程序只能输出 html 页面”在我看来,您真正需要的是了解如何将 html 页面与 php 代码结合起来。做你想做的最简单(但正确)的方法是这样的:

1) Using Notepad or TextEdit, open the HTML file your program output
2) Select All and Copy the contents
3) Create a file called yourPageNameTemplate.php and Paste into it. Pretend it looks like this:

1) 使用记事本或 TextEdit,打开程序输出的 HTML 文件
2) 全选并复制内容
3) 创建一个名为 yourPageNameTemplate.php 的文件并将其粘贴到其中。假设它看起来像这样:

<html> 
<body>
   <form method="post">
      <input type="text" name="user_name" value="" />
      <input type="submit" name="submit" value="submit" />
   </form>
</body>
</html>

4) Create a file called yourPageName.php and put something like this in it:

4) 创建一个名为 yourPageName.php 的文件,并在其中放入如下内容:

<?php 

if(isset($_POST['submit']) && isset($_POST['user_name']))
{
   echo 'Thanks for submitting the form!';
}
else
{
   include 'yourPageNameTemplate.php';
}

?>

That should work just fine. Now, if you want to add some error checking, an an error message to your Template, we can do that quite simply.

那应该工作得很好。现在,如果你想添加一些错误检查,一个错误消息到你的模板,我们可以很简单地做到这一点。

5) Edit yourPageNameTemplate.php like this:

5) 像这样编辑 yourPageNameTemplate.php:

<html> 
<body>
   <?=$form_error_message?>
   <form method="post">
      <input type="text" name="user_name" value="<?=$form_user_name?>" />
      <input type="submit" name="submit" value="submit" />
   </form>
</body>
</html>

6) Edit yourPageName.php like this:

6) 像这样编辑 yourPageName.php:

<?php 

$form_error_message = '';
$form_user_name = '';
if(isset($_POST['user_name']) && strlen($_POST['user_name']) < 5)
{
   $form_error_message = 'User Name must be at least 5 characters long';
   $form_user_name = $_POST['user_name'];
}

if(isset($_POST['submit']) && empty($form_error_message))
{
   echo 'Thanks for submitting the form!';
   //it would be better to include a full 'thank you' template here, instead of echoing.
}
else
{
   include 'yourPageNameTemplate.php';
}

?>