0

I'm working on a signup form using jQuery's AJAX and Codeigniter PHP framework, but the codeigniter doesn't matter that much, if you see weird echo form_open(); or other weird things, don't worry, they're all codeigniter.

Okay, here's my problem with the code. I tried simple post using jquery below (in the php code everything else is omitted and added a simple echo 'hello, world!'; function, and it works / giving me a hello, world!):

 $.post( 'ajax_signup_username' ,function(result){ $(span_error).html(result); } ); 

But when I passed a post data in it, it doesn't work (doesn't output any result,note that I'm using the full PHP code written below-below):

$.post( 'ajax_signup_username', {php_username_error:username_value} ,function(result){ $(span_error).html(result); } ); 

I've tried searching SO, but the solutions provided didn't solve this. Any idea what's wrong?

Here are the full codes:

Following is the html code:

<body> <?php $attr = array( 'onsubmit' => 'return final_val()' ); echo form_open('formValidation/signUp',$attr); ?> <p>Username: <input type="text" onBlur="username_val()" value="<?php echo set_value('username')?>" name="username" maxlength="50" /><span name="username_error"><?php echo form_error('username'); ?></p></span> <p>Email: <input type="text" onBlur="email_val()" value="<?php echo set_value('email')?>" name="email" maxlength="50" /><span name="email_error"><?php echo form_error('email'); ?></p></span> <p>Password: <input type="password" onBlur="pass_val()" value="<?php echo set_value('pass')?>" name="pass" maxlength="50" autocomplete="off"/><span name="pass_error"><?php echo form_error('pass'); ?></p></span> <p>Password Confirm: <input type="password" onBlur="pass_val()" value="<?php echo set_value('passconf')?>" name="passconf" maxlength="50" autocomplete="off" /><span name="passconf_error"><?php echo form_error('passconf'); ?></p></span> <p><input type="submit" value="SIGN UP!" name="signup"/></p> </form> </body> 

Following is the php code:

function ajax_signup_username(){ $username = $_POST['php_username_error']; $uniqueUn = $this->db->prepare("SELECT * FROM iowfiwefwmember WHERE username=:username"); $uniqueUn->bindParam(':username', $username); $uniqueUn->execute(); $count1 = $uniqueUn->fetch(); if( $count1 == FALSE ){ echo 'USERNAME AVAILABLE!'; }else{ echo 'USERNAME IS ALREADY USED!'; } } 

Following is the javascript code:

function username_val(){ $.ajaxSetup({ cache : false }); username_value = $("input[name='username']").val(); span_error = "span[name='username_error']"; //$(span_error).load('ajax_signup_username', {php_username_error:username_value}); <-- also doesn't work [ $.ajaxSetup() is set to POST ] $.post( 'ajax_signup_username', {php_username_error:username_value} ,function(result){ $(span_error).html(result); } ); } 
11
  • Do you get a JavaScript error? What happens when you make a post request to the URL directly? Commented May 24, 2012 at 16:07
  • Also please don't bother the PDO codes, the point is that after sending a post data in the parameter, it doesn't work. Commented May 24, 2012 at 16:07
  • @ExplosionPills The web developer toolbar in firefox gave me a POST Server Error, but I tried navigating the code in the browser and it exists. Commented May 24, 2012 at 16:09
  • how are you routing the url 'ajax_signup_username' to the function? Is this some CodeIgniter magic? Commented May 24, 2012 at 16:10
  • @PHPForLife navigating in the browser is not enough .. you have to make the same post request using the browser or some other user agent. Commented May 24, 2012 at 16:11

1 Answer 1

1

After long hours of doing dumb faces in front of my laptop, I found out that this IS a codeigniter issue, I turned on CSRF protection which caused the problem. After turning off CSRF feature, it works just fine, make sure not to repeat the same mistake, youngsters! Cheers! :D

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

1 Comment

I wouldn't recommend CodeIgniter for new developments though. Since Ellislab decided not to support it anymore, it took almost 2 years for some bug fixes be realeased from version 2.1.3 to 2.1.4. But yeah, for me the CSRF features also caused problems.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.