berry05 Posted May 8, 2009 Share Posted May 8, 2009 Hello, I was wondering how do I make something happen when a check box is ticked and then they click submit... when i load the page it shows the items that can be selled and then it also echoes what i want it to echo but it does it without hitting submit...i just load the page and it does it...I'm kind of confused and need help on this.. thanks! here's my whole sell.php code!! <?php session_start(); if(isset($_SESSION['otherusername'])){ $db=mysql_connect('localhost', 'root', ''); $res=mysql_select_db('textgame',$db) or die(mysql_error()); $otherusername = $_SESSION['otherusername']; $res=mysql_query($otherusername)or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ echo $row['item'] ."<input type='checkbox' name='checkbox[]' id='checkbox'><BR />" ; $item_array[0] = "hoe"; if ( $item_array[0] == 'hoe' ) { echo "Your name is someguy!<br />"; } } }else{ echo "Sorry your not a member please join us!"; } ?> <form id="form1" name="form1" method="post" action=""> <label> <br /> <input type="submit" name="Sell" id="Sell" value="Sell" /> </label> </form> Quote Link to comment Share on other sites More sharing options...
SharkBait Posted May 8, 2009 Share Posted May 8, 2009 if you want something to change during php portion after the form is submitted you would check to see if the value is set: <?php ... if(isset($_POST['mycheckbox'])) { //do stuff here } ?> .. .. <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <input type="checkbox" name-"mycheckbox" /> ... </form> that make sense? Quote Link to comment Share on other sites More sharing options...
berry05 Posted May 8, 2009 Author Share Posted May 8, 2009 Thanks for the reply! Still trying to work on it..i added what you told me to add and still nothing..my code now looks like this <?php session_start(); if(isset($_SESSION['otherusername'])) { $db=mysql_connect('localhost', 'root', ''); $res=mysql_select_db('textgame',$db) or die(mysql_error()); $otherusername = $_SESSION['otherusername']; $res=mysql_query($otherusername)or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ echo $row['item'] ."<input type='checkbox' name='checkbox' id='checkbox'><BR />" ; if(isset($_POST['checkbox'])) { if ( $row['item'] == 'hoe' ) { echo "Your name is someguy!<br />"; } } } }else{ echo "Sorry your not a member please join us!"; } ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Is $_SESSION['otherusername'] set? Also, it's supposed to be a SQL? Quote Link to comment Share on other sites More sharing options...
berry05 Posted May 8, 2009 Author Share Posted May 8, 2009 yes and there's nothing wrong with that part..that part is fine Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Did you mean something like this: <?php session_start(); if (isset($_SESSION['otherusername']) && !empty($_POST['submit'])) { $sell = $_POST['Sell']; $db=mysql_connect('localhost', 'root', ''); $res=mysql_select_db('textgame',$db) or die(mysql_error()); $otherusername = $_SESSION['otherusername']; $res = mysql_query($otherusername) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ $item = $row['item']; echo $item, '<input type="checkbox" name="checkbox[]" id="checkbox"', ($row['item'] === $sell? ' selected="selected"' : ''), '><br />'; } } else { echo 'Sorry you\'re not a member. Please join us!'; } ?> <form id="form1" name="form1" method="post"> <label> <br /> Sell: <input type="submit" name="Sell" id="Sell" value="Sell" /> </label> <label> <input type="submit" name="submit" value="submit" /> </label> </form> Quote Link to comment Share on other sites More sharing options...
berry05 Posted May 9, 2009 Author Share Posted May 9, 2009 I'm almost there! How do I make a if statement that if the item is a hoe and it is selected and the user clicks submit it says Hoe Sold! Thanks! Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 9, 2009 Share Posted May 9, 2009 Change $sell = $_POST['Sell']; To $sell = 'hoe'; And just use an echo to print that out. Quote Link to comment Share on other sites More sharing options...
berry05 Posted May 10, 2009 Author Share Posted May 10, 2009 i tried to add a echo and i got Sorry you're not a member. Please join us! when i tried to sell the item... Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Can you post your updated code? Quote Link to comment Share on other sites More sharing options...
berry05 Posted May 10, 2009 Author Share Posted May 10, 2009 <?php session_start(); if (isset($_SESSION['otherusername']) && !empty($_POST['submit'])) { $sell = 'hoe'; $db=mysql_connect('localhost', 'root', ''); $res=mysql_select_db('textgame',$db) or die(mysql_error()); $otherusername = $_SESSION['otherusername']; $res = mysql_query($otherusername) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ $item = $row['item']; echo $item, '<input type="checkbox" name="checkbox[]" id="checkbox"', ($row['item'] === $sell? ' selected="selected"' : ''), '><br />'; echo "Hoe Sold"; } } else { echo 'Sorry you\'re not a member. Please join us!'; } ?> <form id="form1" name="form1" method="post"> <label> <br /> Sell: <input type="submit" name="Sell" id="Sell" value="Sell" /> </label> <label> <input type="submit" name="submit" value="submit" /> </label> </form> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 <?php session_start(); echo 'sess - ', $_SESSION['otherusername']; if (isset($_SESSION['otherusername']) && !empty($_POST['submit'])) { $sell = 'hoe'; $db=mysql_connect('localhost', 'root', ''); $res=mysql_select_db('textgame',$db) or die(mysql_error()); $otherusername = $_SESSION['otherusername']; $res = mysql_query($otherusername) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ $item = $row['item']; echo $item, '<input type="checkbox" name="checkbox[]" id="checkbox"', ($row['item'] === $sell? ' selected="selected"' : ''), '><br />'; // echo "Hoe Sold"; } } else { echo 'Sorry you\'re not a member. Please join us!'; } ?> <form id="form1" name="form1" method="post"> <label> <br /> Sell: <input type="submit" name="Sell" id="Sell" value="Sell" /> </label> <label> <input type="submit" name="submit" value="submit" /> </label> </form> What does this print out? Quote Link to comment Share on other sites More sharing options...
berry05 Posted May 10, 2009 Author Share Posted May 10, 2009 prints this out sess - SELECT item FROM users_items WHERE username='berry05'Sorry you're not a member. Please join us! Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Oh huh... so what's the sell input box supposed to do? Like what's the purpose of the input field? Quote Link to comment Share on other sites More sharing options...
berry05 Posted May 10, 2009 Author Share Posted May 10, 2009 lol...sry if i didnt explain this good enough..ok..so there's a query that shows the fields of a table and shows all the items that user thats logged in has...once it showed up there's a checkbox to each item..if a user wants to sell that item he checks it off and clicks sell....once he clicks sell it echoes saying you've sold "whatever item"! and then i can take care of the rest like updating the database to the users gold and stuff..i can do that part.... Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 OH! Try this - <?php session_start(); $db = mysql_connect('localhost', 'root', ''); $res = mysql_select_db('textgame',$db) or die(mysql_error()); if (!empty($_POST['Sell']) && !empty($_POST['checkbox'])) { $sold_items = $_POST['checkbox']; foreach ($sold_items as $val) { echo 'You sold ', $val, '<br />'; } } ?> <form id="form1" name="form1" method="post"> <?php if (!empty($_SESSION['otherusername'])) { $otherusername = $_SESSION['otherusername']; $items = ''; $res = mysql_query($otherusername) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ $items .= $item . ' <input type="checkbox" name="checkbox[]" id="checkbox"><br />'; } echo empty($items)? 'You don\'t have any items to sell.' : $items; } else { echo 'You\'re not a member. Please join us!'; } ?> <input type="submit" name="Sell" id="Sell" value="Sell" /> </form> Quote Link to comment Share on other sites More sharing options...
berry05 Posted May 10, 2009 Author Share Posted May 10, 2009 now it shows the two checkbox's but doesn't show the items name next to it >_> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 10, 2009 Share Posted May 10, 2009 Oh sorry.. I took out one thing by mistake. Sorry. <?php session_start(); $db = mysql_connect('localhost', 'root', ''); $res = mysql_select_db('textgame',$db) or die(mysql_error()); if (!empty($_POST['Sell']) && !empty($_POST['checkbox'])) { $sold_items = $_POST['checkbox']; foreach ($sold_items as $val) { echo 'You sold ', $val, '<br />'; } } ?> <form id="form1" name="form1" method="post"> <?php if (!empty($_SESSION['otherusername'])) { $otherusername = $_SESSION['otherusername']; $items = ''; $res = mysql_query($otherusername) or die(mysql_error()); while($row = mysql_fetch_assoc($res)){ $items .= $row['item'] . ' <input type="checkbox" name="checkbox[]" id="checkbox"><br />'; } echo empty($items)? 'You don\'t have any items to sell.' : $items; } else { echo 'You\'re not a member. Please join us!'; } ?> <input type="submit" name="Sell" id="Sell" value="Sell" /> </form> Quote Link to comment 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.