Javascript 如何使用多个按钮通过PHP运行多个函数
我设法让我的上一个项目工作,我点击网页上的一个按钮,它在网页上对所有正在查看它的设备进行一些文本更改,通过点击按钮运行PHP代码,告诉所有设备运行JavaScript函数,该函数读取PHP代码正在更改的文本文件的值 现在我想做同样的事情,但我想有3个不同的按钮,在所有查看网页的设备上运行3个不同的JavaScript函数 我试过制作3个不同的PHP文件、3个不同的文本文件和3个不同的函数,但都不起作用(它似乎认为我在点击按钮,而我没有),而且似乎不是一种非常有效的方法。我认为这与同时运行多个$.get()函数有关,用于所有不同的函数 我如何拥有1个PHP文件和1$.get()函数并使其工作 谢谢, Fjpackard 编辑: 这是我尝试过的代码,不过如果可能的话,我希望我能用一种不同的、更有效的方式来完成 index.html:Javascript 如何使用多个按钮通过PHP运行多个函数,javascript,php,jquery,html,function,Javascript,Php,Jquery,Html,Function,我设法让我的上一个项目工作,我点击网页上的一个按钮,它在网页上对所有正在查看它的设备进行一些文本更改,通过点击按钮运行PHP代码,告诉所有设备运行JavaScript函数,该函数读取PHP代码正在更改的文本文件的值 现在我想做同样的事情,但我想有3个不同的按钮,在所有查看网页的设备上运行3个不同的JavaScript函数 我试过制作3个不同的PHP文件、3个不同的文本文件和3个不同的函数,但都不起作用(它似乎认为我在点击按钮,而我没有),而且似乎不是一种非常有效的方法。我认为这与同时运行多个$.
<title>PHP Test</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
var theButton;
function myClick()
{
$.post('script.php', {});
}
function myClick2()
{
$.post('script2.php', {});
}
function myClick3()
{
$.post('script3.php', {});
}
function myFunc() {
//The code you want to run on the clients
$("#theDiv").text("Say Hello");
setTimeout(function(){
$("#theDiv").text("");
},2000);
}
function myFunc2() {
//The code you want to run on the clients
$("#theDiv").text("Say Goodbye");
setTimeout(function(){
$("#theDiv").text("");
},2000);
}
function myFunc3() {
//The code you want to run on the clients
$("#theDiv").text("Zip Your Mouth");
setTimeout(function(){
$("#theDiv").text("");
},2000);
}
var lastValue = '';
var lastValue2 = '';
var lastValue3 = '';
$("document").ready(function() {
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
if (agentID) {
// mobile code here
$("#btn1").remove();
$("#btn2").remove();
$("#btn3").remove();
setInterval(function() {
$.get(
'script.php',
{},
function (data) {
if (data != lastValue) {
myFunc();
lastValue = data;
}
}
);
/*$.get(
'script2.php',
{},
function (data2) {
if (data2 != lastValue2) {
myFunc2();
lastValue = data2;
}
}
);
$.get(
'script3.php',
{},
function (data3) {
if (data3 != lastValue3) {
myFunc3();
lastValue = data3;
}
}
);*/
},100);
}
else {
$("#theDiv").remove();
}
});
</script>
<div id="theDiv"></div>
<button id="btn1" class="btn" onclick="myClick()">Say Hello</button><br>
<button id="btn2" class="btn" onclick="myClick2()">Say Goodbye</button><br>
<button id="btn3" class="btn" onclick="myClick3()">Zip Your Mouth</button>
<?php
// Disable cache
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header("Pragma: no-cache");
$file = 'file.txt';
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// POST request
$previous = file_get_contents($file);
if ($previous === 'true')
{
file_put_contents($file, 'false');
}
else
{
file_put_contents($file, 'true');
}
}
else
{
// not POST request, we assume it's GET
echo file_get_contents($file);
}
exit();
?>
<?php
// Disable cache
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header("Pragma: no-cache");
$file = 'file2.txt';
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// POST request
$previous = file_get_contents($file);
if ($previous === 'true')
{
file_put_contents($file, 'false');
}
else
{
file_put_contents($file, 'true');
}
}
else
{
// not POST request, we assume it's GET
echo file_get_contents($file);
}
exit();
?>
<?php
// Disable cache
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header("Pragma: no-cache");
$file = 'file3.txt';
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// POST request
$previous = file_get_contents($file);
if ($previous === 'true')
{
file_put_contents($file, 'false');
}
else
{
file_put_contents($file, 'true');
}
}
else
{
// not POST request, we assume it's GET
echo file_get_contents($file);
}
exit();
?>
PHP测试
按下按钮;
函数myClick()
{
$.post('script.php',{});
}
函数myClick2()
{
$.post('script2.php',{});
}
函数myClick3()
{
$.post('script3.php',{});
}
函数myFunc(){
//要在客户端上运行的代码
$(“#theDiv”)。文本(“打招呼”);
setTimeout(函数(){
$(“#theDiv”)。正文(“”);
},2000);
}
函数myFunc2(){
//要在客户端上运行的代码
$(“theDiv”)。文本(“说再见”);
setTimeout(函数(){
$(“#theDiv”)。正文(“”);
},2000);
}
函数myFunc3(){
//要在客户端上运行的代码
$(“#theDiv”).text(“闭嘴”);
setTimeout(函数(){
$(“#theDiv”)。正文(“”);
},2000);
}
var lastValue='';
var lastValue2='';
var lastValue3='';
$(“文档”).ready(函数(){
var deviceAgent=navigator.userAgent.toLowerCase();
var agentID=deviceAgent.match(/(iphone | ipod | ipad)/);
国际单项体育联合会(agentID){
//这里是手机代码
$(“#btn1”).remove();
$(“#btn2”).remove();
$(“#btn3”).remove();
setInterval(函数(){
美元(
“script.php”,
{},
功能(数据){
如果(数据!=lastValue){
myFunc();
lastValue=数据;
}
}
);
/*美元(
“script2.php”,
{},
功能(数据2){
如果(数据2!=lastValue2){
myFunc2();
lastValue=data2;
}
}
);
美元(
“script3.php”,
{},
功能(数据3){
如果(数据3!=lastValue3){
myFunc3();
lastValue=data3;
}
}
);*/
},100);
}
否则{
$(“#theDiv”).remove();
}
});
打招呼
说再见
闭嘴
script.php:
<title>PHP Test</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
var theButton;
function myClick()
{
$.post('script.php', {});
}
function myClick2()
{
$.post('script2.php', {});
}
function myClick3()
{
$.post('script3.php', {});
}
function myFunc() {
//The code you want to run on the clients
$("#theDiv").text("Say Hello");
setTimeout(function(){
$("#theDiv").text("");
},2000);
}
function myFunc2() {
//The code you want to run on the clients
$("#theDiv").text("Say Goodbye");
setTimeout(function(){
$("#theDiv").text("");
},2000);
}
function myFunc3() {
//The code you want to run on the clients
$("#theDiv").text("Zip Your Mouth");
setTimeout(function(){
$("#theDiv").text("");
},2000);
}
var lastValue = '';
var lastValue2 = '';
var lastValue3 = '';
$("document").ready(function() {
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
if (agentID) {
// mobile code here
$("#btn1").remove();
$("#btn2").remove();
$("#btn3").remove();
setInterval(function() {
$.get(
'script.php',
{},
function (data) {
if (data != lastValue) {
myFunc();
lastValue = data;
}
}
);
/*$.get(
'script2.php',
{},
function (data2) {
if (data2 != lastValue2) {
myFunc2();
lastValue = data2;
}
}
);
$.get(
'script3.php',
{},
function (data3) {
if (data3 != lastValue3) {
myFunc3();
lastValue = data3;
}
}
);*/
},100);
}
else {
$("#theDiv").remove();
}
});
</script>
<div id="theDiv"></div>
<button id="btn1" class="btn" onclick="myClick()">Say Hello</button><br>
<button id="btn2" class="btn" onclick="myClick2()">Say Goodbye</button><br>
<button id="btn3" class="btn" onclick="myClick3()">Zip Your Mouth</button>
<?php
// Disable cache
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header("Pragma: no-cache");
$file = 'file.txt';
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// POST request
$previous = file_get_contents($file);
if ($previous === 'true')
{
file_put_contents($file, 'false');
}
else
{
file_put_contents($file, 'true');
}
}
else
{
// not POST request, we assume it's GET
echo file_get_contents($file);
}
exit();
?>
<?php
// Disable cache
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header("Pragma: no-cache");
$file = 'file2.txt';
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// POST request
$previous = file_get_contents($file);
if ($previous === 'true')
{
file_put_contents($file, 'false');
}
else
{
file_put_contents($file, 'true');
}
}
else
{
// not POST request, we assume it's GET
echo file_get_contents($file);
}
exit();
?>
<?php
// Disable cache
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header("Pragma: no-cache");
$file = 'file3.txt';
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
// POST request
$previous = file_get_contents($file);
if ($previous === 'true')
{
file_put_contents($file, 'false');
}
else
{
file_put_contents($file, 'true');
}
}
else
{
// not POST request, we assume it's GET
echo file_get_contents($file);
}
exit();
?>
您可以在php文件中使用一些GET
参数和数组
:
JS代码:
function myClick()
{
$.post('script.php?button=1', {});
}
function myClick2()
{
$.post('script.php?button=2', {});
}
function myClick3()
{
$.post('script.php?button=3', {});
}
然后在script.php
中:
<?php
session_start();
// Disable cache
header("Expires: Thu, 01 Jan 1970 00:00:00 GMT");
header('Cache-Control: max-age=0, no-cache, no-store, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header("Pragma: no-cache");
$fileArray = array("1" => "file.txt", "2" => 'file2.txt', "3" => 'file3.txt');
if(isset($_SESSION['lastFile']) && !isset($_GET['button'])){
$file = $_SESSION['lastFile'];
}else if(isset($_GET['button'])){
$file = $fileArray[$_GET['button']];
}else{
$file = "file.txt";
}
$_SESSION['lastFile'] = $file;
请向我们展示您尝试使用的代码,或者至少尽您所能。。。然后我们可以帮助填补缺少的部分。使三个按钮对三个php函数进行三次ajax调用我想我已经。。。我希望它是一个PHP函数,如果可能的话,将根据我点击的按钮做不同的事情。需要学习开始使用函数的参数。与其多次调用同一个scipt来重命名函数,不如编写一次并将参数传递给它。OK,我现在就试试,并告诉您我是否运气好。函数myClick(){$.post('script.php?button=1',{};}函数myClick2(){$.post('script.php?button=2',{};}函数myClick3(){$.post('script.php?button=3',{};}但是我应该做什么来代替:setInterval(function(){$.get('script.php',{},function(data){if(data!=lastValue){myFunc();lastValue=data;}}}};},100);我不知道如何在我的JS代码中设置$.get()参数!我可以执行类似$.get('script.php?button=1',{},function(data)的操作{if(data!=lastValue){myFunc();lastValue=data;}}}}),但我想检查所有按钮,而不仅仅是1。我不能有3$.get()不过,这是因为它以前不起作用了。@Fjpackard,我添加了一些解释。您只能使用script.php
进行html更新(因为这样就不会单击任何按钮了?还是我没有理解正确?