3

我的页面上有 3 个按钮,根据用户单击的按钮,我想通过 ajax 运行我的数据库中的删除查询。当用户单击按钮时,javascript 函数似乎可以工作,但它不会在 php 脚本中运行查询。

html页面:

<?php session_start(); ?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7"> 
<script>
    function myFunction(name)
    {
        var r=confirm("Are you sure? This action cannot be undone!");
        if (r==true)
        {
            alert(name); // check if is getting in if statement and confirm the parameter's value
            var xmlhttp;
            if (str.length==0)
            { 
                document.getElementById("clearMessage").innerHTML="";
                return;
            }
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("clearMessage").innerHTML= responseText;
                }
            }
            xmlhttp.open("GET","clearDatabase.php?q="+name,true);
            xmlhttp.send();
        }
        else
            alert('pff');
    }
</script>
</head>
<body>
<div id="wrapper">
<div id="header"></div>
<div id="main">
    <?php if (session_is_registered("username")){ ?>
    <!--<a href="#">Εκκαθάριση παλαιών μηνυμάτων</a><br />
    <a href="#">Εκκαθάριση παλαιών συνεδρίων</a><br />
    <a href="#">Εκκαθάριση push notifications</a><br />-->
    <input type="button" value="Εκκαθάριση παλαιών μηνυμάτων" onclick="myFunction('messages')" />
    <input type="button" value="Εκκαθάριση παλαιών συνεδρίων" onclick="myFunction('conferences')" />
    <input type="button" value="Εκκαθάριση push notifications" onclick="myFunction('notifications')" />
    <div id="clearMessage"></div>
    <?php } else echo "Login first."; ?>
</div>
<div id="footer"></div>
</div>
</body>
</html>

和 php 脚本:

<?php
if (isset($_GET["q"]))
    $q=$_GET["q"];

$host = "localhost";
$database = "dbname";
$user = "dbuser";
$pass = "dbpass";

$con = mysql_connect($host,$user,$pass) or die(mysql_error()); 
mysql_select_db($database,$con) or die(mysql_error()); 

if ($q=="messages")
    $query = "DELETE FROM push_message WHERE time_sent IS NOT NULL";
else if ($q=="conferences")
    $query = "DELETE FROM push_message WHERE time_sent IS NOT NULL";
else if ($q=="notifications") {
    $query = "DELETE FROM push_friend WHERE time_sent IS NOT NULL";
}

$res = mysql_query($query,$con) or die(mysql_error());

if ($res)
    echo "success";
else
    echo "failed";

mysql_close($con);
?>
4

4 回答 4

0

why dont you use jquery -> ajax() ?

And your code will look like this:

function myFunction(name)
    {
        var r=confirm("Are you sure? This action cannot be undone!");
        if (r==true)
        {
            alert(name); // check if is getting in if statement and confirm the parameter's value

             $.ajax({
               type: "GET",
               url: "clearDatabase.php?q="+name,true,
               success: function (res){
                    $("#clearMessage").html(res );
              }
             });

        }
        else{
            alert('pff');
           }
    }
于 2013-11-06T16:02:45.010 回答
0

你有一些问题......这得到了来自clearDatabase.php......我只是让它输出一些通用文本

<?php session_start(); ?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-7"> 
<script>
    function myFunction(name)
    {
        var r=confirm("Are you sure? This action cannot be undone!");
        if (r==true)
        {
            // Issue #1: str is not defined anywhere 
            var str = "sfs"; 

            alert("name " + name); // check if is getting in if statement and confirm the parameter's value
            var xmlhttp;
            if (str.length==0)
            { 
                document.getElementById("clearMessage").innerHTML="";
                return;
            }
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            } 
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    // Issue #2: responseText was not defined... it needs to belong to something
                    document.getElementById("clearMessage").innerHTML= xmlhttp.responseText;
                }
            } 
            xmlhttp.open("GET","clearDatabase.php?q="+name,true);
            xmlhttp.send();
        }
        else
            alert('pff');
    }
</script>

注意:任何运行的人都"clearDatabase.php?q="+name可以从您的数据库中删除任何内容。此外,进行这些更改,但如果代码"clearDatabase.php?q="+name不起作用,它仍然可能不起作用。

另外:如果您提供了控制台错误,我们为您解决此问题会容易得多。

我是如何解决这个问题的:我只是自己复制并粘贴到一个文档中,然后打开 Chrome 版本的 Firebug ( control+shift+j) 并且有红色文本告诉我str没有定义。所以我把它定义为var str="". 然后我意识到它正在击中if (str.length==0),所以我给它一个价值。然后,我收到了一个responseText未定义的新错误。我用谷歌搜索了如何使用 javascript ajax 调用的响应形式(我只知道 jquery)并查看了示例。我看到它需要来自请求并添加xmlhttp.responseText;。然后它起作用了。

于 2013-11-06T16:41:49.357 回答
0

尝试;

xmlhttp.open("POST","clearDatabase.php?q="+name,true);

代替;

xmlhttp.open("GET","clearDatabase.php?q="+name,true);
于 2013-11-06T15:57:18.187 回答
0

首先打开 xmlhttp 请求。

尝试这个:

xmlhttp.open("GET","clearDatabase.php?q="+name,true);
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("clearMessage").innerHTML= responseText;
    }
}
xmlhttp.send(null);
于 2013-11-06T15:57:53.387 回答