04-06-2015, 11:53 AM
Hi, i want to submit my form using jquery ajax, it is ok but just for first time i click submit, here is my code ,
my view that contains js :
and this is my controller :
i have enabled csrf protection in config.php
when csrf is disabled in config.php, every thing works fine, but after enabling that, my code works first time i clicking, and after that it doesn't works until i refresh page ,
how i can use ajax+csrf to submit forms in CI#?
thanks
my view that contains js :
Code:
<?php
echo form_open(site_url().'/user/ajaxreg' , array('class' => 'email', 'id' => 'form1'));
echo form_label('نusername : ');
echo form_input('username', set_value('username'));
echo form_label('password : ');
echo form_password('password' , set_value('password'));
echo form_label(' repassword : ');
echo form_password('repassword');
echo '<br/>';
echo form_submit('submit', 'Sign Up','id="submit"');
echo form_close();
?>
<script>
$(function(){
$("#submit").click(function(event){
event.preventDefault();
$.ajax({
url: "<?=site_url().'/user/ajaxreg';?>",
type:'post',
data : {
<?=$this->security->get_csrf_token_name();?> : "<?=$this->security->get_csrf_hash();?>" ,
'username' : 'username01'
} ,
success:function(result){
alert(result);
}
});
});
</script>
and this is my controller :
Code:
public function ajaxreg()
{
$data_post = $this->input->post(array('username','password','repassword'),TRUE);
var_dump($data_post);
}
i have enabled csrf protection in config.php
when csrf is disabled in config.php, every thing works fine, but after enabling that, my code works first time i clicking, and after that it doesn't works until i refresh page ,
how i can use ajax+csrf to submit forms in CI#?
thanks
