<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'];
}