Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

320
Visualizações
How to display a value in textbox with radio button click in PHP

I am new to php and I am trying to display a value ($total_discount) to a textbox when radio button is clicked. But whenever my radio button is clicked, it only outputs a text 'undefined'. This is my code

<script type="text/javascript">
   function insertDiscount(getDiscount) {
      document.getElementById("discount").value = getDiscount;
   }
</script>

<?php
   $quantity = $_POST["quantity"];
   $discount_a = 0.10;
   $discounted_price;
   $total_discount;

   if(isset($item_price)){
       $discounted_price = $item_price * $discount_a;
       $total_discount = $discounted_price * $quantity;
   }
?>

<input type="text" name="discount" id="discount" value="">
<input type="radio" name="discounts" id="discount_A" onclick="insertDiscount(''.$total_discount);">
<label for="discount_A">10% Discount</label>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

  • The output is undefined because $total_discount variable is undefined.
  • First you can check $total_discount is not null.
  • if $total_discount is a not null then you can pass $total_discount in javascript function insertDiscount() function.
  • if $total_discount is null or undefined then you can assign $total_discount variable value and pass in javascript function insertDiscount() function.

Example code

<?php if(isset($total_discount)): ?>
    <input type="radio" name="discounts" id="discount_A" onclick="insertDiscount(<?=$total_discount?>);">
<?php else: ?>
    <!-- First you can assign $total_discount variable value then pass it -->
    <?php $total_discount = 10; ?>
    <input type="radio" name="discounts" id="discount_A" onclick="insertDiscount(<?=$total_discount?>);">
<?php endif ?>
about 4 years ago · Juan Pablo Isaza Relatório

0

<input type="radio" name="discounts" id="discount_A" onclick="insertDiscount(''.$total_discount);">
  • To output the value of $total_discount you need to <?= $total_discount; ?> which will echo the value

So your final result will look something like this.

onclick="insertDiscount('<?= $total_discount; ?>')">
about 4 years ago · Juan Pablo Isaza Relatório

0

PHP is not rendering the value of the variable $total_discount.

Chanage

onclick="insertDiscount(''.$total_discount);"

to

onclick="insertDiscount(<?php echo($total_discount); ?>)"

to achieve the desired result.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda