0

For some reason when I submit the form it says Message: json_encode() expects parameter 2 to be long, string given on this line of the controller and not sure why. Does anyone see what the issue is?

Controller

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 library

/**
* 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);
}
1
  • You should really echo the json in a view, this goes against the MVC pattern. Commented Feb 17, 2012 at 13:24

1 Answer 1

2

Well yes, the second parameter to json_encode is supposed to be a bitmask of options, not a message. You can only encode one thing at a time, not a string and an array. Maybe you want something like json_encode(array($status, $message))?

Sign up to request clarification or add additional context in comments.

2 Comments

Where its supposed to displace the message.
That's too vague. You cannot encode a message and a status this way using json_encode, period. What do you expect the output to be? Whatever happens to the message output is an entirely different topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.