This question already has an answer here:
View:
<table>
<tr>
<td id="percentage">Percentage: <?php echo $percentage; ?> %</td>
</tr>
</table>
<div class="box-footer" style="float: right">
<a href="<?php echo base_url(); ?>student/Examinations" class="btn btn-primary" style="font-size: 20px;" id="submit">Submit</a>
</div>
<script>
$(document).ready(function () {
var percentage = $("#percentage").text();
console.log(percentage); //Percentage: 28.57 %
$("#submit").click(function () {
$.ajax({
type: 'POST',
url: base_url + "student/Examinations",
data: {percentage: percentage},
success: function () {
}
});
});
});
</script>
Controller:
class Examinations extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index() {
$percentage = $_POST["percentage"];
var_dump($percentage); // var_dump null
}
}
MY Question: javascript variable(percentage) to pass controller , and var_dump is null why?please help me.....................................
</div>
Not a user of code igniter myself, but using other frameworks, you better use the request object.
I quickly perused code igniter documentation, and apparently you should do something like that :
$percentage = $this->input->post('percentage');
It behaves like that because You are doing 2 things here. It looks like that:
id="submit"
$(#submit).click()
is executed:percentage
is executedclick
event)"<?php echo base_url(); ?>student/Examinations"
with no percentage
parameterIf You want to send param just modify Your href like that: "<?php echo base_url(); ?>student/Examinations?percentage=sample_value"
and check in target script $_GET['percentage']
you have not prevented execution of anchor tag, you have to prvent event of a tag
$(document).ready(function () {
$("#submit").click(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: base_url + "student/Examinations", // or you can take $("#submit").attr('href');
data: {percentage: $("#percentage").text()},
success: function(data) {
console.log(data);
}
});
return false;
});
});