2012-02-16 73 views
0

由于某种原因,当我提交表单时,它说消息:json_encode()期望参数2很长,字符串在控制器的这一行给出,并且不知道为什么。有没有人看到这个问题是什么?Json编码问题

控制器

function forgot_password_submit() 
{ 
    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); 

    if (!$this->form_validation->run()) 
    { 
     $this->kow_auth->output('There was a problem submitting the form! Please refresh the window and try again!', FALSE); 
     return; 
    } 

    $user_data = $this->users->get_user_by_username($this->input->post('username')); 
    if ($user_data === NULL) 
    { 
     $this->kow_auth->output('User does not exist in the database!', FALSE); 
     return; 
    } 

    $already_sent_password = (isset($user_data->new_password_key) && isset($user_data->new_password_requested)); 
    if ($already_sent_password) 
    { 
     $this->kow_auth->output('Check your email for your temporary password!'); 
     return; 
    } 

    if (!strtotime($user_data->new_password_requested) <= (time() - 172800)) 
    { 
     $this->kow_auth->output('You have to wait 2 days before a new temp password can be emailed!', FALSE); 
    } 
    else 
    { 
     if ($this->kow_auth->forgot_password($this->input->post('username'))) 
     { 
      $this->kow_auth->send_email('forgot_password', 'KOW Manager Forgot Password Email', $user_data); 
      $this->kow_auth->output('A temporary password has been emailed to you!'); 
     } 
     else 
     { 
      $this->kow_auth->output('A temporary password could not be created for you!', FALSE); 
     } 
    } 
} 

kow_auth库

/** 
* Generate output message 
* 
* @param string 
* @return object 
*/ 
function output($message, $success = TRUE) 
{ 
    $status = $success ? array('succes' => 'yes') : array('error' => 'yes'); 
    echo json_encode($status, $message); 
} 
+0

你应该真的在视图中回显json,这违背了MVC模式。 – 2012-02-17 13:24:14

回答

2

嗯,是,到json_encode第二个参数应该是一个选项位掩码,不是一条消息。一次只能编码一个东西,而不是字符串一个数组。也许你想要类似json_encode(array($status, $message))

+0

但它不显示消息。它显示未定义。 – 2012-02-16 00:24:00

+0

哪里显示undefined? – deceze 2012-02-16 00:24:42

+0

它应该取代信息的地方。 – 2012-02-16 00:26:11