doutuan6158 2015-03-14 10:41
浏览 94
已采纳

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

I want to create a program where I can delete mysql records using button with ajax. My problem is that I don't know how to pass a mysql data as ajax parameter so if it matches with the query in the php file, it will be removed. I created one with php alone but it's a static program, I want it to be dynamic with 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;

    }

}

Any help will be greatly appreciated. Thanks

展开全部

  • 写回答

2条回答 默认 最新

  • donglian1953 2015-03-14 12:17
    关注

    Try this it will work :

    Since AJAX happens "behind the scenes" (so to speak) your redirect will just interrupt the response to your javascript handler. So PHP cannot redirect your browser now, javascript can. So use javascript to redirect the user.

    You'll need to return the URL and have your callback kick the browser to a new location.

    "You're doing the ajax call - the php header will redirect the ajax response... not the page that the user is currently sitting on. You will have to modify your javascript code in the client to change the location."

    A javascript redirect is as simple as window.location.href = "http://mylocation";

    Solution to your problem with Ajax and 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>
    

    Solution to your problem without Ajax and 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
        }
    ?>
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 查询优化:A表100000行,B表2000 行,内存页大小只有20页,运行时3页,设计两个表等值连接的最简单的算法
  • ¥15 led数码显示控制(标签-流程图)
  • ¥20 为什么在复位后出现错误帧
  • ¥15 结果有了,想问一下这个具体怎么输入
  • ¥15 怎么修改鸿蒙app的UI及功能设计
  • ¥15 帮我利用jupyter 运行一个正确的代码
  • ¥15 如何使用Gephi软件和Python包中的GephiStreamer交互
  • ¥15 sqlite加密问题咨询
  • ¥15 appdesigner接收不到udp组播的数据
  • ¥15 verilog 非阻塞赋值下的移位拼接错误
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部