skip to Main Content

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


  1. Chosen as BEST ANSWER

    i've change my code, controller : enter image description here

    jquery : enter image description here

    but when i click print button. and new tab showed variable NULL. enter image description here

    can someone tell me, and fix this code?


  2. 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:

    $("printButton").click(function(){
        var limit = $("#per_page_pendapatan").val();
        var page = <?php echo $this->uri->segment(3) ? $this->uri->segment(3) : 1; ?>;
        
        $.post(
            "<?php echo base_url('index.php/Pendapatan/process_pdf'); ?>",
            {"limit": limit, "page": page},
            function(data) {
                console.log(limit);
                console.log(page);
                alert("Data berhasil dikirim ke controller");
            }
        ).fail(function(data) {
            alert("Data gagal diterima");
        });
    });
    

    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.

    Login or Signup to reply.
  3. (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:

    1. 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.

    2. 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.

    3. 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.

    4. 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.

    5. 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(); ?>";

      $.post(
          "<?php echo base_url('index.php/Pendapatan/process_pdf'); ?>",
          {"limit": limit, "page": page, "<?php echo $this->security->get_csrf_token_name(); ?>": csrfToken},
          function(data) {
              console.log(limit);
              console.log(page);
              alert("Data berhasil dikirim ke controller");
          }
      ).fail(function(data) {
          alert("Data gagal diterima");
      });
      

    Replace "<?php echo $this->security->get_csrf_hash(); ?>" with the actual way you retrieve the CSRF token in your application.

    1. Check CodeIgniter Configurations:
      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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search