0

我有这段代码使用 AJAX 方法获取 php 文件。目标是通知用户电影表是否为空。如果表格为空,则会显示一个确认框,询问用户是否要添加电影?

这是我的 index.php 文件:

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

这是我的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 文件。

<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>

当我单击链接时,它没有执行或显示任何内容。需要帮忙。谢谢。

4

3 回答 3

1

您需要使用 ajax 请求从服务器获取数据,响应可能是 JSON 格式。

建议。

  1. 使用 jQuery 和 $.ajax - 从服务器中提取数据
  2. 在得到响应 - 做确认和切换窗口

您可以构建逻辑以处理客户端和服务器上的特定逻辑

  • PHP:服务器上的逻辑可以创建和接口,用于响应电影表上条目状态的 JSON 结果。您当前的查询应该可以正常工作。

  • Javascript:利用php上定义的接口查询数据,利用'confirm' javascript进行切换。

现在如果你改变

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

您应该看到它适用于页面重新加载和重定向。

于 2013-06-22T07:47:03.220 回答
0

我现在能够使代码运行。感谢萤火虫和咖啡。锚标签是问题所在。我不知道为什么,但是当我改用 img 时,它起作用了。

<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();
    }

}

感谢所有回答我的问题并帮助我的人。咻!

于 2013-06-22T12:40:31.667 回答
0

这可能会更好:

<?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){
    echo "empty";
 }
?>


<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;
            if(XMLHttpRequestObject.responseText === "empty"){
                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.
                }
             }
        }
    }
    XMLHttpRequestObject.send(null);

}
</script>
于 2013-06-22T07:43:50.573 回答