【问题标题】:Change button on clicked and records update in database and vice versa单击更改按钮并在数据库中更新记录,反之亦然
【发布时间】:2017-07-05 12:02:42
【问题描述】:

我有一个称为删除的按钮。如果任何用户单击删除,则删除文本将替换为恢复,删除操作将在数据库中执行。 同样认为我必须设置恢复按钮。如果单击恢复,则通过删除替换文本并执行恢复操作,但我只需要一个按钮。 请检查我的以下代码。有删除按钮,当我单击删除按钮时,它显示恢复按钮并重定向以执行删除事件。

我知道如何删除和恢复 php 中的记录。我需要脚本来执行。 我在 Jquery 的帮助下所做的更改按钮。不刷新,我必须显示恢复按钮。

$(document).ready(function () {
    $("a.btn").click(function () {
        if ($(this).hasClass('btn')) {
            $(this).html('Restore').toggleClass('btn');
        } else {
            $(this).html('Delete').toggleClass('btn');
        }
    });
});


function b_delete(id){
    $.ajax({
        type: "get",
        url: "process.php?function=b_delete&record_id=id",
        data: {
            record_id:record_id
        },
        success: function (data){
            alert(data);
        },
        error: function (xhr, ajaxOptions, thrownError){
        }
    });
    
    return false;
}
a{
	text-decoration: none;
}
.btn-center
{
text-align: center;
}
.btn{
	background-color: blue;
	color: #fff;
	padding: 10px 15px;

}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<div class="btn-center">
<a href="javascript:void(0);" onclick="return b_delete('<?php echo $user_Id;?>')"  class="btn">Delete</a>
</div>

Process.php

switch($_GET['function']) {
case 'b_delete':b_delete($conn);break;
default : redirect('index.php');
}

function b_delete($conn)
{
$record_id=$_GET['record_id'];
echo "testing";
}

【问题讨论】:

  • 单击按钮时,向您的 php 脚本发送 Ajax 请求,您可以在其中删除/恢复数据库中的内容。这是不刷新页面的唯一方法
  • 感谢Cashbee先生的回复。我同意你的看法,但我们如何才能访问它
  • Cashbee 先生,我试过 删除
  • 嗨 Mr.Cashbee,我尝试使用 ajax 但仍然有问题。你能帮我吗
  • checkout firebase,它让这样的事情变得非常简单firebase.google.com

标签: javascript php jquery ajax html


【解决方案1】:

您正在使用 get 方法进行 ajax 调用, 您可以通过 url 传递参数或使用数据。 在数据中传递参数“record_id”的值时有一个小错误。 必须是数据:{record_id:id}

<!-- language: lang-css -->
<style>
a {
    text-decoration: none;
}

.btn-center {
    text-align: center;
}

.btn {
    background-color: blue;
    color: #fff;
    padding: 10px 15px;
}
</style>
<!-- language: lang-html -->

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<div class="btn-center">
    <a href="javascript:void(0);"
        onclick="return b_delete('<?php echo $user_id;?>')" class="btn">Delete</a>
</div>

<!-- end snippet -->

<script type="text/javascript">
    $(document).ready(function () {
        $("a.btn").click(function () {
            if ($(this).hasClass('btn')) {
                $(this).html('Restore').toggleClass('btn');
            } else {
                $(this).html('Delete').toggleClass('btn');
            }
        });
    });


    function b_delete(id){
        $.ajax({
            type: "get",
            url: "process.php?function=b_delete&record_id=id",
            data: {
                record_id:id
            },
            success: function (data){
                alert(data);
            },
            error: function (xhr, ajaxOptions, thrownError){
            }
        });

        return false;
    }
</script>

【讨论】:

  • 感谢 Mr.Saurabh 的回复,它正在工作,但刷新后,它变成了恢复按钮被删除替换
  • 当页面刷新时,您必须从数据库中检查所需记录 ID 的状态。如果“状态”状态记录被删除,我们必须显示“恢复”按钮,否则显示“删除”。
  • 对。但是我应该在哪里添加该代码?你能帮我写代码吗?
【解决方案2】:

您可以尝试将数据属性分配给a

<a data-action="delete">Delete</a>

然后在你的脚本中:

$('a').on('click', function(){
   if ($(this).data('action') == 'delete')
   {
     //Do your ajax for delete
     $.ajax({
        type: "get",
        url: "process.php?function=b_delete",
        data: {
            'record_id':record_id
        },
        success: function (data){
            alert(data);
        },
        error: function (xhr, ajaxOptions, thrownError){
        }
     });

     $(this).text('Restore');
     $(this).data('action', 'restore');
   } else {
     //Do  your ajax for restore
     $(this).text('Delete');
     $(this).data('action', 'delete');
   }
});

【讨论】:

  • 感谢 Mr.herondale 的回复,你能分享一下 ajax 代码吗,因为我现在很困惑。如何从这里调用ajax?
  • @NarendraVerma 只需将您的函数b_delete() 中的ajax 请求代码复制并粘贴到click 事件处理程序中的删除语句中,您可以在我放置评论的$(this).text('Restore'); 之前执行此操作。对恢复执行相同操作。
  • 仍然是同样的问题 Mr.herondale,点击删除后没有执行任何操作。
  • 你得到什么输出?
  • 嗯,我只是注意到这里有一些冗余。尝试从 url 中删除 &amp;record_id=id 看看是否可行
【解决方案3】:

您对 ajax 的尝试还不错,但我可以看到我在以下代码中更改了一些小东西:

HTML

<div class="btn-center">
    <button id="delete_restore" onclick="b_delete(this, '<?php echo $user_Id;?>')" class="btn">Delete</a>
</div>

Javascript

function b_delete(element, user_id){

    // change text of button
    if (element.hasClass('btn')) {
        element.html('Restore').toggleClass('btn');
    } else {
        element.html('Delete').toggleClass('btn');
    }


    /* where is this coming from? */
    var record_id = 9999;


    var mode = 'b_delete';
    if(!element.hasClass('btn')){
        mode = 'b_restore';
    }

    $.ajax({
        type: "post",
        url: "process.php",
        data: {
            user_id: user_id,
            mode: mode,
            record_id: record_id
        },
        success: function (data){
            console.log(data);
        },
        error: function (xhr, ajaxOptions, thrownError){
            console.log("ajax request failed", xhr, ajaxOptions, thrownError);
        }
    });
}

process.php

if(isset($_POST['mode'])){
    switch($_POST['mode']) {
        case 'b_delete': b_delete($conn);break;
        case 'b_restore': b_restore($conn);break;
        default : header('Location: index.php'); //maybe use full url, i dont know for sure
    }
}

function b_delete($conn)
{
    $record_id=$_POST['record_id'];
    echo "testing delete";
}
function b_restore($conn)
{
    $record_id=$_POST['record_id'];
    echo "testing restore";
}

【讨论】:

  • 感谢Cashbee先生的回复。我试过你的代码,当我点击删除按钮时,没有执行任何操作。
  • 编辑了我的代码(var record_id 的取消注释声明)。您必须在开发人员工具控制台中查看任何消息。如果代码有效,它将在那里输出测试字符串,如果它不在那里会有某种错误
  • Cashee 先生,我收到错误 SyntaxError: unterminated string literal b_delete(this, '
    in console
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-17
  • 2014-04-28
  • 2016-06-11
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 2015-03-02
相关资源
最近更新 更多