hello friends
i have 3 variable in form option and it content diferent price of shipping and i have varile price of product, and and other variable who is the total, i would like when user choose shipping of his choice that the total change my code is below

<?php $shipping1='20.78';
          $shipping2='12.89';
          $shipping3='0.00';
          ?>
          <?php $totalpay=  ($row_total['sub_total'])+($shipping1);?>
            <option name="express" value="20.00"><?php echo $shipping1 ?> </option>
            <option name="free" value="0.00"><?php  echo $shipping2 ?> </option>
            <option name="zunaphone" value="12.00"><?php echo $shipping3 ?></option>
          </select><br>
          <label for="shipping">Total</label>
          <input name="total" type="text" value="<?php echo number_format ($totalpay, 2, '.',''); ?>" readonly>

Dani AI

Generated

Short summary and fixes (for ): your page currently calculates the total once on load using a single shipping variable, so changing the select doesn’t update the shown total. Two solid options: update the total immediately in the browser with JavaScript (best UX), or recalculate on form submit in PHP (what suggested). Also note the HTML pitfalls hinted at: the name belongs on the select (not on each option), and disabled inputs are not sent with the form — use readonly plus a hidden field if you need to submit the value.

Client-side live update (keeps the page from reloading)

<?php
$subtotal = floatval($row_total['sub_total']);
$rates = ['express'=>15.50, 'free'=>0.00, 'pickup'=>7.99];
?>
<form method="post" action="...">
  <input type="text" value="<?php echo number_format($subtotal,2,'.',''); ?> EUR" readonly>
  <input type="hidden" name="subtotal" value="<?php echo htmlspecialchars($subtotal); ?>">

  <select id="shipSel" name="shipping_method">
    <?php foreach($rates as $key=>$amt): ?>
      <option value="<?php echo $key; ?>"><?php echo $key . ' — EUR ' . number_format($amt,2,'.',''); ?></option>
    <?php endforeach; ?>
  </select>

  <input id="totalDisplay" type="text" readonly>
  <input id="totalHidden" type="hidden" name="total" value="">
  <input type="submit" value="Place order">
</form>

<script>
var rates = <?php echo json_encode($rates); ?>;
var subtotal = parseFloat(document.querySelector('input[name="subtotal"]').value) || 0;
var sel = document.getElementById('shipSel');
var out = document.getElementById('totalDisplay');
var hidden = document.getElementById('totalHidden');

function updateTotal(){
  var key = sel.value;
  var ship = parseFloat(rates[key]) || 0;
  var total = subtotal + ship;
  out.value = total.toFixed(2) + ' EUR';
  hidden.value = total.toFixed(2);
}
sel.addEventListener('change', updateTotal);
updateTotal();
</script>

Server-side validation / fallback (must always be done)

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $allowed = ['express'=>15.50, 'free'=>0.00, 'pickup'=>7.99];
  $method = $_POST['shipping_method'] ?? '';
  $ship = $allowed[$method] ?? 0.0;           // do NOT trust client values
  $subtotal = floatval($_POST['subtotal'] ?? 0);
  $total = $subtotal + $ship;
  // use number_format($total,2,'.','') when printing
}

Quick troubleshooting tips

  • Use readonly for visible fields you want submitted (or add a hidden input).
  • Always validate shipping on the server; don’t rely on a client-sent amount.
  • Use parseFloat(... ) and toFixed(2) in JS for correct decimals.
  • If the select isn’t updating, check that it has the id used by your script and that the script runs after the DOM loads.

Recommended Answers

All 11 Replies

i can see the end of the

 </select>

tag on line 9 , but where is the starting point??

This is the missng part.

<select>

soory to didn't pay attetion when i posted this is the code and check the comment.

<select name="shipping" size="1"> // start of select
          <?php $shipping1='20.78';
          $shipping2='12.89';
          $shipping3='0.00';
          ?>
          <?php $totalpay=  ($row_total['sub_total'])+($shipping1);?> // when users select one of the below option that the $shipping1 change according
            <option name="express" value="20.00"><?php echo $shipping1 ?> </option> // first choice
            <option name="free" value="0.00"><?php  echo $shipping2 ?> </option> // second choice
            <option name="zunaphone" value="12.00"><?php echo $shipping3 ?></option> // third choice
          </select><br>// end fo select
          <label for="shipping">Total</label>
          <input name="total" type="text" value="<?php echo number_format ($totalpay, 2, '.',''); ?>" readonly>

Why not using form submission

i never listen about Submission please tell me more it will give me the solution i'm going to find more on network ;)

is it j-querry form ???

Can you post full code, i mean not a part but the full script.

<?php $totalpay= ($row_total['sub_total'])+($shipping1);?> //

In this code $row_total where did it come from

Form is html, though you can add jquery for validation, but you can only use html and php for submissions.

the

$row_total// is from database

and here is the whole form it is what you ask right ???

 <form name="valide" method="POST" action="<?php echo $editFormAction; ?>">
          <label for="Sub total"></label>
          <label for="shipping">Sub total</label><input name="subtotal2" type="text" value="<?php echo number_format($row_total['sub_total'], 2, '.', ''); ?>  EUR" readonly disabledid="subtotal2">
           <br>
          <label for="total"></label>
          <select name="shipping" size="1">
          <?php $shipping1='20.78';
          $shipping2='12.89';
          $shipping3='0.00';
          ?>
          <?php $totalpay=  ($row_total['sub_total'])+($shipping1);?>
            <option name="express" value="20.00">EUR <?php echo $shipping1 ?> </option>
            <option name="free" value="0.00">EUR <?php  echo $shipping2 ?> </option>
            <option name="zunaphone" value="12.00">EUR <?php echo $shipping3 ?></option>
          </select><br>
          <label for="shipping">Total</label>
          <input name="total" type="text" value="<?php echo number_format ($totalpay, 2, '.',''); ?>  EUR" readonly><br>
          <input name="date" type="hidden" id="date" value="<?php echo $row_total['']; ?>">
          <input name="sku" type="hidden" id="sku" value="<?php echo $row_total['']; ?>">
          <input type="hidden" name="client" id="client" value="<?php echo $_SESSION[''] ?>">
<input type="submit" name="button" id="button" value="Place an order">
<input type="hidden" name="MM_insert" value="valide">
        </form>

hello dears friensds i give the code up please help

Isn't this code working, if not then is it throwing any error or not!

this code work but i would like to change the $totalpay when user select one of the 3 option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.