Get radio button value via jQuery and set in PHP

Asked

Viewed 1,191 times

1

I have to capture an event of click in a radio button and pass its value in a PHP variable to be able to do a value calculation, I was not very successful, follow my code.

function Tipo_Frete() {
    var frete = $("input[name='tipo_frete']:checked").val();          
    if (frete == "PAC") {
      frete = 'pac'
    }else if(frete == 'SEDEX'){
      frete = 'sedex'
    }        
}  

<?php $frete = "<script>document.write(frete)</script>"; 

What I need even now is for this variable to be accessed in view, without having to go through controller. I need a PHP variable to receive the value of button marked, so in PHP I can do the calculation. How to make all this happen dynamically, without having to submit this along with the form, and handle everything on view?

  • 1

    You need to pass via post or get to php, so the "freight" variable does not exist in the server context. Use ajax to pass the variable "shipping" to php and capture using the global $_POST or $_GET, depending on how the request is made in ajax.

  • You can use ajax or pass the url as @Diegofelipe said or pass an Hidden field via form Submit.

  • Thank you guys, but what I really need right now is for this variable to be accessed in the view, without having to go through the controller, I need a php variable to receive the value of the marked button, so in php I can do the calculation, all this needs to happen dynamically, understand, I can not submit this along with the form, I need to treat everything in the view.

  • So the solution is to use ajax, so you make a request only when the radio button is changed.

  • Okay, thanks a lot to everyone, we got through here, we used jquery and it worked, thanks a lot.

  • 1

    If you can share your solution, it will help others with the same problem. Just post a!

Show 1 more comment

1 answer

1

The solution is using AJAX

in his .js (using jquery)

$(document).ready(function(){
   //gatilho para executar o ajax no click do radio (da escolha PAC ou SEDEX)
   $("input[name='tipo_frete']").click(function(){
      $.post('/script_que_calcula.php',{
         //fazendo dessa forma abaixo o seletor pega o valor do radio    selecionado e atribui a variavel com o valor correto
         frete: $("input[name='tipo_frete']").val() == "SEDEX" ? "sedex" : "pac"
      },function(data){
          /*aqui recebemos um objeto json com o valor 
          do frete no atributo 'valor' é evidente que
          a lógica para esse calculo deverá estar em 
          seu script e atualizamos o elemento na 
          view sem atualizar a página (refresh)*/
          $("#valor_frete").val(data.valor);
      },"json");
   });
});

below let’s imagine your script_que_calcula.php

<?php
     $array = array();

     //abaixo está como deveria ser feito o retorno para a
     //requisicao ajax... é claro que deverá conter sua lógica de negócio
     if($_POST['frete']=="sedex"){
        $array("valor" => "valor do sedex");
     }
     else{
        $array("valor" => "valor do pac")
     }

     /*aqui está o retorno sendo 'encodado' 
     para json assim sendo possivel usar no ajax*/
     echo json_encode($array);

is a fairly simple example of the use of ajax, is the ideial use to reach the desired solution

Browser other questions tagged

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