ajax foreach,php - AJAX within a foreach loop - Stack Overflow

本文作者在不使用插件的情况下,寻求解决如何在foreach循环中通过AJAX调用PHP,实现列表项的删除操作,同时传递两个参数更新数据库的问题。对于AJAX新手来说,这是一篇关于基础操作与数据库交互的实用指南。
摘要由CSDN通过智能技术生成

I am unfamiliar with AJAX and am having a difficult time trying to learn it for what I need.

I need to write ajax calls within a foreach loop.

if i just use PHP calls they will all fire even if i don't use the onclick event.

What im basically doing is writing out a list from the DB and adding a remove button to the row.

when the remove link is clicked, it will fire a query to update a field in the DB for item.

My index.php file

<?php echo $item['item_name']; ?>

My PHP code: (note: I am using wordpress's $wpdb to query the WP database. query is valid. There is no user input, and its on an admin page so dont worry about prepare() or other injection defenses. )

$wpdb->query("UPDATE " . $wpdb->prefix."item

SET is_removed =" . $remove_option . "

WHERE item_id =" . $item_id );

?>

($remove_option is populated earlier in the index.php page, and $item_id comes from $items array)

I need to pass 2 variables through the ajax call, populate $remove_option and $item_id.

fire the query, return to index.php page.

How do I use ajax to achieve this? I'm completely unfamiliar with ajax, and i am not using a plugin for WP, only php scripts.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值