Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

328
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!