doutuan6158 2015-03-14 18: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 20: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 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”