1

I currently have page logic which relys on the value tag of my checkbox. Want to send different data to my next page but doing so breaks code on my current page.

I want to send the ROW number not price. I thought to store the seat values into session data but I don't know how I can refer to the data in different columns when I can only have one value be bound to thecheckbox.

echo "input type='checkbox' name='check_list[]' value=\"$price;\";

to

echo "input type='checkbox' name='check_list[]' value=\"$seat;\";

breaks the total cost field below

enter image description here

foreach($res as $row) {


      $seat = $row['RowNumber'];  
      $price = $row['Zone.PriceMultiplier * 15.00'];

       echo "<tr>";
       echo "<td>".$seat."</td>";
       echo "<td>".$price."</td>";
       echo "<td><input type='checkbox' name='check_list[]' value=\"$price;\" </td>";

       echo "</tr>";

    }


enter image description here

code for the text feild.

echo "<script> function calculateCheckbox() {


  var el = document.getElementById('output');
  var products = el.getElementsByTagName('input');
  var len = products.length;

  for (var i = 0; i < len; i++) {
    if (products[i].type === 'checkbox') {
      products[i].onclick = updateCost;
    }
  }
}
</script>";


echo "<script> function updateCost(e) {

  var myForm = this.form;
  var val = parseFloat(myForm.elements['total-cost'].value);


  if (this.checked) {
    val += parseFloat(this.value);
  } else {
    val -= parseFloat(this.value);
  }

  myForm.elements['total-cost'].value = val} 
</script>";

I tried the non JS solution here but it didnt work.

HTML submit multiple values through one check box?

Do i need to use JS to get past this.

  • Why don't you just put the row number in the value field then? – Timberman Dec 03 '19 at 14:52
  • Well of course calculating the total price fails … if you replace all the prices in the place where you read them from, with something else. If you need the row ID as submission value - then store the price somewhere else, keyword _custom data attribute_. – 04FS Dec 03 '19 at 14:53
  • _“code for the text feild.”_ - that is not the code that actually does the calculation, this is only adding the click event handlers. `updateCost` is what you will need to modify to read the price from somewhere else. – 04FS Dec 03 '19 at 14:54
  • can you provide more detail as to what you mean by "read the price from somewhere else." do you mean an array ie array $arr[] = array('price' => $row['Zone.PriceMultiplier * 15.00'],'row' => $row['RowNumber']); – Gooze_Berry Dec 03 '19 at 16:45

1 Answers1

0

You're running into the issue of passing irrelevant data from the form to the server.

If you simply send the seats, you should be able to look up the price on the server.

Besides, you should not be sending the price from the form anyways since this data could easily have been tampered with.

// Form - just send the chosen seats
echo "<td><input type='checkbox' name='check_list[]' value=\"$seat;\" </td>";

// Form processing.
$seats = $_POST['check_list'];

$total = 0;
foreach ($seats as $seat) {
    $total += your_nifty_price_service($seat);
}

EDIT

Taking this a step further, you can use AJAX to retrieve the price from the server.

Snippets taken from

Form

<form ...>
    <input type="text" id="total-price" name="price">

    <input type="checkbox" class="seats" name="check_list[] value="<?php echo $seat; ?>">;
    <input type="checkbox" class="seats" name="check_list[] value="<?php echo $seat; ?>">;

    ...
</form>

<script>
    $('#calculate-price').click(function() {

        $.ajax({
            url: "your-form-processor.php",
            type: "post",
            data: $('.seats:checked').serialize(),
            success: function(data) {
                $("#total-price").val(data);
            }
        });
    });
</script>

your-form-processor.php

// Check this is a POST request & do validation
// ...

$seats = $_POST['check_list'];

$total = 0;
foreach ($seats as $seat) {
    $total += your_nifty_price_service($seat);
}
return $total;
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • so when updating the cost via updateCost(e), i should use the $seat row number to look up the cost of that particular seat – Gooze_Berry Dec 03 '19 at 15:10
  • Well, I'm afraid that things will get a bit more complicated if you want to use JS to update the total amount. You'll need to use AJAX to retrieve the amount from the server and then set the textbox's value to that value. – waterloomatt Dec 03 '19 at 15:25
  • Added some code to illustrate the idea. Let me know if you have questions. – waterloomatt Dec 03 '19 at 15:39