通过Ajax将多个PHP变量POST到PHP Handler

时间:2013-03-26 00:19:45

标签: php jquery ajax variables

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

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,反之亦然。我正在发送带有jquery ajax的article_id帖子,但我不知道如何发送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'];
    }

1 个答案:

答案 0 :(得分: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请求的可能方法