What to put in the $.post ajax variable

Asked

Viewed 187 times

3

Hi, I’m not very good at javascript,ajax,jquery and json I’m still learning and remembering that I’ve researched a lot, but I couldn’t do what I want. The following is my project is in MVC with friendly url. On the page has a text box that validates the zip code and displays the freight values that I did with PHP, but I had to put two options with radio to choose the type of freight, I wanted the moment I clicked on the type of freight for example "Pac" or "sedex" he updated the page and sent this value to a PHP variable.

here the file load.js.

$(document).ready(function() {
    $('#botao').click(function(){
        $('#aguarde, #blanket').css('display','block');
    });

  $("input:radio").click(function() {    

  var cor = $(this).val();

    var correio = cor;

     $.post("lojavirtualpoo/carrinho<-não estou sabendo colocar isso aqui",
    {
        correio: correio
    },function(){
        console.log(correio);
    });


 }); 


});

this page is located in localhost/lojavirtualpoo/app/views/cart/Index.php, inside the page Index.php has these code to receive the value but does not receive localhost/lojavirtualpoo/app/views/cart/Index.php

$tipo_correio = $_POST["correio"];
var_dump($tipo_correio);

html code on the same page

<td>

 <span class="tt1">Opção</span>

 <?php if ($cep_destino != "") { ?>

 <span class="tt2"><input type="radio" name="correio" value="pac"> 1</span>

 <span class="tt2"><input type="radio" name="correio" value="sedex"> 2</span>

 <?php } else { ?>

 <span class="tt2">Nenhum resultado</span>

 <?php } ?>

</td>

here’s an image for you to understand better what I want inserir a descrição da imagem aqui

  • directly accessing the url lojavirtualpoo/carrinho works?

  • works yes, on the console when I click one of the two options appears on the Pac or sedex console

  • It may be that you are giving error. See in the network tab of devtools the response of the request

  • I checked the status is 200 as ok, I click on one of the options and the console appears normal, but the receive variable in php $type_mail does not receive the value...

  • Notice: Undefined index: correio in C: wamp64 www lojavirtualpoo app views carrinho Index.php on line 2

  • Already tried to put in the Ajax URL the absolute path: http://localhost/lojavirtualpoo/app/views/carrinho/Index.php?

  • Yes I have tested and it didn’t work, I’m studying to know where the error is, you want to leave it open? or better close? I’m sure if I devote a little more time to mastering these technologies I’ll know how to do what I want...

Show 2 more comments

1 answer

0

Carlos, the question was confused, but I worked out this flow...

$(document).ready(function() {
    $('#botao').click(function(){
        $('#aguarde, #blanket').css('display','block');
    });

  $("input:radio").click(function() {  
    let cor = $(this).val();
    let correio = cor;
	
    let tipoFrete = ''; // id ou string com tipo de frete

    $.ajax({
	method: 'POST',
	url: 'rota de busca do cep',
	data: {frete: tipoFrete},
	dataType: 'JSON',
	})
	.done(function (response) {
	  // executa alguma ação após a ação no servidor for efetivada com sucesso
	})
	.fail(function(error) {
	  console.error(error)
	})

   }); 


});

Browser other questions tagged

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