Jump to content

Checkbox values


NewbieAmi

Recommended Posts

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) post-206737-0-85294100-1516641019_thumb.png

 

thank you
Link to comment
Share on other sites

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 checkbox

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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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? :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

^^^ 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'  :happy-04:

Link to comment
Share on other sites

 

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!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.