简体   繁体   English

我无法通过PHP语言上的Ajax脚本发送完整数据。 (代码点火器)

[英]I can't send full data through ajax script on php language. (code igniter)

<script language='javascript' type='text/javascript' charset='<?=$page_charset?>'>
    $(document).ready(function(){

        $('#btn_login').click(function(){
            $.search_keyword();
        });
    });

here is the script 这是脚本

<form name='frm_search_keyword'>
<table style='width:250px;'>
    <tr>
        <td style='width:100px;'>
            Search 
        </td>
        <td>
            <input type="text" name="web_keyword" />
        </td>
        <td>
            <input type="submit" id="btn_login" name="btn_login" value="search!" />
        </td>
    </tr>
</table>
</form> 

here is the form 这是表格

search_keyword:function(type)
{
    $.ajax({
        type: 'POST',
        url: '/web_keyword',
        data: {'b_type':type},
        cache: false,
        async: false,
        success: function(result){
            if(result == 12001)
            {
                alert('please choose your interest.');
                location.href = '/account/interest';
            }else
                location.href = '/'+type+'/'+result;
        }
    });
}

It successfully sends 'web_keyword' to db query and get result. 它成功发送“ web_keyword”到数据库查询并获得结果。 but I can't get type data through ajax script. 但是我无法通过ajax脚本获取类型数据。 Can you help me to 'type' data from the form table to ajax script? 您能帮我将数据从表单表“键入”到ajax脚本吗?

Thank you. 谢谢。

Try adding 尝试添加

" dataType: 'json'," like 


   $.ajax({
        type: 'POST',
        url: '/web_keyword',
   dataType: 'json',
        data: {'b_type':type},
    });

and return the date using 并使用返回日期

$data['return1']='true';
echo json_encode($data);


         success: function(result){
            if(result.return1 == 12001)
            {
                alert('please choose your interest.');
                location.href = '/account/interest';
            }else
                location.href = '/'+type+'/'+result;
        }

you should remove async:false 您应该删除async:false

Setting this option to false is strongly discouraged, as it can cause the browser to become unresponsive. 强烈建议不要将此选项设置为false,因为它可能导致浏览器无响应。

I'm not exactly sure if this is the problem, but it is suspect: 我不确定这是否是问题,但是有疑问:

 $('#btn_login').click(function(){
        $.search_keyword();
    });

Where is "type" supposed to come from? “类型”应该从哪里来? What is "type" referring to? 什么是“类型”? Is "type" a value returned from your DB query? 从数据库查询中返回的是“类型”值吗? Or is it something selected by the user when they perform the search? 还是用户执行搜索时选择的东西?

If type is something within your form element, then use Javascript or jQuery, or whatever, to "harvest" that value from the page, and then you will need to pass that data to the AJAX functionality. 如果type是表单元素中的内容,则使用Javascript或jQuery或其他任何方法从页面中“获取”该值,然后您需要将该数据传递给AJAX功能。

$('#btn_login').click(function(){
        //First get the "type" value, for example if "type" is retrieved from the form element
        var type = $('#btn_login').attr('type'); //this is for example's sake, since you did not assign an id to this form element...
        search_keyword(type);
    });

You just need to get "type" from where ever it is being generated and/or stored and pass it to your AJAX function. 您只需要从生成和/或存储它的任何地方获取“类型”,然后将其传递给您的AJAX函数即可。

Also, maybe this is irrelevant, but your "search_keyword()" function definition looks odd to me... 另外,也许这无关紧要,但是您的“ search_keyword()”函数定义对我来说似乎很奇怪。

Instead of: 代替:

search_keyword:function(type){...}

Should be: 应该:

search_keyword = function(type){...}

One final thing, could you tell us which Javascript library you are using? 最后一件事,您能告诉我们您使用的是哪个Javascript库吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM