2015-04-07 70 views
-1

在我的single-product.php页面上,我有一个下拉选择。此页面上还有一个按钮可将帖子保存到收藏夹中。有什么方法可以在下拉菜单中点击最喜欢的按钮时,用下拉菜单中的当前选定值更新帖子的元信息?WordPress的 - update_post_meta从按钮点击当前下拉值?

update_post_meta(get_the_ID(), 'newcolor', $value); 

回答

0

当然。您需要在这里使用AJAX,这将允许您在不刷新此页面的情况下运行PHP脚本。

首先,你需要创建一个新的PHP脚本,称为add_favorite.php其中将包含:

$id = $_POST['id']; 
update_post_meta($id, 'newcolor', $value); 

然后你可以使用jQuery的从$的阿贾克斯()函数异步调用这个脚本并更新数据库中的后期元。返回时,您可以使用JavaScript更改HTML的特定元素以反映更改(添加到收藏夹)。

request = $.ajax({ 
    url: "add_favorite.php", 
    type: "post", 
    data: // send the id here 
}); 

// Callback handler that will be called on success 
request.done(function (response, textStatus, jqXHR){ 
    console.log("Hooray, added to favorites!"); 
}); 

看到这个答案,了解如何发送AJAX数据: jQuery Ajax POST example with PHP

这是jQuery的API文档。数据可以是字符串或数组类型。使用数组,如果多个值[ 'ID'=> $ ID]:

data 
Type: PlainObject or String or Array 
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below). 

http://api.jquery.com/jquery.ajax/