Jump to content

how to make a checkbox do something


berry05

Recommended Posts

I have check boxes next to each item when I call up the items from mySQL . How do i make the item disappear in the mySQL database when i user ticks the box and clicks "sell item"? I've been having trouble on that ..

<html>
<body>
<p><a href="index2.php">Index</a> | <a href="shop.php">Shop</a> | <a href="sell.php">Sell</a> | <a href="logout.php">Logout</a>
</p>
</p> 

<?php 

session_start();
$checked = $_POST['checked'];

if(isset($_SESSION['otherusername'])){

$db=mysql_connect('localhost', 'root', '');

$res=mysql_select_db('txtgame',$db) or die(mysql_error());

    
$otherusername = $_SESSION['otherusername']; //"SELECT item FROM users_items WHERE username='".$Username."'";
    
    $res=mysql_query($otherusername)or die(mysql_error());
    
  
    while($row = mysql_fetch_assoc($res)){


echo ' <input type="checkbox" name="item[]" value="items" />'. ' '.$row['item']."<BR />";

  }
  

  
}else{
   
   echo "Sorry your not a member please join us!";
}
?>

<br><input type="submit" value="sellitem">
</form>
</body>
</html>

 

 

Thanks !

Link to comment
https://forums.phpfreaks.com/topic/170576-how-to-make-a-checkbox-do-something/
Share on other sites

Pretty easy with AJAX - if you want it live (i.e. Click the checkbox and it instantly disappears)

 

But... I think I might have misread your post when I moved it.

 

If you want it when they click the submit button, you need to use a foreach loop to get each checkbox value....

if(isset($_POST['item']) && is_array($_POST['item'])) {
    // there were some items selected:
    foreach($_POST['item'] as $item) {
        // your query string here...
    }
}

 

And the syntax past that is based on how you have your DB setup.

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.