Sending view string to controller via Ajax. Codeigniter, javascript and php

Asked

Viewed 195 times

0

I’m trying to send information that is in the javascript code block to my controller, but I’m not sure how to capture the information in my controller.

Follow view Javascript code:

<script type="text/javascript">
			fetch('http://localhost/page.com.br/public/id_notification', {
				method: "POST",
				id: 1515
			})
			.then(function(response) {
				return console.log(response);
			});			

</script>

Route file:

$route['id_notification'] = 'post_notification/insert_id_post_notification';

My controller:

<?php

class Post_notification extends CI_Controller
{
    public function insert_id_post_notification()
    {
        $teste = $_POST['id'];
        var_dump($teste);
    }
}

?>

Name of my controller: post_notification.

I want to capture the id with value 1515 in my controller. Test value Thank you very much, everyone.

  • Your fetch is wrong: https://googleweblight.com/i?u=https://github.com/github/fetch/issues/635&hl=en-BR take a look

  • Hello friend! really was incorrect, I changed and it was as follows

  • fetch('http://localhost/app.realizaconstructa.com.br/public/id_notification', { method: "POST", body: 1515 }) . then(Function(Response) { Return console.log(Response); });

  • Now I need to know how to capture this 1515 value in my controller :/

  • thus: fetch('localhost/app.realizaconstrutora.com.br/public/id_notification', { method: "POST", body: 'source=1515', headers: {&#xA; "Content-Type": "application/x-www-form-urlencoded"} }) .then(function(response) { return console.log(response); }); and then in your code you search for source so it will work in your backend...

5 answers

0

Passes the entire url without the route this way:

<script type="text/javascript">
                fetch('http://localhost/page.com.br/public/Post_notification/insert_id_post_notification', {
                    method: "POST",
                    id: 1515
                })
                .then(function(response) {
                    return console.log(response);
                });         

    </script>

Then in your Controller do the following

<?php

class Post_notification extends CI_Controller
{

    public function __construct() {
      parent::__construct();
    }
    public function insert_id_post_notification()
    {

        $teste = $this->input->post('id');
        var_dump($teste);
    }
}

?>
  • I put and Ajax is sending with status Ok, but by playing the Ajax url directly I can access the method insert_id_post_notification, however through ajax is not accessing. I just posted as is my updated code :/

  • @Felippesousa Could you show me the server response

  • I managed to do with the Next js, it worked here. Thank you very much!

0

My js in view code is now as follows:

var formData = new FormData();
			formData.append('id', '1515');
            fetch('http://localhost/page.com.br/public/post_notification/insert_id_post_notification', {
                method: "POST",
                body: formData
            })
            .then(function(response) {
                return console.log(response);
            });

The POST with Ajax is OK status! inserir a descrição da imagem aqui

My Controller is as follows:

<?php
    class Post_notification extends CI_Controller
    {
        public function __construct() {
            parent::__construct();
        }
        public function insert_id_post_notification()
        {        
            $teste = $this->input->post('id');
            var_dump($teste);
        }
    }
?>

It’s not coming in the var_dump, so it’s not even coming in the way insert_id_post_notification controller. I don’t know why, because playing the directory I’m using in ajax right in the url I can access :/

0

Code with Axios js worked!

<script src="https://unpkg.com/axios/dist/axios.min.js"></script><script type="text/javascript">				axios.post('http://localhost/app.realizaconstrutora.com.br/public/id_notification', {
					id: 1515
				})
				.then((response) => {
					console.log(response.data);
		    	});
</script>
      

Thank you guys!

0

Use jQuery to fail

// CDN : https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js
$.post(
       'link',//url de destino
       {id: '151515'},//dados e como vao chegar no controller, para adicionar mais dados basta dar , e continuar o array
       function(data){
    //Aqui voce lida com o retorno do controller
});

-1

Man says send it this way:

var formData = new FormData();
formData.append('id', 14);
formData.append('name', 'teste');
formData.append('valor', '1.4');

fetch('http://localhost/page.com.br/public/id_notification', {
                method: "POST",
                body: formData
            })
            .then(function(response) {
                return console.log(response);
            });
  • Hello, I put it like this: var formData = new FormData();&#xA;formData.append('id', '1515');&#xA;fetch('http://localhost/page.com.br/public/id_notification', {&#xA;method: "POST",&#xA;body: formData&#xA; })&#xA; .then(function(response) {&#xA; return console.log(response);&#xA; }); POST with ajax is sending with status ok. The route is correct! Playing it directly in the url I can access the method in the controller, but ajax is not entering the method insert_id_post_notification controller, even with the correct directory

Browser other questions tagged

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