![]() |
Simple ajax call page?? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Simple ajax call page?? (/showthread.php?tid=26437) |
Simple ajax call page?? - El Forum - 01-13-2010 [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: { then in ajax/userchk.php page: Code: <?php 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 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. Simple ajax call page?? - El Forum - 01-13-2010 [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'); 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. Simple ajax call page?? - El Forum - 01-13-2010 [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 Simple ajax call page?? - El Forum - 01-14-2010 [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, then, in the ajax.php file, located in SITE\system\application\controllers i have this: Code: <?php 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, { 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, Simple ajax call page?? - El Forum - 01-14-2010 [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() before your index function. Anyways glad it's sorted for you! |