简体   繁体   English

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

[英]How can I pass mysql data as ajax parameter

I want to create a program where I can delete mysql records using button with ajax. 我想创建一个程序,在其中可以使用带有Ajax的按钮删除mysql记录。 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. 我的问题是我不知道如何将mysql数据作为ajax参数传递,因此,如果它与php文件中的查询匹配,它将被删除。 I created one with php alone but it's a static program, I want it to be dynamic with ajax. 我只用php创建了一个,但它是一个静态程序,我希望它与ajax一起动态运行。

index.php : 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 : 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 谢谢

An HTTP redirect just says "The thing you were looking for? It's actually over there." HTTP重定向仅显示“您正在寻找的东西?它实际上在那儿。”

It won't make the browser stop processing it using XHR and start processing it as if you followed a link. 它不会使浏览器停止使用XHR处理它并开始像对待链接一样开始处理它。

The contents of index.php will be in transport.responseText . index.php的内容将在transport.responseText

If you want to send the browser to a new page, then don't use Ajax. 如果要将浏览器发送到新页面,请不要使用Ajax。

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. 由于AJAX发生在“幕后”(可以这么说),因此您的重定向只会中断对javascript处理程序的响应。 So PHP cannot redirect your browser now, javascript can. 因此,PHP现在无法重定向您的浏览器,而javascript可以。 So use javascript to redirect the user. 因此,请使用javascript重定向用户。

You'll need to return the URL and have your callback kick the browser to a new location. 您需要返回URL,并使回调将浏览器移至新位置。

" 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. " 您正在执行ajax调用-php标头将重定向ajax响应……而不是用户当前所在的页面。您将必须在客户端中修改javascript代码以更改位置。

A javascript redirect is as simple as window.location.href = " http://mylocation "; javascript重定向就像window.location.href =“ http:// mylocation ”一样简单;

Solution to your problem with Ajax and Javascript : 解决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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM