PHP jQuery Post - 返回太多HTML

时间:2012-01-30 21:25:22

标签: php jquery

使用jQuery Post时遇到问题,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 .= '&nbsp;<span class="bullet"></span>&nbsp; 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 .= '&nbsp;<span class="bullet"></span>&nbsp; <span class="gocheckout">'.$this->output_checkout_link().' </span>';

                return $output;

            }

    }

2 个答案:

答案 0 :(得分:3)

您需要检查表单是否已使用PHP发布。为此,只需检查'ajax'参数是否存在,或者根据需要发送另一个$ _GET变量(通过将&anotherparementer=1添加到jQuery post URL的末尾)。例如:

<?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
} 
?>

我希望这会有所帮助。

答案 1 :(得分:0)

好的,事实证明我的问题在于Wordpress处理AJAX请求的方式。我正在构建的插件是使用AJAX但没有遇到这些问题(我不确定为什么可能因为他们使用的是eval),所以我没有意识到有正确的方法在Wordpress中使用AJAX。如果其他人有类似的问题,这里有一堆信息:


http://codex.wordpress.org/AJAX_in_Plugins

http://byronyasgur.wordpress.com/2011/06/27/frontend-forward-facing-ajax-in-wordpress/ (这个真的帮助了我,一个我能够适应的非常简单的例子)

- 抱歉我无法发布更多链接,因为我在这个网站上太新了,但请查看上面第一个链接底部的链接,特别是&#39; 5提示&# 39;


当我使用类时,我使用以下方法从我的插件的索引文件中实例化了该类:

    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'));

}