【问题标题】:HTML Form sending values with jQuery Ajax to PHP使用 jQuery Ajax 向 PHP 发送值的 HTML 表单
【发布时间】:2016-09-16 08:11:43
【问题描述】:

我有这个 HTML 表单,它使用 jquery ajax 将输入值发送到 php。这个过程很顺利,但只有当我右键单击>检查元素>网络>标题时看到它。不幸的是,真正的页面保持静止。

有没有我做错了?请帮忙。谢谢大家,非常感谢您的帮助。

  • 我没有包含完整的代码,因为我认为我在 jquery ajax 上搞砸了,因为显然过程进展顺利并显示了正确的结果

HTML 表单

<div class="product-filter-type">
                    <input type="radio" class="checkFilter"  id="filterAllTV" name="typeTV" value="AllTV" checked><label class="filterType" for="filterAllTV">All TV</label><br/>
                    <input type="radio" class="checkFilter"  id="filterFHDTV" name="typeTV" value="FHD"><label class="filterType" for="filterFHDTV">FHD/HD</label><br/>
                    <input type="radio" class="checkFilter"  id="filter4K" name="typeTV" value="FOURK"><label class="filterType" for="filter4K">4K</label>
                </div>

JAVASCRIPT/JQUERY/AJAX

var type;


    $('input[name="typeTV"]').click(function(){
        if ($(this).is(':checked'))
        {
            type = $('input[name="typeTV"]:checked').val();
            sendType(type);
        }
    });



function sendType(type){

    $.ajax({ 
        type: 'POST', 
        url: "{{ base_url() }}product",
        data: { 
            'type' : type
        },
        success: {}
    }); 

PHP (FUELPHP)

public function action_product() {

    $this->set_meta_info($this->_meta_slug);

    $input_data = \Input::param();

    if(count($input_data) > 0){

        $this->_filter($input_data);

    }else{

        $this->_data_template['product'] = \Product\Productutil::get_product_data();
        $this->_data_template['AllTV'] = 'checked';

    }

    return \Response::forge(\View::forge('pages::frontend/product_list.twig', $this->_data_template, FALSE));
}

private function _filter($input_data) {

    $this->set_meta_info($this->_meta_slug);

    if (count($input_data) > 0) {

        $type       = isset($input_data['type']) ? $input_data['type'] : null;


        if ($type){

            $this->_data_template['product'] = \Pages\Filterutil::get_type_product($type);
            $this->_data_template[$type] = 'checked';

        }elseif { ... }

        return $this->_data_template;
    }

}

【问题讨论】:

  • 您期望发生什么?您的代码所做的只是发送 AJAX 请求,并且在回调中不做任何事情,因此 UI 中的任何内容都不会改变。
  • 定义你的问题?
  • 在您的开发工具中,您应该会看到一个 xhr 请求,主页不会更改其标题
  • @RoryMcCrossan:谢谢,是的,我之前注意到我必须在“成功:函数(){..}”中编写一些代码,对吗? .我之前在“成功:函数(数据){..}”中添加了这个,只是为了看看我得到了什么“var ok = JSON.parse(data); alert('Data: '+ok);" 但是有一些错误:VM2311:1 Uncaught SyntaxError: Unexpected token
  • 该错误意味着您调用的 URL 返回的是 HTML,而不是 JSON。检查控制台中请求的响应文本,以查看返回的确切内容。通常是 404 或 500 错误页面。

标签: php jquery json ajax fuelphp


【解决方案1】:

好的,我明白了。

我已经添加了

$this->_filter($input_data);
        return json_encode($this->_data_template['product']);

在 PHP 部分并添加,

success: function (response) {

            var json = $.parseJSON(response);

            for (idx= 0; idx< json.length; idx++) {

                ....

            }
        }
    }); 

谢谢大家,谢谢。如何结束这个问题?

【讨论】:

    猜你喜欢
    • 2020-03-12
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多