why this code always evaluate to true?

Discussion in 'jQuery' started by zoreli, Feb 22, 2013.

  1. #1
    Hi

    I have the following php

    public function checkEmail()
            {   
                // set the validation rules
                $this->form_validation->set_rules('checkemail', 'E-Mail', 'valid_email');
                $this->form_validation->set_error_delimiters('<br /><p class=jsdiserr>', '</p><br />');
                // if validation is passed
                if ($this->form_validation->run() !== FALSE)
                {
                    $ids=array();
                    $ids[0]=$this->db->where('email', $this->input->post('checkemail'));
     
                    $query = $this->backOfficeUsersModel->get();
                    if($query) {
                 
                        $data = array(
                            'userid'      => $query[0]['userid'],
                            'username'    => $query[0]['username'],
                            'password'    => $query[0]['password'],
                            'firstname'  => $query[0]['firstname'],
                            'lastname'    => $query[0]['lastname'],
                            'email'      => $query[0]['email']           
                        );
                       
                        $message = "#successMailMessage";
                       
                    } else {
     
                        $message = "#errorMailMessage";
             
                    }
                   
                    $output = '{ "message": "'.$message.'" }';
                    echo $output;
                        //  form validation has failed
                } else {   
                    $errorMessage = "Wrong email!";
                    //echo json_encode(array("success" => "false"));
                     
                }
            }  // end of function checkEmail 
    Code (markup):
    Now, here, if i output the output, result is correct, that is if the e-mail exists in the database the output is #successMailMessage, if not the output is #errorMailMessage

    Now, here is the javascript code:

     $("#formSendPassword").submit(function(e){   
              e.preventDefault();
              var email = $(this).find("#checkemail").val(); 
              var url = $(this).attr("action");
         
          $.ajax({
            type: "POST",
            url: $(this).attr("action"),
            dataType: "json",
            data: "email="+email,
            cache:false,
            success:
              function(output){
                console.log(output.message);
                $('#forgotPasswordForm').hide();
                $(output.message).fadeIn();
                // add those in the variable message somehow later
              }
            });
             
          return false;
          });
        
    Code (markup):
    Here, i always get successMailMessage in the console.log, hence the successMailMessage div fades in. If i disable the javascript, i get correct output in PHP
    Can anyone help me with this?

    Regards,zoreli
     
    zoreli, Feb 22, 2013 IP