如何将 mysql 数据作为 ajax 参数传递

How can I pass mysql data as ajax parameter

我想创建一个程序,我可以使用带有 ajax 的按钮删除 mysql 条记录。我的问题是我不知道如何将 mysql 数据作为 ajax 参数传递,因此如果它与 php 文件中的查询匹配,它将被删除。我单独用 php 创建了一个,但它是一个静态程序,我希望它是动态的 ajax.

index.php :

    <?php

    require_once 'dbconfig.php';

    $query = $con->query("SELECT * FROM statuspost ORDER BY id DESC");

    while($i = $query->fetch_object()){

 echo $i->post ?><button onclick='ajaxWallpost('.<?php 
    $i->id ?>.')'>Remove</button>

<?php
    }
    ?>

            <script src='https://ajax.googleapis.com/ajax/libs/prototype  
    /1.7.2.0/prototype.js'></script>
                <script>

                    function ajaxWallpost(status){

                        new Ajax.Request('wallpost.php?type=post&  
    id='+status, {
                      method:'get',
                      onSuccess: function(transport) {

                      },
                      onFailure: function() { alert('Something went   
    wrong...'); }
                    });

                    }

                </script>               

wallpost.php :

<?php

require_once 'dbconfig.php';

if(isset($_GET['id'], $_GET['type'])){

    $post = $_GET['type'];
    $id = (int)$_GET['id'];

    switch($post){

        case 'post':
            $con->query("DELETE FROM statuspost WHERE id={$id}");
        break;

    }

}

任何帮助将不胜感激。谢谢

HTTP 重定向只是说 "The thing you were looking for? It's actually over there."

它不会使浏览器停止使用 XHR 处理它并开始处理它,就好像您遵循了 link。

index.php 的内容将在 transport.responseText

如果要将浏览器发送到新页面,请不要使用 Ajax。

试试这个它会起作用:

由于 AJAX 发生 "behind the scenes"(可以这么说),您的重定向只会中断对 javascript 处理程序的响应。所以 PHP 现在无法重定向您的浏览器,javascript 可以。所以使用 javascript 重定向用户。

您需要 return URL 并让您的回调将浏览器踢到新位置。

"您正在进行 ajax 调用 - php header 将重定向 ajax 响应...而不是页面用户当前正坐在上面。您必须修改客户端中的 javascript 代码才能更改位置。"

javascript 重定向就像 window.location.href = "http://mylocation";

一样简单

Ajax 和 Javascript 问题的解决方案:

<script>

       function ajaxWallpost(status){

          new Ajax.Request('wallpost.php?type=post&id='+status, {
                      method:'get',
                      onSuccess: function(transport)
                      {
                       window.location.href = "index.php";
                      },
                      onFailure: function() 
                      { 
                       alert('Something went wrong...'); 
                      }
                          });
                                  }
</script>

没有 Ajax 和 Javascript 的问题的解决方案:

 <?php

    require_once 'dbconfig.php';

    if(isset($_GET['id'], $_GET['type'])){

    $post = $_GET['type'];
    $id = (int)$_GET['id'];

    switch($post){

        case 'post':
            $con->query("DELETE FROM statuspost WHERE id={$id}");
        break;

    }

}

    $query = $con->query("SELECT * FROM statuspost ORDER BY id DESC");

    while($i = $query->fetch_object())
{
?>

    <a href="index.php?id=<?php $i->id ?>&type=post>Remove</a>

<?php
    }
?>