CodeIgniter - Can't pass javascript value to controller

Hello.

I am fairly new to javascript, and I have this code where I am trying to pass a value to the controller from the view using Javascript. Any ideas why doing a vardump() in the controller is not displaying anything? Thanks

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script language="javascript">

  $(document).ready( function() 
  {
    $('#submitbutton').click( function()
    {
      $.ajax(
        {
                url: "../../index.php/testcontroller/getdatabasedata",
                data: { 'searchvalue' : $('#input').val() },
                method: 'GET'
            })
      });
    });  

    </script>`

<div class="col-sm-4">

            <form class="form">
              <div class="form-group">
                <label>Find Database Rows</label>
                <input class="form-control" type="text" id="input" placeholder="">
              </div>
              <div class="form-group">
                <input class="form-control" type = submit id="submitbutton" value="Submit">
              </div>    
            </form>
      </div>

Controller function

public function index(){
		$this->load->view('testview');
		
	}

	public function getdatabasedata(){

		$this->load->model('testmodel');
		$valuetosearch = $this->input->get('searchvalue');
		var_dump($valuetosearch);
	
	}

When running the application I am running this path: http://localhost/Codeigniter/index.php/testcontroller?

Why isn’t the JavaScript at the bottom just before the </body> tag where it belongs. That probably isn’t the cause of your problem but if you have the JavaScript correctly placed at the bottom of the page then a lot of potential problems with interactions between the script and the page can be ruled out.

I did put it at the bottom of the page before. Still same result… I am really stuck and it’s really frustrating I can’t understand why at least is not doing vardump() for the value that I am giving from the input field.

Managed to modify something: Here is the input I am getting:

ddd; } body { margin: 1em 10%; padding: 1em 3em; font: 80%/1.4 tahoma, arial, helvetica, lucida sans, sans-serif; border: 1px solid 999; background:

At a glance, everything seems fine. Are you sure you’re looking for the vardump response in the right place? Notice that your ajax doesn’t have a success handler, so the response is thrown away the moment it’s received.

EDIT: Also, your submit click handler doesn’t prevent default, so while the browser is in the middle of trying to make that ajax request, the form continues on with its normal submit, which would refresh the page and throw away the ajax request.

This may help because I have recently installed an Ajax search routine on a site and struggled with paths.

Try removing the “…/…/” from the **url: ** because “index.php/test…/” is in the same path as the calling Controller and not relative from the view path.

[off topic]. Tapped slowly from a tablet :frowning:

For someone looking for a similar answer to my problem.

Script

<script 
  src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script language="Javascript">
$(document).ready(function ()
{
  $("#submitbutton").bind("click",function() 
    { 
      var target_url = '<?php echo(base_url()."testcontroller/getdatabasedata") ; ?>';
      var data_to_be_sent = {'searchvalue' : $('#inputtext').val() }; 
      $.ajax ({

              url : target_url, 
              type: 'GET', 
              data: data_to_be_sent, 
              cache: false, 
              success: function(data_that_came_from_controller)
                { 
                  var dataarray = data_that_came_from_controller.split('#'); 
                  $('#question').html(dataarray[1]); 
                  $('#answer1').html(dataarray[3]); 
                }, 
            }); 
            return false; 
    }); 
});
</script> 

$(“#submitbutton”).bind(“click”,function() - click event should be between quotes

var dataarray = data_that_came_from_controller.split(‘#’); - splitting the data with a character. Character must match implode character function used in controller…

*$(‘#question’).html(dataarray[1]); *
$(‘#answer1’).html(dataarray[3]);

question, answer1 are id of divs in html.

Controller:

public function getdatabasedata(){

	$this->load->model('testmodel');
	$year = $this->input->get('searchvalue');
	$movie = $this->testmodel->findquestion($year); 
	$separated = implode("#", $movie);
	echo $separated;

}

$separated = implode(“#”, $movie); - the hastag charachter must mach the split function in the view.

Hoped this helped

That’s so prehistoric - why not try using a medieval type="text/javascript" at least - or better a modern type="application/javascript" instead.

That prehistoric version is about 20 years dead and the medieval one is only needed if you need to support JScript in IE8.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.