如何从php变量中获取数据属性?

时间:2022-02-12 23:29:49

I have a form that can delete records from a MySql database using ajax and jQuery. I'm trying to get the jQuery to only select the relevant record passed to it and not just delete the top row of records which it does at the moment. I think I need to get

我有一个表单,可以使用ajax和jQuery从MySql数据库中删除记录。我试图让jQuery只选择传递给它的相关记录,而不是删除当前它所做的记录的第一行。我想我得去

<div class="'.$id.'">

from my form and make it a data attribute that can be selected. Hope I'm making myself clear. Many thanks.

从我的表单中,使它成为可以选择的数据属性。希望我讲清楚了。多谢。

<script type="text/javascript">
    //Delete Review
    $(document).ready(function(){
        //Need to get $id here.
        $(".deleteReview").click(function (e) {
            e.preventDefault();
            var username = $("#username").val();
            var film_id = $("#film_id").val();
            var id = $("#id").val();

            $.post('ajax_deleteReview.php', {
                username: username,     
                film_id: film_id, 
                id: id
            }, function(data) {
                $("#message").html(data);
                $("#message").hide();
                $("#message").fadeIn(500); 
                $("#message").fadeOut(2500); 
            });
            return false;
        });
    });
</script>
<form>
    <div class="'.$id.'">
        <input type="hidden" id="username" name="username" value="'. $username.'">
        <input type="hidden" id="film_id" name="film_id" value="'.$film_id .'">
        <input type="hidden" id="id" name="id" value="'.$id .'">
        <button type="submit" id="" class="btn btn-danger btn-xs pull-right">delete</button>
    </form>
</div>

2 个解决方案

#1


3  

I would recommend you to use data-* prefixed attributes to persists your data.

我建议您使用数据-*前缀属性来保存数据。

HTML

HTML

 <div data-id="'.$id.'" data-username="'. $username.'" data-filmid="'.$film_id .'" class="deleteReview">
    <button type="submit" id="" class="btn btn-danger btn-xs pull-right">delete</button>
 <div >

Then you can fetch it using .data(key)

然后您可以使用.data(key)获取它

Return the value at the named data store for the first element in the jQuery collection, as set by data(name, value) or by an HTML5 data-* attribute

以数据(名称、值)或HTML5 data-*属性设置的jQuery集合中的第一个元素返回指定数据存储的值。

Script

脚本

 //Delete Review
$(document).ready(function(){
    //Need to get $id here.
    $(".deleteReview").click(function (e) {
        e.preventDefault();
        var username = $(this).data("username");
        var film_id = $(this).data('filmid');
        var id = $(this).data('id');

        ......
        return false;
    });
});

#2


0  

First in your PHP do something like this, you add the data attributes to your delete button since you are not actually submitting a real form. I also moved the deleteReview class to the button.

首先,在PHP中做这样的事情,将数据属性添加到delete按钮中,因为实际上并没有提交真正的表单。我还将deleteReview类移动到按钮。

print '<button type="submit" class="deleteReview" data-id="'.$id.'" data-film-id="'.$film_id.'" data-username="'.htmlentities($username).'" class="btn btn-danger btn-xs pull-right">delete</button>';

Then, in your jQuery, update click event callback like so:

然后,在jQuery中更新单击事件回调,如下所示:

$(".deleteReview").click(function (e) {
    e.preventDefault();
    var username=$(this).data('username');
    var film_id=$(this).data("film-id");
    var id=$(this).data('id');
    /* the rest of your code */

The data attributes become accessible easily with jQuery using $(this).data('some-attribute-name')

使用jQuery使用$(this).data('some-attribute-name')可以轻松访问数据属性

#1


3  

I would recommend you to use data-* prefixed attributes to persists your data.

我建议您使用数据-*前缀属性来保存数据。

HTML

HTML

 <div data-id="'.$id.'" data-username="'. $username.'" data-filmid="'.$film_id .'" class="deleteReview">
    <button type="submit" id="" class="btn btn-danger btn-xs pull-right">delete</button>
 <div >

Then you can fetch it using .data(key)

然后您可以使用.data(key)获取它

Return the value at the named data store for the first element in the jQuery collection, as set by data(name, value) or by an HTML5 data-* attribute

以数据(名称、值)或HTML5 data-*属性设置的jQuery集合中的第一个元素返回指定数据存储的值。

Script

脚本

 //Delete Review
$(document).ready(function(){
    //Need to get $id here.
    $(".deleteReview").click(function (e) {
        e.preventDefault();
        var username = $(this).data("username");
        var film_id = $(this).data('filmid');
        var id = $(this).data('id');

        ......
        return false;
    });
});

#2


0  

First in your PHP do something like this, you add the data attributes to your delete button since you are not actually submitting a real form. I also moved the deleteReview class to the button.

首先,在PHP中做这样的事情,将数据属性添加到delete按钮中,因为实际上并没有提交真正的表单。我还将deleteReview类移动到按钮。

print '<button type="submit" class="deleteReview" data-id="'.$id.'" data-film-id="'.$film_id.'" data-username="'.htmlentities($username).'" class="btn btn-danger btn-xs pull-right">delete</button>';

Then, in your jQuery, update click event callback like so:

然后,在jQuery中更新单击事件回调,如下所示:

$(".deleteReview").click(function (e) {
    e.preventDefault();
    var username=$(this).data('username');
    var film_id=$(this).data("film-id");
    var id=$(this).data('id');
    /* the rest of your code */

The data attributes become accessible easily with jQuery using $(this).data('some-attribute-name')

使用jQuery使用$(this).data('some-attribute-name')可以轻松访问数据属性