berry05 Posted August 17, 2009 Share Posted August 17, 2009 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/170576-how-to-make-a-checkbox-do-something/ Share on other sites More sharing options...
taquitosensei Posted August 17, 2009 Share Posted August 17, 2009 it will require either onClick posting it or onClick combined with ajax. onClick='this.form.submit();' then checking for the post and doing whatever you need to with the database. Quote Link to comment https://forums.phpfreaks.com/topic/170576-how-to-make-a-checkbox-do-something/#findComment-899748 Share on other sites More sharing options...
Philip Posted August 17, 2009 Share Posted August 17, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/170576-how-to-make-a-checkbox-do-something/#findComment-899751 Share on other sites More sharing options...
berry05 Posted August 17, 2009 Author Share Posted August 17, 2009 I'm sorry but i wanted to post this in the PHP section, no AJAX is included...if someone could move this thread that would be great! Quote Link to comment https://forums.phpfreaks.com/topic/170576-how-to-make-a-checkbox-do-something/#findComment-900051 Share on other sites More sharing options...
taquitosensei Posted August 17, 2009 Share Posted August 17, 2009 you can't do what you're wanting to do with php only. You have to have some javascript involved. Quote Link to comment https://forums.phpfreaks.com/topic/170576-how-to-make-a-checkbox-do-something/#findComment-900055 Share on other sites More sharing options...
Philip Posted August 17, 2009 Share Posted August 17, 2009 My apologies for the misunderstanding. It is very possible to do this without ajax - as long as they use a submit button and it isn't a live update (there has to be a fresh page loaded) See my post above for a code example. Quote Link to comment https://forums.phpfreaks.com/topic/170576-how-to-make-a-checkbox-do-something/#findComment-900458 Share on other sites More sharing options...
taquitosensei Posted August 17, 2009 Share Posted August 17, 2009 I guess I misunderstood I thought he wanted it to happen when they clicked the checkbox. Quote Link to comment https://forums.phpfreaks.com/topic/170576-how-to-make-a-checkbox-do-something/#findComment-900468 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.