使用ajax激活和停用产品

时间:2014-04-18 20:22:24

标签: javascript php ajax

我试图使用ajax将按钮从活动状态更改为无效状态,只要用户单击该按钮并对数据库进行更改,反之亦然。但是代码似乎不起作用,每当我点击按钮时,错误就出现了:“有些东西没能正常工作。”

$query = "SELECT * FROM product";    //query for getting the product from database.
$result = $mysqli->query($query); 
if (!$result) die ("Database access failed: " . mysql_error()); //error message.

$rows = mysqli_num_rows($result);  //getting number of row.

if ($rows > 0)  //Checking whether product exist in the database to shown.
{
    for ($i=0; $i < $rows; $i++)  //loop through all the row.
    { 
        $row = mysqli_fetch_row($result);
        echo '<div id="active">';
        if ($row[8] == 1) //product is actived display Deactive button.
        {
            echo '<form onclick="changeActive()" method="post" id="activeButton">';
            echo '<input type="hidden" name="deactive" value="yes"/>';
            echo '<input type="hidden" name="id"    value="'.$row[0].'" />';
            echo '<input type="hidden" name="image" value="$row[5]" />';
            echo '<input type="submit" name="deactive" value="DEACTIVE PRODUCT" />';
            echo '</form>';
        } // end if statement.
        else   //product is deactived display Active button.
        {
            echo '<form onclick="changeActive()" method="post" id="activeButton">';
            echo '<input type="hidden" name="active" value="yes"/>';
            echo '<input type="hidden" name="id"    value="'.$row[0].'" />';
            echo '<input type="hidden" name="image" value="$row[5]" />';
            echo '<input type="submit" name="active" value="ACTIVE PRODUCT" />';
            echo '</form>';
        } //end else statement
        echo '</div>';
    }
}

这是我的ajax代码。

request = function(link, target, result)
{
    var changeListener;
    var xhr = new XMLHttpRequest();
    changeListener = function()
    {
        if(xhr.readyState === 4)
        {
            if(xhr.status == 200) 
            {
                target.innerHTML = xhr.responseText;
            } 
            else 
            {
                target.innerHTML = "<p>Something didn't work right.</p>";
            }
        }
        else
        {
            target.innerHTML = "<p>Hold Up...</p>";
        }
    }; 
    xhr.open("GET", link, true);
    xhr.onreadystatechange = changeListener;
    xhr.send(result);
};

changeActive = function()
{
var target;
target = document.getElementById('active');
request("active_deactive.php", target);
    };

和php文件:

<?php
include_once 'connect.php'; //connect to the database
if (isset($_POST['deactive']) && isset($_POST['id'])) //Check whether deactive button is pushed.
{
    $id = $_POST['id'];
    $query = "UPDATE Product SET Active = 0 WHERE ID = '$id'";
    $deactive = $mysqli->query($query);
    echo '<form onclick="changeActive()" method="post" id="activeButton")">';
    echo '<input type="hidden" name="deactive" value="yes"/>';
    echo '<input type="hidden" name="id"    value="'.$row[0].'" />';
    echo '<input type="hidden" name="image" value="$row[5]" />';
    echo '<input type="submit" name="deactive" value="DEACTIVE PRODUCT" />';
    echo '</form>';
}

if (isset($_POST['active']) && isset($_POST['id'])) //Active button is pushed.
{
    $id = $_POST['id'];  //getting the id.
    $query = "UPDATE Product SET Active = 1 WHERE ID = '$id'"; 
    //back for delete the picture file.
    $active = $mysqli->query($query);
    echo '<form onclick="changeActive()" method="post" id="activeButton")">';
    echo '<input type="hidden" name="active" value="yes"/>';
    echo '<input type="hidden" name="id"    value="'.$row[0].'" />';
    echo '<input type="hidden" name="image" value="$row[5]" />';
    echo '<input type="submit" name="active" value="ACTIVE PRODUCT" />';
    echo '</form>';
}

?>

1 个答案:

答案 0 :(得分:0)

也许你必须看到JQuery {AJ}的AJAX API https://api.jquery.com/jQuery.ajax/ 这对你的工作来说非常容易。我建议使用此API来使用ajax,而不是使用本机XMLHttpRequest对象。