php 从javascript调用php文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16358925/
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
call php file from javascript
提问by anders
Hi I have searched the web but can't get this to work. I'm trying to call the file databaseUpdated.php(placed in the same folder as the index.phpfile) from a function that is called every 10 seconds.
嗨,我已经在网上搜索过,但无法使其正常工作。我正在尝试从每 10 秒调用一次的函数中调用文件databaseUpdated.php(与index.php文件位于同一文件夹中)。
If I place the following in the index.php
如果我将以下内容放在 index.php 中
<script type='text/javascript'>updateboolean();</script>;
the function is runned so thats not the problem, the problem is that the php file is not read.
运行该函数所以这不是问题,问题是未读取php文件。
the file databaseUpdated.php
文件databaseUpdated.php
<body>
<?php
echo ("<script type='text/javascript'>updateboolean();</script>;");
?>
</body>
And here is my functions in the javascript
这是我在 javascript 中的函数
$(document).ready(function(){
setInterval(function() {
$.get("databaseUpdated.php");//Can't get this to work any obvious reason for this (And yes I have jquery working)?
return false;
}, 10000);
});
function updateboolean(){
alert("Database updated");
document.location.reload(true);
}
Thanks in advance =)
提前致谢 =)
Edit
编辑
_________________________________________________________________________________________
________________________________________________________________________________________
When I do
当我做
alert(data); in the below function I get the result as the image will show
警报(数据);在下面的函数中,我得到了图像将显示的结果
$(document).ready(function(){
setInterval(function() {
$.get('databaseUpdated.php', function(data) {
alert('Load was performed.');
alert(data);
eval(data);
});
}, 5000);
});
But the "eval(data)" doesn't seem to work
但是“ eval(data)”似乎不起作用
采纳答案by Dharmesh Patel
$.get("databaseUpdated.php"); // This will only return contents of that file The scripts in that file are not executed. To execute them you need to do eval(). So Try This
$.get('databaseUpdated.php', function(data) {
eval(data);
});
Also, may be you will require to change your php file as following:
此外,您可能需要按如下方式更改您的 php 文件:
echo ("updateboolean();");
回答by Arun Killu
where is your callback function for ajax this is how it should be
你的 ajax 回调函数在哪里,这就是它应该的样子
$(document).ready(function(){
setInterval(function() {
$.get('databaseUpdated.php', function(data) {
alert('Load was performed.');
});
}, 10000);
});
回答by Rohan Kumar
Try this:
尝试这个:
$(document).ready(function(){
setInterval(function() {
$.get('databaseUpdated.php', function(data) {
alert("Database updated");
// or alert(data); //in case you return data from php
document.location.reload(true);
});
}, 10000);
});
回答by mguimard
By doing $.get("databaseUpdated.php")
, you request the PHP page, but ignore the return results.
通过这样做$.get("databaseUpdated.php")
,您请求 PHP 页面,但忽略返回结果。
Try this:
尝试这个:
PHP
PHP
echo "updateboolean();";
JavaScript:
JavaScript:
$.getScript("databaseUpdated.php");
回答by deepi
try this in place of your echo
试试这个代替你的回声
echo "<script>updateboolean();</script>";