dongzhi8984 2012-04-11 13:06
浏览 37

如何在PHP中使用return获取响应并在jquery中使用它

The following jquery is to check if a user name and email in the database. When I echo false or ture, jquery works but it gives PHP error. So I just want to return false or true from PHP code. But when I do it jquery does not work.

What am I doing wrong here?

Thanks in advance.

Jquery

$(document).ready(function(){
$("#name").blur(function()
{
    var nameval = $(this).val();
    if(nameval)
    {
        ...
        ...
        //check the username exists or not from ajax
        $.post("<?php echo site_url('welcome/user_availability'); ?>",{ what:$(this).val(), where:"name" } ,function(data)
        {   
            if(data==false) //if username not avaiable
            {
                $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
                {
                //add message and change the class of the box and start fading
                $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
                });
            }
            else
            {
                $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
                {
                    //add message and change the class of the box and start fading
                    $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
                });
            }

        });
    } 
    else
    {
        $("#msgbox").fadeTo(200,0);
    }       
});
 ...
 ...

PHP

function user_availability()
{
    $module = "subscribers";
    $where = $this->input->post('where');
    $what = $this->input->post('what');
    $customers = $this->MKaimonokago->getAllSimple($module, $where , $what );
    if (!empty($customers))
    {
     //user name is not available
        return false; // I want to use return here.
       // this works but it gives php error
       // echo "false";
    }
    else
    {
        return true;
    }    
}

PHP error

ERROR - 2012-04-11 21:43:06 --> Severity: Warning  --> Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/kaimonokago2.0/application/modules/welcome/controllers/welcome.php:1084) /Applications/MAMP/htdocs/kaimonokago2.0/system/core/Output.php 391
  • 写回答

2条回答 默认 最新

  • doushi2047 2012-04-11 13:08
    关注

    try:

    // in php
    echo(json_encode(TRUE));
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!