i have jquery looks like this :
“
$("printButton").click(function(){
var limit = $(#per_page_pendapatan).val();
var page = <?php echo $this->url->segmetn(3) ? $this->url->segment(3) : 1; ?>;
$.post({
url: "<?php echo base_url('index.php/Pendapatan/process_pdf'); ?>",
type : "post",
dataType : "json",
data : {"limit" : limit, "page" : page},
success : function (data){
console.log(limit);
console.log(page);
alert("Data berhasil dikirim ke controller");
},error : function(data){
alert("Data gagal diterima");
}
});
});
“
and controller who accept the data :
“
public function process_pdf(){
$limit = $this->input->post('limit');
$page = $this->input->post('page');
echo "limit".$limit.nl2br("n");
echo "page".$page;
}
“
the question is why my controller didn’t accept data?
3
Answers
i've change my code, controller :![enter image description here](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20572%20193'%3E%3C/svg%3E)
jquery :![enter image description here](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20812%20361'%3E%3C/svg%3E)
but when i click print button. and new tab showed variable NULL.![enter image description here](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20622%20237'%3E%3C/svg%3E)
can someone tell me, and fix this code?
It looks like there might be a couple of issues in your code that could be causing the problem.
Typo in jQuery Selector:
In your jQuery code, you’re missing the # symbol before per_page_pendapatan in the
$(#per_page_pendapatan)
selector. It should be$("#per_page_pendapatan")
.Syntax Error in
$.post
:The way you’re using
$.post
is incorrect. You should pass the URL as the first argument and the data as the second argument. Additionally, you are using the dataType parameter, which is unnecessary in this case because you are expecting JSON, and jQuery will automatically parse the JSON response. Here’s the corrected$.post code
:Also, make sure that your button element has the correct ID attribute, like
<button id="printButton">Print</button>.
URI Segment Function Name:
It seems like you have a typo in your segment function call. It should be
$this->uri->segment(3) instead of $this->url->segment(3).
Check Base URL:
Ensure that the base_url in
base_url('index.php/Pendapatan/process_pdf')
is correctly pointing to the URL of your CodeIgniter application.Check Controller and Method Names:
Double-check that the controller name (Pendapatan) and method name (process_pdf) in your URL match the actual names of your controller class and method.
If you’ve fixed these issues and your code still doesn’t work, please provide more details about any error messages you’re encountering or any additional relevant information.
(Am new here so i cant reply directly to ur comment so i just go ahead and add answers)
Sure, I’d be happy to help. If you’re still experiencing issues with your code and the variables are showing as NULL in your controller, there could be a few more things to check:
Check Your AJAX Request:
Make sure that the AJAX request is being triggered properly and that the correct values are being sent for limit and page. You can use browser developer tools (such as the Network tab) to monitor the network requests and check if the AJAX request is being made correctly.
Check jQuery Selector:
Confirm that the per_page_pendapatan element exists on your page and that the jQuery selector
$("#per_page_pendapatan")
is correctly selecting the element. If this selector doesn’t match any element, the limit value might be empty.Check Base URL:
Verify that the base_url in
base_url('index.php/Pendapatan/process_pdf')
is correct and points to the correct URL of your CodeIgniter application.Check Route Configuration:
If you’re using custom routes in your CodeIgniter application, make sure that the route for the Pendapatan/process_pdf URL is properly defined in your routes.php configuration file.
Check CSRF Protection:
If your CodeIgniter application has CSRF protection enabled, ensure that you’re sending the CSRF token along with your AJAX request data. You can do this by including the CSRF token as a data parameter in your AJAX request:
var csrfToken = "security->get_csrf_hash(); ?>";
Replace
"<?php echo $this->security->get_csrf_hash(); ?>"
with the actual way you retrieve the CSRF token in your application.Double-check your CodeIgniter configuration files (like config.php and autoload.php) to make sure that everything is set up correctly, including the base URL, URI protocols, and any other relevant settings.
If you’ve reviewed all these points and the issue still persists, it might be helpful to provide more specific details about any error messages or unexpected behaviors you’re encountering.
I am with you. we keep trying to we get it fix. Let me know the outcome