Home » Jquery » php – Ajax working successfully but not posting data to controller in codeigniter

php – Ajax working successfully but not posting data to controller in codeigniter

Posted by: admin February 22, 2020 Leave a comment


I’m facing a weird situation. I’ve an ajax post method in a view and I’m trying to post some data in the controller. But, it isn’t bringing the controller page.
Here is the view :

<!DOCTYPE html>
  <title>Insert student info</title>
  <link rel = "stylesheet" href="<?php echo base_url('bootstrap/css/bootstrap.min.css') ?>">
  <script type = "text/javascript" src="<?php echo base_url('/js/jquery-3.js') ?>" > </script>

    <button id = "upload" value = "paaapiiiii">Press</button>

<script type="text/javascript">  

  $('#upload').click(function() {
    var h = $('#upload').val();
                url: "<?php echo base_url();?>/Result_Controller/testing_purpose",
                type: 'POST',
                data: {h:h},
                dataType: 'html',    
                success: function(data) {


Here is the controller:

public function testing_purpose() {
                $value = $_POST['h'];
                echo 'hii';

Here, it is alerting the values inside alert() just fine, even inside the success function. But it isn’t echoing the value which is in the controller’s testing_purpose().
What makes it more weirder is that my other view page’s ajax methods are working perfectly. The problem is causing just in this single view page. So,it is obvious that my controller is fine.
Another interesting thing is, if I open firefox and go to console, then for clicking the view page’s button,a link for the controller method appears at the right side of the console. If I click it, it shows,
My jquery path is totally correct. To get rid of this problem, I’ve already uninstalled my xampp and reinstall it just yesterday but in vain. My php version is 7.4.2.
Please, please, someone help me. This problem has started making me to believe in invisible spirits.
Thanks in advance…

How to&Answer:

First off add any element with id content like

<div id='content'></div>

Here is your controller method

public function testing_purpose() {
                    $value = $_POST['h'];
                    echo 'hii';

Replace with this

public function testing_purpose() {
                        $value = $_POST['h'];
                        echo 'hii';
                        echo '<h1>Testing</h1>';

Try this and post your comment. Thanks