Sending Ajax Information to Controller

Asked

Viewed 159 times

-1

Good morning guys, all beauty?

I am making an application where my select will operate what will appear on my page. I am using Codeigniter, so the choice of select goes to a controller. I need my page not refresh!

I’m using Ajax to try to get around this situation, but my first visit to him was yesterday, I’m very lay yet.

What happens is:

I want to take the value of select -> play this value in the controller -> and bring the data of my Query check in db to view.

Html:

 <form real="form" id="User" nome="form" method="post" action='<?= base_url("index.php/welcome");?>'>
                  <select class="form-control" name="selectUser" id="seletor">
                     <option  value='<?= $_SESSION["nome"]?>' >Only me</option>
                     <option  value="" >Team</option>
                  </select>
                  </form>

Controller:

public function index()
	{	
		
		$nome = $this->input->post('selectUser');
		echo $nome;
		$this->load->model('testemodel');

		$verifica= $this->testemodel->verifica($nome);

		$data['nomecliente'] = $verifica['nome_cliente'];
		$data['horatotal'] = $verifica['hora_total'];
		$data['atividade'] = $verifica['nome_atividade'];
		
	
		
	

		$this->load->view('test', $data);
		
		
	}

Ajax:

(function () {
    document.getElementById('seletor').addEventListener('change', function() {
        var selectedOption = this.children[this.selectedIndex];

        var value = this.value;
        
        $.ajax({
          type: "post",
          url: "http://localhost/Time/index.php/welcome/index",
		  data: value,
		  dataType: 'json',
		  
		  
        });
       
       
    })
})();

I thank you for any north!

1 answer

0

According to my new understanding of the answer you need that by changing the option of a select, the value of option be sent to your controller. In case I’m wrong correct me.

Example:

<body>
    <select class="form-control" name="selectUser" id="seletor">
        <option  value='<?= $_SESSION["nome"]?>' >Only me</option>
    </select>

    <script>
        $("#seletor").change(function(){
            $.ajax({
                type: "post",
                url: "LINK_PARA_O_CONTROLLER",
                data: {"seletor": $("#seletor").val()},
                success: function(data){

                    console.log(data);
                },
                error: function(){
                    console.log("Erro na requisição")
                }
            });
        });
    </script>
</body>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.