WordPress插件 / Ajax调用
WordPress插件 / Ajax调用
我一直在研究JavaScript和AJAX的世界。我离成功非常近了,但由于某种原因,我不认为我正确地连接到了WordPress的ajax函数。我研究了文档和这些内容,并认为已经完成了99%的工作。这个应用程序的作用是显示一系列项目。每个项目都有一个"+"按钮。点击按钮会弹出一个确认框,如果确认,则获取所需的数据传递给php。php使用wpdb->insert将项目添加到mysql中。如果购买,还会进行一些更改。
js的工作一直持续到调用,正确获取值等。如果我硬编码从POST中获取的值,单独测试php也是可以的。所以我知道这两个部分都可以运行,我只是无法让js正确调用ajax api。请有人看一下这个问题,并告诉我如何将它们连接在一起,使ajax调用实际运行php?
以下是代码:
get_row($getItemDetailsSQL); $iID = $getItemDetailsResults->idfyxt_items; $iName = $getItemDetailsResults->fyxt_item_name; $iDesc = $getItemDetailsResults->fyxt_item_description; $iCost = $getItemDetailsResults->fyxt_item_cost; $iWeight = $getItemDetailsResults->fyxt_item_weight; $charItemTable = fyxt_char_items; $wpdb->insert( $charItemTable, array ( idfyxt_item => $iID, idfyxt_character => $charID, item_name => $iName, item_desc => $iDesc, item_cost => $iCost, item_weight => $iWeight, item_quant => 1, equip => 0, carried => 1 ) ); $wpdb->print_error(); $newItemAdded = $wpdb->insert_id; // 如果购买项目,则减去现金 if ($buyItem == 1 ) { $curCharMoneySQL = "Select fyxt_wp_db_fatcow.fyxt_characters.char_money From fyxt_wp_db_fatcow.fyxt_characters Where fyxt_wp_db_fatcow.fyxt_characters.idfyxt_character = $charID"; $curCharCash = $wpdb->get_var($curCharMoneySQL); $wpdb->print_error(); $newCash = $curCharCash - $iCost; $changeCashSQL = " UPDATE fyxt_characters SET char_money = $newCash WHERE idfyxt_character = $charID"; $changeCash = $wpdb->query($changeCashSQL); $wpdb->print_error(); } $debugArray = Array(); array_push($debugArray,$charID, $itemID, $buyItem, $getItemDetailsSQL, $getItemDetailsResults,$newItemAdded, $newCash); echo $debugArray ; die(); } ?>
我相当确定问题是以下两种情况之一(也可能是两种情况的结合)。我不确定是否正确地将这些函数连接到WordPress,或者可能是由于我为jQuery按钮创建的嵌套函数而引起的问题。不过我怀疑是第二种情况,因为它似乎是有效的... 但是服务器返回的是一个没有任何数据库活动的0。以下是日志内容:
cID = 112 ?charID=112:538 charMoney = 9990 ?charID=112:539 thisValue = + ?charID=112:540 iID = 664 ?charID=112:541 buy = 0 ?charID=112:542 ajaxurl = http://localhost/nnnnnnnn.com/wp-admin/admin-ajax.php ?charID=112:543 data = [object Object] ?charID=112:550 Object {action: "addItemAJAX", charID: 112, itemID: 664, buyItem: 0} ?charID=112:551 XHR finished loading: "http://localhost/nnnnnnnn.com/wp-admin/admin-ajax.php". jquery-1.9.1.js:8526 send jquery-1.9.1.js:8526 jQuery.extend.ajax jquery-1.9.1.js:7978 jQuery.(anonymous function) jquery-1.9.1.js:7614 (anonymous function) ?charID=112:554 jQuery.event.dispatch jquery-1.9.1.js:3074 elemData.handle
非常感谢您的所有帮助和建议!