我在使用jQueryPost时遇到了一个问题,PHP将页面的所有HTML返回到新创建的HTML,而不仅仅是PHP输出的HTML。
例如php输出:'<div>Some Content</div>'
然后jQuery Post返回:'<html><head>...all the head content...</head><body>...other content...<div>Some Content</div>'
下面是jQuery(链接到完整代码:http://pastebin.com/U7R8PqX1):
jQuery("form[ID^=product_form]").submit(function() {
var current_url = js_data.current_url;
form_values = jQuery(this).serialize();
jQuery.post(current_url+"?ajax=true", form_values, function(returned_data) {
jQuery('div.shopping-cart').html(returned_data);
}
});
return false;
});
下面是PHP(5.3.6版-完整代码链接:http://pastebin.com/zjSUUbmL):
function output_cart()
{
ob_start();
echo $this->output_cart_html();
$output = ob_get_contents();
ob_end_clean();
echo $output;
exit();
}
function output_cart_html() {
if (!isset($_SESSION['cart_items']))
{
$output = '<div class="cart_content faded">BASKET - Empty</div>';
return $output;
} else {
$total_items = 0;
$total_items = 0;
$items_in_cart = $_SESSION['cart_items'];
// work out total price and total items
foreach ($items_in_cart as $item_in_cart) {
$total_items += $item_in_cart['quantity'];
$total_price += floatval($item_in_cart['updated_price']*$item_in_cart['quantity']);
}
$template_url = get_bloginfo('template_directory');
$output = '';
$output_price = $dp_shopping_cart_settings['dp_currency_symbol'].number_format($total_price,2);
if($total_items == 1){ $item_text = ' Item'; } else { $item_text = ' Items'; }
$output .= $total_items . $item_text;
$output .= ' <span class="bullet"></span> Total '.$output_price;
// empty cart btn
$output .= '<form action="" method="post" class="empty_cart">
<input type="hidden" name="ajax_action" value="empty_cart" />
<span class="emptycart"> <a href="'.htmlentities(add_query_arg("ajax_action", "empty_cart", remove_query_arg("ajax")), ENT_QUOTES).'"><img src="'.$template_url.'/images/empty.png" width="9" height="9" title="Empty Your Items" alt="Empty Your Items" />Empty Your Cart</a></span>
</form>';
// check out btn
$output .= ' <span class="bullet"></span> <span class="gocheckout">'.$this->output_checkout_link().' </span>';
return $output;
}
}
您需要检查表单是否已使用PHP发布。要做到这一点,只需检查"ajax"参数是否存在,或者如果愿意,发送另一个$_GET变量(通过在jQuery post URL的末尾添加&anotherparementer=1
)。示例:
<?php
if(isset($_GET['ajax'])) {
//your PHP code here that submits the form, i.e. the functions that you have, etc.
}
else {
?>
<html>
<head>
head content here...
</head>
<body>
body content here...
</body>
</html>
<?php
}
?>
我希望这能有所帮助。
好吧,原来我的问题是Wordpress处理AJAX请求的方式。我在上面构建的插件使用AJAX,但没有这些问题(我不知道为什么,可能是因为他们使用的是eval),所以我没有意识到有一种正确的方法可以将AJAX与Wordpress一起使用。如果其他人也有类似的问题,这里有一堆信息:
http://codex.wordpress.org/AJAX_in_Plugins
http://byronyasgur.wordpress.com/2011/06/27/frontend-forward-facing-ajax-in-wordpress/(这个真的帮了我一把,一个我能够适应的非常简单的例子)
--很抱歉,我不能发布更多的链接,因为我在这个网站上太新了,但请查看上面第一个链接底部的链接,尤其是"5个提示"。
当我使用类时,我从插件的索引文件中实例化了这个类:
if ($_POST['action'] === 'action_name') {
$class_obj = new 'namespace_name'class();
add_action('wp_ajax_action_name', array($class_obj, 'method_name'));
add_action('wp_ajax_nopriv_action_name', array($class_obj, 'method_name'));
}