1

我想检查客户端名称是否存在然后不创建它。
功能和每件事都运行良好。
我需要的是:函数向我的 ajax 函数返回 true 或 false。

我在 js 和 ajax 中的创建函数是:

function createClient(){
       var newClientForm = $$('newClientForm').getValues();
       var client_name = newClientForm.client_name;

       if (client_name != ''){
       url_call = window.server_name + "/index.php/clients/create_new_client/"+client_name; 
       url_call = url_call.replace(/\n/g, '|');
       $.ajax({
              url: url_call ,               
              success: function(data) {
                       if ((data==true)||(data==false)){
                            alert('data true');
                        }


              }     
        }); 
        }else{            
            dhx.alert({title:'Error', message:"Please enter the client name."});
        }
    };

和我在 cakephp 中的控制器

function create_new_client($string = null){
            $this->layout = 'default_really_empty';
            $string = urldecode($string);
            $explode_item = explode('^', $string);

            $client_name        = $explode_item[0]; 

            //--Sreach if the Client is already there don't create it
            $modelClassChild = 'Client';
            $this->loadModel($modelClassChild);                
            $objects=$this->$modelClassChild->find('all',array('conditions'=>array($modelClassChild .'.deleted'=>'0', 
                                                                                   $modelClassChild .'.name'=>$client_name)));
            foreach($objects as $object) { 
                $dbName = $object['Client']['name'];
            }                

            if ($client_name != $dbName){  
            //--Sreach if the Client is already there don't create it
                $clientArray = array();
                $fields = array();

                $clientArray['Client']['name']  = $client_name;
                $fields[] = 'name';

                $client_exists = FALSE;                    

                $this->Client->create();
                $this->Client->save($clientArray, true); 
           }else{
               $client_exists = TRUE;                   
           }
           $this->set('client_exists',$client_exists);
    }
4

1 回答 1

0

返回布尔值

if ($client_name != $dbName){  
    //--Sreach if the Client is already there don't create it
    $clientArray = array();
    $fields = array();

    $clientArray['Client']['name']  = $client_name;
    $fields[] = 'name';


    $this->Client->create();
    $this->Client->save($clientArray, true); 

    return false;                    

}else{
    return true;                   
}

检查ajax的成功回调为

success: function(data) {
    if ((data==0)||(data==1)){
        alert('data true');
    }
}
于 2012-10-19T11:13:05.067 回答