NewbieAmi Posted January 22, 2018 Share Posted January 22, 2018 Hello, I continue with a few exercises and can't get any further with the selected values. <html> <head> <title>Benutzer Aktivität</title> <meta charset="utf-8" /> </head> <body> <?php include("Parameters.php"); // Connecting to the MySQL server $mysqli=mysqli_connect($host,$user,$pass) or die("Problem of connection to the MySQL server :".mysqli_error()); // Creating the database //query($mysqli,'CREATE DATABASE IF NOT EXISTS '.$base); // Selecting the database mysqli_select_db($mysqli,$base) or die("Selection of the database failed! : $base"); //Darstellung der Zutaten Tabelle für die Auswahl //Auswahl Zutaten aus Kategorie? ("obst_gemüse","alkohol","saft_sirup","sonstige") //Speichern in der Datenbank als neue/eigene Cocktail # execute search query $query = 'SELECT * FROM `DetailsCategory`'; $result = mysqli_query($mysqli, $query); # check result if (!$result) die('Could not successfully run query: ' . mysqli_connect_error()); # display returned data if (mysqli_num_rows($result) > 0) { ?> <form action="" method="post"> <table border = "1" align = "center" style="line-height:25px;"> <tr> <th>Auswahl</th> <th>Id</th> <th>Obst/Gemuese</th> <th>Alkohol</th> <th>Saft/Sirup</th> <th>Sonstige</th> <?php while ($row = mysqli_fetch_assoc($result)) { echo '<tr><td>'; echo '<input type="checkbox" name="selected[]" value="'.$row['id'].'"/>'; echo '</td>'; foreach ($row as $key => $value) echo '<td>'.htmlspecialchars($value).'</td>'; echo '</tr>'; } ?> </table> <!-- <input type="submit"/> --> <input type = "submit" name = "submit" value = "Submit" /> <?php if(isset($_POST['submit'])){ if(!empty($_POST['selected'])) { // Counting number of checked checkboxes. $checked_count = count($_POST['selected']); echo "Sie haben folgende ".$checked_count." Zutaten(s) ausgewählt: <br/>"; // Loop to store and display values of individual checked checkbox. foreach($_POST['selected'] as $selected) { echo "<p>".$selected ."</p>"; } } } ?> </form> <?php } else echo '<p>No data</p>'; # free resources mysqli_free_result($result); ?> </body> </html> And why do I have a gap in my HTML table even though the value exists? (image) thank you Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/ Share on other sites More sharing options...
benanamen Posted January 22, 2018 Share Posted January 22, 2018 You need one more table header after <th>Sonstige</th> Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555657 Share on other sites More sharing options...
NewbieAmi Posted January 22, 2018 Author Share Posted January 22, 2018 You need one more table header after <th>Sonstige</th> Thank you but it didn't really fix the problem. Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555660 Share on other sites More sharing options...
benanamen Posted January 22, 2018 Share Posted January 22, 2018 Lets see what you did. Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555664 Share on other sites More sharing options...
NewbieAmi Posted January 22, 2018 Author Share Posted January 22, 2018 How can i post a picture on a quote? Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555667 Share on other sites More sharing options...
Stratadox Posted January 22, 2018 Share Posted January 22, 2018 Hello, I continue with a few exercises and can't get any further with the selected values. In order to tell you how to get further with the selected values, we'll need to know what you want to end up doing with them... And why do I have a gap in my HTML table even though the value exists? Are you sure the value exists? The code you're using seems to be correct; the only reason for it to show an empty result is if there's an empty result in the database. PS: the part right below Loop to store and display values of individual checked checkboxputs values from $_POST directly on the webpage. This may make you vulnerable to xss attacks, and - depending how you move on - to sql injections. You can counter that by using htmlspecialchars($selected). Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555669 Share on other sites More sharing options...
NewbieAmi Posted January 22, 2018 Author Share Posted January 22, 2018 In order to tell you how to get further with the selected values, we'll need to know what you want to end up doing with them... => Actually i get everything wrong here. I want to add a checkbox in front of EACH element of EACH row so that the checked ones can be showed to the user as his choices Are you sure the value exists? The code you're using seems to be correct; the only reason for it to show an empty result is if there's an empty result in the database. =>> Yup here is the insertion code INSERT INTO `DetailsCategory` (`id`, `obst_gemuese`, `alkohol`, `saft_sirup`, `sonstige`) VALUES ('1', 'Ananas', 'Malibu', 'Grenadinesirup', 'Eis'), ('2', 'Himbeere', 'Whisky', 'Ananassaft', 'Zucker'), ('3', 'Grapefruit', 'Weisser Rum', 'Apfelsaft', ''), ('4', 'Zitrone', 'Trockener Weißwein', 'Preiselbeersaft', ''), ('5', 'Guava', 'Rum', 'Kirschsaft', ''); PS: the part right below puts values from $_POST directly on the webpage. This may make you vulnerable to xss attacks, and - depending how you move on - to sql injections. You can counter that by using htmlspecialchars($selected). => i didnt get it. sorry thank you Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555676 Share on other sites More sharing options...
dodgeitorelse3 Posted January 22, 2018 Share Posted January 22, 2018 (edited) start with changing <th>Auswahl</th> <th>Id</th> <th>Obst/Gemuese</th> <th>Alkohol</th> <th>Saft/Sirup</th> <th>Sonstige</th> to this <th>Auswahl</th> <th>Id</th> <th>Obst/Gemuese</th> <th>Alkohol</th> <th>Saft/Sirup</th> <th>Sonstige</th> <th>Datum</th> as benanamen suggested and id 4 insert has issue with Trockener Weißwein Edited January 22, 2018 by dodgeitorelse3 Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555679 Share on other sites More sharing options...
NewbieAmi Posted January 22, 2018 Author Share Posted January 22, 2018 start with changing <th>Auswahl</th> <th>Id</th> <th>Obst/Gemuese</th> <th>Alkohol</th> <th>Saft/Sirup</th> <th>Sonstige</th> to this <th>Auswahl</th> <th>Id</th> <th>Obst/Gemuese</th> <th>Alkohol</th> <th>Saft/Sirup</th> <th>Sonstige</th> <th>Datum</th> as benanamen suggested and id 4 insert has issue with Trockener Weißwein Thank you ... it was ss instead of ß (Weisswein). and now? Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555684 Share on other sites More sharing options...
dodgeitorelse3 Posted January 22, 2018 Share Posted January 22, 2018 (edited) and now where are you at with the problem? I can guess but will probably be wrong. SHow us your current code and explain what your question is. Have you solved the gap issue? Edited January 22, 2018 by dodgeitorelse3 Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555685 Share on other sites More sharing options...
NewbieAmi Posted January 22, 2018 Author Share Posted January 22, 2018 and now where are you at with the problem? I can guess but will probably be wrong. SHow us your current code and explain what your question is. Have you solved the gap issue? Yes i have solved the gap issue. but when i do some selections the result is displaying only numbers (from id) but as i mentioned in my last quote i need help to add a checkbox in front of EACH element of EACH row. i want to select each item of the table and display the result. thank you Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555686 Share on other sites More sharing options...
Stratadox Posted January 22, 2018 Share Posted January 22, 2018 You can select them from the database again by using a where clause in your query: $query = 'SELECT * FROM `DetailsCategory`WHERE id = ?'; $stmt = $mysqli->prepare($query); $stmt->bind('i', $selected); $result = $stmt->execute(); Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555690 Share on other sites More sharing options...
dodgeitorelse3 Posted January 23, 2018 Share Posted January 23, 2018 echo '<input type="checkbox" name="selected[]" value="'.$row['id'].', '.$row['obst_gemuese'].', '.$row['alkohol'].', '.$row['saft_sirup'].', '.$row['sonstige'].'"/>'; no idea how you really want the data to show with such a vague description. 1 Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555691 Share on other sites More sharing options...
NewbieAmi Posted January 23, 2018 Author Share Posted January 23, 2018 echo '<input type="checkbox" name="selected[]" value="'.$row['id'].', '.$row['obst_gemuese'].', '.$row['alkohol'].', '.$row['saft_sirup'].', '.$row['sonstige'].'"/>'; no idea how you really want the data to show with such a vague description. Thanks Dod, i coming closer thank you... but it selected the entire row when i click on any checkbox BUT what i try to do is to select ONE item by ONE on EACH row and column. so that i could just have what i selected i.e. : Sie haben folgende 2 Zutaten(s) ausgewählt: Ananas, Zitrone, Malibu,, Rum, Ananassaft, Zucker thank you for your time Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555696 Share on other sites More sharing options...
dodgeitorelse3 Posted January 23, 2018 Share Posted January 23, 2018 Our goal here is to help folks with problems provided they are helping themselves. We are not going to just write code for people. What we ask is that when you post a question you also show us what you have tried by showing your current code, not just by saying you tried something. Stop and think about what you are trying to achieve and make an attempt. For example, a single checkbox cannot magically know which selection you are making at each row. So more than one checkbox is needed, or perhaps instead of displaying in a table, let users make choices from dropdowns. A good design saves a lot of work and also gives the end user a better experience. Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555715 Share on other sites More sharing options...
dodgeitorelse3 Posted January 23, 2018 Share Posted January 23, 2018 You are also probably going to need to name each checkbox ( if that's what you end up using) which is going to help you end the end because I would think you intend to do more with the selections than just display them the way you are currently. Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555716 Share on other sites More sharing options...
NewbieAmi Posted January 26, 2018 Author Share Posted January 26, 2018 You are also probably going to need to name each checkbox ( if that's what you end up using) which is going to help you end the end because I would think you intend to do more with the selections than just display them the way you are currently. Hey! you are right ... after showing what has been selected i'll store them (selections) in a table on my database. I succeeded with the checkboxes but now i can't fetch each selected variable anymore. please help ... i'm trying. <html> <head> <title>User Activities</title> <meta charset="utf-8" /> </head> <body> <?php include("Parametres.php"); // Connecting to the MySQL server $mysqli=mysqli_connect($host,$user,$pass) or die("Problem of connection to the MySQL server: " .mysqli_error($mysqli)); // Creating the database //query($mysqli,'CREATE DATABASE IF NOT EXISTS '.$base); // Selecting the database mysqli_select_db($mysqli,$base) or die("Selection of the database failed! : $base"); # execute search query $query = 'SELECT * FROM `DetailsCategory`'; $result = mysqli_query($mysqli, $query); # check result if (!$result) die('Could not successfully run query: ' . mysqli_connect_error()); # display returned data if (mysqli_num_rows($result) > 0) { ?> <form action="" method="post"> <table border = "1" align = "center" style="line-height:25px;"> <tr> <th>Obst/Gemuese</th> <th>Alkohol</th> <th>Saft/Sirup</th> <th>Sonstige</th> </tr> <?php $checkbox = '<input type ="checkbox" name = "selected[]" value="" />'; while ($row = mysqli_fetch_assoc($result)) {?> <tr> <td><?php echo $checkbox . $row['obst_gemuese'];?></td> <td><?php echo $checkbox . $row['alkohol'];?></td> <td><?php echo $checkbox . $row['saft_sirup'];?></td> <td><?php echo $checkbox . $row['sonstige'];?></td> </tr> <?php } ?> </table> <!-- <input type="submit"/> --> <input type = "submit" name = "submit" value = "Submit" /> <?php if(isset($_POST['submit'])){ if(!empty($_POST['selected'])) { // Counting number of checked checkboxes. $checked_count = count($_POST['selected']); echo "Sie haben folgende ".$checked_count." Zutat(en) ausgewählt: <br/>"; // Loop to store and display values of individual checked checkbox. foreach($_POST['selected'] as $selected) { echo "<p>".$selected ."</p>"; } } } ?> </form> <?php } else echo '<p>No data</p>'; # free resources mysqli_free_result($result); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555793 Share on other sites More sharing options...
dodgeitorelse3 Posted January 26, 2018 Share Posted January 26, 2018 (edited) by declaring $checkboxes as you have there is no value being assigned therefore nothing is showing as expected which is correct. I am not a master coder by any means, in fact I'm just a noob in learning. however the following does seem to work. I would assume there is probably a much better way to do this. //$checkbox = '<input type ="checkbox" name = "selected[]" value="" />'; while ($row = mysqli_fetch_assoc($result)) {?> <tr> <td><input type ="checkbox" name = "selected[]" value="<?php echo $row['obst_gemuese'];?>" /> <?php echo $row['obst_gemuese'];?></td> <td><input type ="checkbox" name = "selected[]" value="<?php echo $row['alkohol'];?>" /> <?php echo $row['alkohol'];?></td> <td><input type ="checkbox" name = "selected[]" value="<?php echo $row['saft_sirup'];?>" /> <?php echo $row['saft_sirup'];?></td> <td><input type ="checkbox" name = "selected[]" value="<?php echo $row['sonstige'];?>" /> <?php echo $row['sonstige'];?></td> </tr> Edited January 26, 2018 by dodgeitorelse3 Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555795 Share on other sites More sharing options...
dodgeitorelse3 Posted January 26, 2018 Share Posted January 26, 2018 (edited) editted: removed due to double post lol Edited January 26, 2018 by dodgeitorelse3 Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555796 Share on other sites More sharing options...
dodgeitorelse3 Posted January 27, 2018 Share Posted January 27, 2018 this also works <table border = "1" align = "center" style="line-height:25px;"> <tr> <th>Obst/Gemuese</th> <th>Alkohol</th> <th>Saft/Sirup</th> <th>Sonstige</th> </tr> <?php while ($row = mysqli_fetch_array($result)) { $tableid[] = $row['id']; $obst_gemuese[] = $row['obst_gemuese']; $alkohol[] = $row['alkohol']; $saft_sirup[] = $row['saft_sirup']; $sonstige[] = $row['sonstige']; } foreach($tableid as $key => $tblid) { echo "<tr> <td><input type='checkbox' name='selected[]' value='{$obst_gemuese[$key]}' /> $obst_gemuese[$key]</td> <td><input type='checkbox' name='selected[]' value='{$alkohol[$key]}' /> $alkohol[$key]</td> <td><input type='checkbox' name='selected[]' value='{$saft_sirup[$key]}' /> $saft_sirup[$key]</td> <td><input type='checkbox' name='selected[]' value='{$sonstige[$key]}' /> $sonstige[$key]</td> </tr>"; } ?> </table> 1 Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555797 Share on other sites More sharing options...
ginerjm Posted January 27, 2018 Share Posted January 27, 2018 ?? Why do you loop thru the query results and place them in an array only to loop thru that array to output them? You could have saved yourself a lot of typing by just outputting the query result elements directly. Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555809 Share on other sites More sharing options...
mac_gyver Posted January 27, 2018 Share Posted January 27, 2018 ^^^ if implemented without a lot of extra variables and unnecessary lines of code, this would be a programming practice called 'separation of concerns', which the OP will wish he was using when he finds out how much extra work it will be when using the mysqli extension with prepared queries and wants to switch to the much simpler php PDO extension. if implemented by creating a bunch of extra variables and extra lines of code, it's a programming practice called 'wearing out your keyboard faster' Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555811 Share on other sites More sharing options...
dodgeitorelse3 Posted January 28, 2018 Share Posted January 28, 2018 Understood. Is it possible for you to remove my posts to prevent showing anyone the improper way? Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555823 Share on other sites More sharing options...
HemendraSingh Posted January 29, 2018 Share Posted January 29, 2018 Thanks for your code but you should provide more data with check boxes in details. Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555834 Share on other sites More sharing options...
NewbieAmi Posted January 29, 2018 Author Share Posted January 29, 2018 this also works <table border = "1" align = "center" style="line-height:25px;"> <tr> <th>Obst/Gemuese</th> <th>Alkohol</th> <th>Saft/Sirup</th> <th>Sonstige</th> </tr> <?php while ($row = mysqli_fetch_array($result)) { $tableid[] = $row['id']; $obst_gemuese[] = $row['obst_gemuese']; $alkohol[] = $row['alkohol']; $saft_sirup[] = $row['saft_sirup']; $sonstige[] = $row['sonstige']; } foreach($tableid as $key => $tblid) { echo "<tr> <td><input type='checkbox' name='selected[]' value='{$obst_gemuese[$key]}' /> $obst_gemuese[$key]</td> <td><input type='checkbox' name='selected[]' value='{$alkohol[$key]}' /> $alkohol[$key]</td> <td><input type='checkbox' name='selected[]' value='{$saft_sirup[$key]}' /> $saft_sirup[$key]</td> <td><input type='checkbox' name='selected[]' value='{$sonstige[$key]}' /> $sonstige[$key]</td> </tr>"; } ?> </table> Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/306301-checkbox-values/#findComment-1555846 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.