Jump to content

Checkboxes - Saving and Retrieving Values (PHP/MySQL)


idontknowphp

Recommended Posts

I have figured out far enough to loop through my table and return a list of check boxes that will be matched to a product. My issue now, is i cannot figure out the proper way to loop through them after they are selected and return them as checked/unchecked for the given product. Thought there is some MySQL involved, that part isn't the problem, I know how to insert into a database or update...when the product is selected from a drop down, I need to be able to populate the checkboxes.

 

Here is the code i am using to initially display all of the categories to choose from...

<ul class="categories">
  <?php 
    $query = "SELECT * FROM products";
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_assoc($result)) {
 $category = $row['category'];
 $catId    = $row['id'];  
 echo "<li><input class='catCheck' type='checkbox' name='p_cat[]' value='$catId' /> $category</li>";
    }
  ?>
</ul>

 

 

:shrug:  Thanks guys....

Link to comment
Share on other sites

There needs to be more HTML...

A form tag specifying where and how the data should be be sent, and a submit button.

<?php
foreach($_POST['p_cat'] AS $p_cat){
  echo $p_cat;
}
?>
<ul class="categories">
<form action="" method="post">
  <?php 
    $query = "SELECT * FROM products";
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_assoc($result)) {



$category = $row['category'];



$catId    = $row['id'];  



echo "<li><input class='catCheck' type='checkbox' name='p_cat[]' value='$catId' /> $category</li>";
    }
  ?>
<input type="submit" name="submit" value="submit" />
</form>
</ul>

^ I

Link to comment
Share on other sites

I am aware of that, I was posting only the pertinent code....here is the whole "form" portion:

 

<form id="theform" action="manage-products.php?action=addProduct" method="post">
  <input type="text" name="p_name" id="p_name"/>
  <textarea name="p_desc"></textarea>
  <input "name="submit" type="submit" value="Create Product"/>
  <ul class="categories">
    <?php 
 $query = "SELECT * FROM products";
 $result = mysql_query($query) or die(mysql_error());
 while ($row = mysql_fetch_assoc($result)) {
	 $category = $row['category'];
	 $catId    = $row['id']; 
	echo "<li><input class='catCheck' type='checkbox' name='p_cat[]' value='$catId' /> $category</li>";
 }
    ?> 
  </ul>
</form>

Link to comment
Share on other sites

If you had run the code I gave you earlier, you would have seen I only did one thing wrong and that it answered your problem, though it seems I forgot to mention it (you can see I started saying something under "^ I". It would give you an error about $_POST['p_cat'] not being set if it wasn't set. You also have not connected to MySQL or chosen database.

 

Run this code, and you will see it does what you want. So try to understand it. The solution lies with the foreach loop.

 

<?php
if(isset($_POST['p_cat'])){
foreach($_POST['p_cat'] AS $p_catID){
	echo 'You selected category: '.$p_catID.'.<br/>';
	// mysql_query('UPDATE table SET col = value WHERE id = '.$p_catID);
}
}
?>

<form id="theform" action="" method="post">
  <input type="text" name="p_name" id="p_name"/>
  <textarea name="p_desc"></textarea>
  <input "name="submit" type="submit" value="Create Product"/>
  <ul class="categories">
<?php
//$query = "SELECT * FROM products";
//$result = mysql_query($query) or die(mysql_error());
//while($row = mysql_fetch_assoc($result)){
//	$category = $row['category'];
//	$catId    = $row['id'];
for($i=0; $i<10; $i++){
$rows[$i]['id'] = $i;
$rows[$i]['category'] = 'category '.$i;
}
foreach($rows AS $row){
$catId = $row['id'];
$category = $row['category'];
echo "<li><input class='catCheck' type='checkbox' name='p_cat[]' value='$catId' /> $category</li>";
}
?> 
  </ul>

I added some extra lines of code just while I were testing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.