HTML调用PHP的方法

html本身是无法处理动态请求, 要完成这个,一般是用javascript。在 生成静态网页,可以根据数据库id给html页面生成一个相对应的javascript文件引用。 比如页面是123.html, 那就在这个页面生成一个 
<script type="text/javascript" src="click.php?id=123"></script> 

然后在click.php这个页面就按照php的语法去处理操作数据库就行。

==============================================

 

静态页面中是不能直接调用php文件,但是有可以使用js调用方式来调用php文件,还可以使用ajax 调用php文件。
举一个简单的例子来说明:在页面a.html中使用如下代码,可以将action=test的参数传递到b.php。
Javascript代码如下:
<script type="text/javascript" src="b.php?action=test"></script> 
在b.php中有这样一段PHP代码:
<?php
	$action=$_GET['action'];     
	echo "document.write('".$action."');n";    
?>
========================================
已标记关键词 清除标记
<div class="post-text" itemprop="text"> <p>I am playing around with GPIO pins on a raspberry pi and have been trying to teach myself how to get them to function via a webpage, I am familiar with python, CSS and HTML but not so much with javascript so I am struggling with this code.</p> <p>I followed a tutorial on youtube where I built a basic webpage with 2 buttons that turns on and off an LED on a breadboard.</p> <p>As I have the code written there, the code works fine, the LED's turn on and off.</p> <p>I am trying to add more buttons to the page but I am having trouble accessing the javascript so the LED will turn on. The functioning web page is as follows.</p> <pre><code> <!DOCTYPE html> <html> <head> <style> </style> <meta charset="UTF-8"> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('#clickOn').click(function(){ var a = new XMLHttpRequest(); a.open("GET","pinOn.php"); a.onreadystatechange=function(){ if(a.readyState==4){ if(a.status == 200){ } else alert("HTTP ERROR!"); } } a.send(); }); $('#clickOff').click(function(){ var a = new XMLHttpRequest(); a.open("GET","pinOff.php"); a.onreadystatechange=function(){ if(a.readyState==4){ if(a.status == 200){ } else alert("HTTP ERROR!"); } } a.send(); }); }); </script> <title>Pi GPIO Controller</title> </head> <body> <h1><center>Turn the light on and off!</center></h1></br></br> <div class="button"> <center><button type="button" id="clickOn">ON</center></button></br> <center><button type="button" id="clickOff">OFF</center></button></br> </div> </body> </html> </code></pre> <p>This is the pinOff.php file that sets pin 4 to high.</p> <pre><code><?php system("gpio -g mode 4 out"); system("gpio -g write 4 1"); ?> </code></pre> <p>And the pinOn.php to set it to pin low.</p> <pre><code><?php system("gpio -g mode 4 out"); system("gpio -g write 4 0"); ?> </code></pre> <p>I am trying to add another button and have tried simply adding:</p> <pre><code><center><button type="button" id="clickOff">OFF</center></button></br> </code></pre> <p>But it isn't working.</p> <p>How can I call the javascript in a button to get the code in the pinOn and pinOff files to work.</p> </div>
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页