doc1355 Posted November 13, 2021 Share Posted November 13, 2021 (edited) I have the following array: $available_seasons = array ( 0 => array ( 'season_id' => 226, 'season_name' => '2022 - Season 1', 'season_price' => '25.99', 'season_start_date' => 'Jan. 1, 2022', 'season_end_date' => 'Mar. 31, 2022', 'prize' => 100, ), 1 => array ( 'season_id' => 227, 'season_name' => '2022 - Season 2', 'season_price' => '28.99', 'season_start_date' => 'Apr. 1, 2022', 'season_end_date' => 'Jun. 30, 2022', 'prize' => 100, ), 2 => array ( 'season_id' => 238, 'season_name' => '2022 - Season 3', 'season_price' => '40.99', 'season_start_date' => 'Jul. 1, 2022', 'season_end_date' => 'Sep. 30, 2022', 'prize' => 230, ), 3 => array ( 'season_id' => 239, 'season_name' => '2022 - Season 4', 'season_price' => '30.65', 'season_start_date' => 'Oct. 1, 2022', 'season_end_date' => 'Dec. 31, 2022', 'prize' => 300, ), ) I'm using the following loop to create the form: echo "<form name='seasonForm' method=\"post\" action='execute.php'>"; foreach ( $available_seasons as $k => $season ) { echo "<input name='choice' class='uk-checkbox' type='checkbox' value='{$season[ 'season_price' ]}' onchange='checkTotal()'/>"; } echo "<button type='submit'>Continue</button>"; I have the following JS to calculate the total price: function checkTotal() { document.seasonForm.total.value = \'\'; var sum = 0; for (i=0;i<document.seasonForm.choice.length;i++) { if (document.seasonForm.choice[i].checked) { sum = sum + (document.seasonForm.choice[i].value *1); } } document.seasonForm.total.value = "Total: $" + sum; } I am showing the total price to my users, before they click on submit. As it is, I am only able to able to get the value of one checkbox, which is season_price in my execute.php. What I need is to pass the all season_id and season_price values of the selected checkboxes to execute.php. I am not sure how to do this. Thank you for your help in advance. Edited November 13, 2021 by doc1355 Quote Link to comment https://forums.phpfreaks.com/topic/314222-get-two-values-from-one-checkbox/ Share on other sites More sharing options...
ginerjm Posted November 13, 2021 Share Posted November 13, 2021 You need to 'name' your checkboxes as 'name[]' to produce an array of the checkboxes that are checked. Quote Link to comment https://forums.phpfreaks.com/topic/314222-get-two-values-from-one-checkbox/#findComment-1592025 Share on other sites More sharing options...
doc1355 Posted November 13, 2021 Author Share Posted November 13, 2021 Thank you for your reply. 1- When I change the name of the checkbox to choice[], the javascript doesn't calculate or show the total, but you are correct, I can get all the values of the checkboxes. 2- How can I get the second value (season_id) with the current setup? Quote Link to comment https://forums.phpfreaks.com/topic/314222-get-two-values-from-one-checkbox/#findComment-1592027 Share on other sites More sharing options...
ginerjm Posted November 13, 2021 Share Posted November 13, 2021 My bad. You need to reference the boxes using an id, not a name value. Quote Link to comment https://forums.phpfreaks.com/topic/314222-get-two-values-from-one-checkbox/#findComment-1592029 Share on other sites More sharing options...
Barand Posted November 13, 2021 Share Posted November 13, 2021 Name the checkboxes choice[226], choice[227] etc where 226 is your id 1 Quote Link to comment https://forums.phpfreaks.com/topic/314222-get-two-values-from-one-checkbox/#findComment-1592030 Share on other sites More sharing options...
doc1355 Posted November 13, 2021 Author Share Posted November 13, 2021 15 minutes ago, ginerjm said: My bad. You need to reference the boxes using an id, not a name value. Not sure how to do that. Quote Link to comment https://forums.phpfreaks.com/topic/314222-get-two-values-from-one-checkbox/#findComment-1592031 Share on other sites More sharing options...
doc1355 Posted November 13, 2021 Author Share Posted November 13, 2021 14 minutes ago, Barand said: Name the checkboxes choice[226], choice[227] etc where 226 is your id This is brilliant. Thanks. Now I need to figure out how to get the script to work with the ID, instead of name. Quote Link to comment https://forums.phpfreaks.com/topic/314222-get-two-values-from-one-checkbox/#findComment-1592032 Share on other sites More sharing options...
doc1355 Posted November 13, 2021 Author Share Posted November 13, 2021 1 hour ago, doc1355 said: Not sure how to do that. Ok. I figured it out. I used getElementsByClassName instead of ID and it worked. Quote Link to comment https://forums.phpfreaks.com/topic/314222-get-two-values-from-one-checkbox/#findComment-1592042 Share on other sites More sharing options...
Barand Posted November 14, 2021 Share Posted November 14, 2021 You are passing the prices in the post data as the checkbox values, but don't use them (they can be spoofed). Instead use the id to get the price. EXAMPLE <?php $available_seasons = array ( 226 => array ( 'season_name' => '2022 - Season 1', 'season_price' => '25.99', 'season_start_date' => 'Jan. 1, 2022', 'season_end_date' => 'Mar. 31, 2022', 'prize' => 100, ), 227 => array ( 'season_name' => '2022 - Season 2', 'season_price' => '28.99', 'season_start_date' => 'Apr. 1, 2022', 'season_end_date' => 'Jun. 30, 2022', 'prize' => 100, ), 238 => array ( 'season_name' => '2022 - Season 3', 'season_price' => '40.99', 'season_start_date' => 'Jul. 1, 2022', 'season_end_date' => 'Sep. 30, 2022', 'prize' => 230, ), 239 => array ( 'season_id' => 239, 'season_name' => '2022 - Season 4', 'season_price' => '30.65', 'season_start_date' => 'Oct. 1, 2022', 'season_end_date' => 'Dec. 31, 2022', 'prize' => 300, ) ); if ($_SERVER['REQUEST_METHOD'] == 'POST') { $total = 0; echo "<h3>Chosen seasons</h3>"; foreach ($_POST['choice'] as $id => $chosen) { $price = $available_seasons[$id]['season_price']; // get price from your data, not the user $total += $price; echo "{$available_seasons[$id]['season_name']} — \${$price}<br>"; } echo "<h3>Total due  \${$total}</h3><hr><br><br>"; exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Example</title> <meta charset="utf-8"> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type='text/javascript'> function checkTotal(box) { var sum = parseFloat($("#total").val()) if ( $(box).is(":checked")) { sum += $(box).val() * 1 } else { sum -= $(box).val() * 1 } $("#total").val(sum.toFixed(2)) } </script> </head> <body> <form name='seasonForm' method="post" action=''> <fieldset> <legend>Tickets for Seasons</legend> <?php foreach ( $available_seasons as $k => $season ) { echo "<input name='choice[$k]' class='uk-checkbox' type='checkbox' value='{$season[ 'season_price' ]}' onclick='checkTotal(this)'/> {$season['season_name']}<br>"; } echo "<button type='submit'>Continue</button><br>"; ?> </fieldset> <input id='total' value='0' > </body> </html> 1 Quote Link to comment https://forums.phpfreaks.com/topic/314222-get-two-values-from-one-checkbox/#findComment-1592050 Share on other sites More sharing options...
doc1355 Posted November 14, 2021 Author Share Posted November 14, 2021 Great point. Thank you for the hint and the code. Quote Link to comment https://forums.phpfreaks.com/topic/314222-get-two-values-from-one-checkbox/#findComment-1592053 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.