Welcome Guest, Not a member yet? Register   Sign In
Simple ajax call page??
#1

[eluser]Guest[/eluser]
Hi guys,

I've hit a bit of a problem - i'm jquery validation to call a php page to check if an email is in the database.

usally, i would just use this:

validate.js

Code:
username: {
                required: true,
                minlength: 4,
                remote: "../ajax/userchk.php"
            },

then in ajax/userchk.php page:

Code:
<?php
$request = trim(strtolower($_REQUEST['username']));

require_once('../database.php'); // get's database details

    $request = db_input($request);
    $query = db_query("SELECT userID FROM users where username = '" . $request . "'LIMIT 0 , 1 ");
    $found = 0;
    while($suggest = db_fetch_array($query)) {
        $found = $suggest['userID'];
    }
    $valid = 'true';
    if ($found !=0){
        $valid = 'false';
    }
echo $valid;
?>

This works really great on other sites i've done, but i do not know how to do it in codeigniter.

I have tried the same validate.js..

Then created a folder in the root of the site, called "ajax", and made this:

Code:
<?php
    function index()
    {
            $request = trim(strtolower($_REQUEST['username']));
            $query = $this->db->query('SELECT userID FROM users WHERE userName = "'.$request.'"');
            if ($query->num_rows() > 0){
                echo 'true';
            } else {
                echo 'false';
            }    
    }
?>

But it doesnt work :-(

I think the js file is connecting to the php file, as i'm getting an error_log file, so the path is ok. I'm just doing somthing wrong. Error log is this:

Quote:PHP Fatal error: Using $this when not in object context in /home/username/public_html/sitename.com/ajax/userchk.php on line 3


Any help getting this working would be amazing.

Many thanks.
#2

[eluser]mcr_rm[/eluser]
Is this your full controller?

If so you need to extend the base controller to pass in the $this->db object:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Ajax extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {

    }

}

Then you put your code in but I am pretty sure you need to tweak $_RQUEST as super globals aren't allowed and neither are $_GET. Is the jQuery validation script using $.post? if not you could write your own or amend it quite easily.
#3

[eluser]kgill[/eluser]
Few things:

1st, ensure jquery is using POST not GET to send data to the server.
2nd, your getting the error because you're using object references in a file that's just a function, so this-> anything won't work
3rd, to get at the CI db stuff, you need to use a controller so instead of using something.php in your validate.js you want the remote to be your_controller/your_validation_function
#4

[eluser]Guest[/eluser]
Thanks so much guys! With your help, i got it working :-)
(There are some problems, but i will explain them at the end)

I thought id show exactly what i did, in case anyone else ever finds this post and needs to work it out.

In my validate script, i have the following:

Code:
required: true,
                minlength: 4,
                remote: "../ajax/checkusername"
            },

then, in the ajax.php file, located in SITE\system\application\controllers

i have this:

Code:
<?php
//if (!defined('BASEPATH')) exit('No direct script access allowed');
class Ajax extends Controller {

    function index()
    {
        echo 'false';
    }

    function checkusername()
    {
        
        $request = strtolower($_REQUEST['username']);
        
        $query = $this->db->query('SELECT userID FROM users WHERE LOWER(userName) = "'. $request .'"');
        if ($query->num_rows() > 0){
            echo 'false';
        } else {
            echo 'true';
        }
        
        
    }
    
}
?>

It really is great :-), as i only have to have 1 ajax.php file -- and then just add more functions as i need them, and call them like this ../ajax/checkemail or ../ajax/otherfunction instead of the 100's of little ajax files i used to do.

I had to change one line in the jquery.validate.js plugin file, and that was at line 932, i had to add type: "POST",

Code:
$.ajax($.extend(true, {
                    url: param,
                    mode: "abort",
                    type: "POST",


Thanks kgill and mcr_rm :-) great advice.

I just have two problems now...

I had to comment out the line to check if the file is being loaded directly or not, as it stops it working...

Code:
//if (!defined('BASEPATH')) exit('No direct script access allowed');


This dosnt really matter, though.

Another problem was that i had to disable compression in my config.php file...

Code:
$config['compress_output'] = FALSE;

Otherwise i was getting an error.

I did read there was another way to turn on compression in the php.ini file -- so i will have to look into that (unless anyone wants to post the answer?)

Thanks again for the help!

Regards,
#5

[eluser]mcr_rm[/eluser]
The first thing you commented out only stops people viewing the file directly if you have it in a public folder however it's not essential. Secondly I would put the constructor back in so you get the full methods from the base controller loaded.

If you don't have php 5 (for construct)

then use

Code:
public function ajax()
    {
        parent::Controller();
}

before your index function.

Anyways glad it's sorted for you!




Theme © iAndrew 2016 - Forum software by © MyBB