如何在AJAX / JavaScript的调用PHP文件? [英] How to invoke PHP file in AJAX/JavaScript?

查看:118
本文介绍了如何在AJAX / JavaScript的调用PHP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个code表示GET使用AJAX方法的PHP文件。的目的是通知用户如果影片表是空的或不是。如果表是空的,它会显示一个确认框,如果用户想添加的电影或不是会问?

I have this code that GET the php file using an AJAX method. The goal is to inform the user if the movie table is empty or not. If the table is empty, it will display a confirm box that will ask if the user wants to add movies or not?

下面是我的index.php文件:

Here's my index.php file:

<a href="#" onclick="checkMovieTable('../ajaxphp/moviestbl.php')" title="Movies">Movies</a>

下面是我的moviestbl.php:

Here's my moviestbl.php:

<?php
include ('../phpfunc/connect'); //includes the connection to mysql
$checkMovieTable = mysql_query("SELECT * FROM movies ORDER BY title ASC") or die("Table not found");
$countRows = mysql_num_rows($checkMovieTable);
if($countRows == 0){
?>
 <script language="JavaScript">
 var option = confirm("No Movies found. Add Movies now?");
 if(option ==true){
   //redirect to another page
   window.location = "../addmedia.php?add=movies";
 }
 else{
    //do nothing. return to current window.
 }
 </script>
<?php
 }
?>

最后,这是我的AJAX文件。

And finally, here's my AJAX file.

<script>
/*connect to database then count the table, if it's zero, dispLay a confirmation box*/
var XMLHttpRequestObject = false;

if(window.XMLHttpRequest){
        XMLHttpRequestObject = new XMLHttpRequest();
}
else if(window.ActiveXObject){
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");      
}

function checkMovieTable(dataSource){
    //get data from the server
    //onreadystatechange = stores a function
    var obj = document.getElementById("readystate");
    XMLHttpRequestObject.open("GET", dataSource);

    XMLHttpRequestObject.onreadystatechange = function() {
        if(XMLHttpRequestObject.status == 200){
            obj.innerHTML = XMLHttpRequestObject.responseText;
        }
    }
    XMLHttpRequestObject.send(null);

}
</script>

当我点击链接,它不会做或显示任何内容。需要帮忙。谢谢。

When I click the link, it is not doing or displaying anything. need help. thanks.

推荐答案

我是能够使code运行了。由于萤火虫和咖啡。锚标签的问题。我不知道为什么,但是当我用IMG代替,它的工作。

I was able to make the code run now. Thanks to Firebug and coffee. The anchor tag is the problem. I don't know why, but when I used the img instead, it worked.

<img alt="movies" style="cursor:hand;cursor:pointer;" onclick="checkMovieTbl('/movies/ajaxphp/moviestbl.php')"/>

PHP脚本文件:

<?php
//fetch the data from the table movies
include '../phpfunc/connect.php';
$checkMovieTable = mysql_query("SELECT * FROM movies ORDER BY title ASC") or die("Table not found");
$countRows = mysql_num_rows($checkMovieTable);
$json = json_encode($countRows);
if($json == 0){
    echo $json;
}
?>

最后,在 AJAX 文件。

var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
} 
else if (window.ActiveXObject) {
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function checkMovieTbl(url){
    //if not false
    if(XMLHttpRequestObject){
        XMLHttpRequestObject.open("GET", url, false); //not sync
        XMLHttpRequestObject.onreadystatechange = function(){
            if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200){
                var response = XMLHttpRequestObject.responseText;
                //alert(XMLHttpRequestObject.responseText + "readyState:" + XMLHttpRequestObject.readyState + "\nStatus:" +XMLHttpRequestObject.status);
                if(response==0){
                    //execute something here
                }
            }
            else{
                alert(XMLHttpRequestObject.responseText + "readyState:" + XMLHttpRequestObject.readyState + "\nStatus:" +XMLHttpRequestObject.status);
            }
        }
        XMLHttpRequestObject.send();
    }

}

谢谢大家谁回答我的问题,并帮助了我。乔!

Thanks to everyone who answered my questions and helped me. Chiao!

这篇关于如何在AJAX / JavaScript的调用PHP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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