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);
} );
}