0

<span>通过 Ajax 将多个 php 变量(或数据库表字段)从用作图标的元素传递到我的处理程序的最佳方法是什么?提前感谢您的帮助!

桌子

art_id  art_title  art_company    art_featured
1       lorem 1    lorem ipsum 1  1
2       lorem 2    lorem ipsum 2  0

HTML/PHP

<section class="row">
    <?php
    $sql_categories = "SELECT art_title, art_company, art_id, art_featured FROM app_articles"; 

        if($result = query($sql_categories)){
            $list = array();

            while($data = mysqli_fetch_assoc($result)){
                array_push($list, $data);
            }

            foreach($list as $i => $row){ 
            ?>
                <div class="row">
                    <div class="column two"><p><?php echo $row['art_title']; ?></p></div>
                    <div class="column two"><p><?php echo $row['art_company']; ?></p></div>
                    <div class="column one"><span id="<?php echo $row['art_id']; ?>" class="icon-small star"></span></div>
                </div>
            <?php
            }
        }
        else {
            echo "FAIL";
        }
    ?>
    </section>

jQuery

        $(".star").click(function(){

        var art_id = $(this).attr('id');

        $.ajax({
        type: "POST",
        data: {art_id:art_id},
        url: "ajax-feature.php",
        success: function(data){
            if(data != false) {

            } 
            else {

            }  
        }
        });

    });

MYSQL/PHP

    if(isset($_POST['art_id'])) {



    $sql_articles = "UPDATE `app_articles` SET `art_featured` = 1 WHERE `art_id` =".$_POST['art_id'];

    if(query($sql_articles)) {
        echo "YES";
    }
    else {
        echo "NO";
    }
}
else {
    echo "FAIL";
}

我想要做的是将字段的值art_featured从 0 更改为 1,比如如果art_featured是 0,那么我想将数据库字段更新为 1,反之亦然。我正在article_id使用 jquery ajax 发送 via 帖子,但我不知道如何发送该article_feature字段。因为我猜最后会做的是这样的事情

        if($_POST['art_featured']==0) {
        $sql_articles = "UPDATE `app_articles` SET `art_featured` = 1 WHERE `art_id` =".$_POST['art_id'];

    }
    else {
        $sql_articles = "UPDATE `app_articles` SET `art_featured` = 0 WHERE `art_id` =".$_POST['art_id'];
    }
4

1 回答 1

0
    ('span').click(function(){

    jQuery.ajax({type:'POST',
    data:'id=8391283&length=8943&north='+variable+'&typ=normal,

    url:'yourUrl',
    success:function(data,textStatus){alert('success')},
    error:function(XMLHttpRequest,textStatus,errorThrown){}
    });

});

这是使用多个参数发出 ajax 请求的一种可能方式

于 2013-03-26T00:27:15.907 回答