Welcome Guest, Not a member yet? Register   Sign In
Code Igniter and Jquery Validation Plugin
#1

[eluser]Xtrex[/eluser]
I, I cant resolve this problem..
I am using CI with Jquery validation plugin. I have this code with jquery:

To validate the form I need to check in a DB id the username does't exist, so I am trying to use: (the remote use AJAX)
Code:
email: {
    required: true,
    remote: "<?php echo base_url()?>register/checkemail"
}

But it dont work.. but if I use a CI external file, connecting to the DB it work, like:

Code:
email: {
    required: true,
    remote: "<?php echo base_url()?>includes/emails.php"
},

In CI I use this controller:
Code:
// Controller
function checkemail()
   {
  $this->load->database();
  $this->load->model('check');
  $this->check->atribuido('membros','email',$_REQUEST['email']);
}
// Model
function atribuido($tabela,$atributo,$campo)
    {
        $query = $this->db->query('SELECT * FROM '.$tabela.' WHERE '.$atributo.' = "'.$campo.'"');
        if ($query->num_rows() > 0){
            echo true;
        } else {
            echo false;
        }    
}

The model and the controller works fine!

Help me please, dont know what to do..
#2

[eluser]jnorris441[/eluser]
One says checkuser and the other one says checkemail. Is that the problem?
#3

[eluser]Xtrex[/eluser]
nop, my mistake.. the problem is not simple errors, I tryed everything, I cant do it.. Sad
#4

[eluser]jnorris441[/eluser]
You aren't doing anything with the return value from the model?

EDIT: Sorry I see where you are echoing from the model.
#5

[eluser]marcoss[/eluser]
Don't echo from the Model, use return instead.

Code:
// Model
function atribuido($tabela,$atributo,$campo){
        $query = $this->db->query('SELECT * FROM '.$tabela.' WHERE '.$atributo.' = "'.$campo.'"');
        return ($query->num_rows() > 0) ? true : false;
}

The in your controller output a VALUE to be checked by JS, like this:

Code:
// Controller
function checkemail(){
  $this->load->database();
  $this->load->model('check');
  echo ($this->check->atribuido('membros','email',$_REQUEST['email'])) ? 1 : 0;
}

As a side note, you should output the value through a view, not directly from the controller.
#6

[eluser]Xtrex[/eluser]
it doest work Sad I dont know what to do, I tryied everything!!

this a example of emails.php that works:

Code:
<?php
$request = trim(strtolower($_REQUEST['email']));
$emails = array('[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]');
$valid = 'true';
foreach($emails as $email) {
    if( strtolower($email) == $request )
        $valid = "false";
}
echo $valid;
?>

but even if I pass it to the controller checkemail, to test, it dont work..
#7

[eluser]Pascal Kriete[/eluser]
[quote author="Xtrex" date="1217027058"]
Code:
function atribuido($tabela,$atributo,$campo)
    {
        $query = $this->db->query('SELECT * FROM '.$tabela.' WHERE '.$atributo.' = "'.$campo.'"');
        if ($query->num_rows() > 0){
            echo true;
        } else {
            echo false;
        }    
}
[/quote]

The ajax request gets the string output, not the value. Echoing false doesn't return anything, and echoing true just returns 1. Try echoing the string instead.

Since this request doesn't need anything else, you can make debugging easier by killing the script to avoid any unexpected output.
Code:
if ($query->num_rows() > 0)
{
    exit('true');
}
else
{
    exit('false');
}
#8

[eluser]Xtrex[/eluser]
dont work Sad I use a ajax request in the form submit and it works, maybe its something with the jquery validate plugin in conflit with CI..
#9

[eluser]Pascal Kriete[/eluser]
Oh silly me, I remember this problem from another thread.

The validation script uses GET to send the values - CI kills the $_GET array. I'm pretty close to implementing this myself, so when I do I'll post back with the required changes.
#10

[eluser]Xtrex[/eluser]
thank you Smile




Theme © iAndrew 2016 - Forum software by © MyBB