php - CSRF Protections with AJAX & FORM - CodeIgniter - Not Sending
In the beginning I turned off my CSRF just for simplicity reasons to make my form / ajax work. Now when I turned it on and tried to use the inputs and stuff for CSRF nothing will work. My form doesn't submit or when It does my whole page refreshes which I can't let that happen. Here is my current code.
form method="post">
<input type="text" name="vip_text_box" id="vip" value="<?php echo $total_amount ?>"> <br><br>
<input type="submit" id="submit" value="Redeem" onclick="return rebate_amount()">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
</form>
And the AJAX:
function rebate_amount(){
var value = document.getElementById('vip').value;
var datastring = 'vip='+value;
var url = base_url + '/index.php/home/redeeming_form_value';
$.ajax({
type : 'post',
url : url,
data: datastring,
cache: false,
success: function(html){
$('#vip_point_redeeming').html(html);
}
});
return false;
}
Keep in mind. This all works when CSRF is off in the config file. But I think theres a problem here with CSRF with form / ajax.
Answer
Solution:
You should also include
csrf_token
with data500 internal server error
Either you make
OR
Your function would look somewhat