Guber-X Posted April 18, 2012 Share Posted April 18, 2012 Hey phpFreaks, im having some troubles getting my script to work correctly and im also not sure if this issue is in the right section of the forum. but heres what i have going on. I have a query result that displays a list of images with a checkbox and a couple buttons for edit and delete. everything works fine other than the checkbox stuff. I had it working when i was using a submit button, but i wanted to get rid of that button cuz it was only dealing with the checkboxes. so whats going on when using the checkboxes is that when checked or uncheck it would submit the form. It works to submit but its not submitting any data to the database. heres what i got for code for this section of checkbox. <?php echo '<form method="post">'; if(isset($_POST['submit'])){ foreach($_POST['id'] as $id){ $value = (isset($_POST['location'][$id]) && $_POST['location'][$id]=="0" ? '0' : '1'); $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error()); } } $result = mysql_query("SELECT * FROM items") or die("Query Failed: ".mysql_error()); $counter = 0; echo '<div class="specialcontainer">'; while($row = mysql_fetch_array($result)){ list($id, $item_info, $item_img, $price, $sale, $location) = $row; if($location == '0'){ $set_checked = 'checked="checked"'; }else{ $set_checked = ''; } if($counter % 5==0) { echo '</div>'; echo '<div class="specialcontainer">'; } echo '<div class="special"><img src="../images/items/'.$item_img.'" width="130" /><br />'.$item_info.'<br />$'.$price.'<br />$'.$sale.'<br />Slide Show: <input type="checkbox" id='.$id.' value="0" name="location['.$id.']" '.$set_checked.' onchange="this.form.submit()"/><br /><input type="button" value="Edit" name="edit" id="'.$id.'" onclick="window.location.href=\'specials.php?action=edit&id='.$id.'\'"><input type="button" value="Delete" name="Delete" id="'.$id.'" onclick="window.location.href=\'specials.php?action=delete&id='.$id.'\'"><input type="hidden" name="id[]" value='.$id.' /></div>'; $counter++; } echo '</div>'; echo '</form>'; ?> Quote Link to comment Share on other sites More sharing options...
MMDE Posted April 18, 2012 Share Posted April 18, 2012 If I understand you correctly, then I don't see what this has to do with PHP. It seems like you are requesting someone to help you with some JavaScript. You code is a mess though. :s You should specify the action parameter in the form. You should also give it an id. <form action="" id="cb" method="post"> See if you can make any sense of what I just scrambled down, I think it got the functionality that you are looking for. <?php $cbchecked = ''; if(isset($_POST['cb']) && $_POST['cb']=='checked'){ $cbchecked = 'checked="checked" '; } echo '<form action="" id="cbf" method="post"> <input type="checkbox" name="cb" value="checked" '.$cbchecked.'onchange="document.getElementById(\'cbf\').submit();" /> check me out! </form>'; ?> Quote Link to comment Share on other sites More sharing options...
Guber-X Posted April 18, 2012 Author Share Posted April 18, 2012 what u just replied with i already have working, i just used it in a different way. what im trying to get to work is the "onChange" event to send the "post" information. so for instance: Unchecked box = 1 Checked box = 0 when clicking on a Checked box to make it a Unchecked box it will post a 1 to the database query. for my issue it doesnt post data... just reloads page. I had this working when using a submit button, but i didnt want the submit button no more for use with checkboxes... heres a cleaner version of my code: <?php // Sends information on Submit if(isset($_POST['submit'])){ foreach($_POST['id'] as $id){ $value = (isset($_POST['location'][$id]) && $_POST['location'][$id]=="0" ? '0' : '1'); $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error()); } } echo '<form action="" id="images" method="post">'; // Results from database array $result = mysql_query("SELECT * FROM items") or die("Query Failed: ".mysql_error()); // Counter for array to align items properly $counter = 0; echo '<div class="specialcontainer">'; while($row = mysql_fetch_array($result)){ list($id, $item_info, $item_img, $price, $sale, $location) = $row; // Checks to see if the items are suppose to have a check mark or not if($location == '0'){ $set_checked = ' checked="checked" '; }else{ $set_checked = ''; } if($counter % 5==0) { echo '</div>'; echo '<div class="specialcontainer">'; } echo '<div class="special"><img src="../images/items/'.$item_img.'" width="130" /><br />'; echo $item_info.'<br />'; echo 'Reg. $'.$price.'<br />'; echo 'Sale $'.$sale.'<br />'; // This line below is my checkbox line where i want to submit on change aka when checked or unchecked echo 'Slide Show: <input type="checkbox" value="0" name="location['.$id.']"'.$set_checked.'onchange="this.form.submit();"/><br />'; echo '<input type="button" value="Edit" name="edit" onclick="window.location.href=\'specials.php?action=edit&id='.$id.'\'">'; echo '<input type="button" value="Delete" name="Delete" onclick="window.location.href=\'specials.php?action=delete&id='.$id.'\'">'; echo '<input type="hidden" name="id[]" value='.$id.' />'; echo '</div>'; $counter++; } echo '</div>'; echo '</form>'; ?> Quote Link to comment Share on other sites More sharing options...
MMDE Posted April 19, 2012 Share Posted April 19, 2012 what u just replied with i already have working, i just used it in a different way. what im trying to get to work is the "onChange" event to send the "post" information. so for instance: Unchecked box = 1 Checked box = 0 when clicking on a Checked box to make it a Unchecked box it will post a 1 to the database query. for my issue it doesnt post data... just reloads page. I had this working when using a submit button, but i didnt want the submit button no more for use with checkboxes... heres a cleaner version of my code: <?php // Sends information on Submit if(isset($_POST['submit'])){ foreach($_POST['id'] as $id){ $value = (isset($_POST['location'][$id]) && $_POST['location'][$id]=="0" ? '0' : '1'); $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error()); } } echo '<form action="" id="images" method="post">'; // Results from database array $result = mysql_query("SELECT * FROM items") or die("Query Failed: ".mysql_error()); // Counter for array to align items properly $counter = 0; echo '<div class="specialcontainer">'; while($row = mysql_fetch_array($result)){ list($id, $item_info, $item_img, $price, $sale, $location) = $row; // Checks to see if the items are suppose to have a check mark or not if($location == '0'){ $set_checked = ' checked="checked" '; }else{ $set_checked = ''; } if($counter % 5==0) { echo '</div>'; echo '<div class="specialcontainer">'; } echo '<div class="special"><img src="../images/items/'.$item_img.'" width="130" /><br />'; echo $item_info.'<br />'; echo 'Reg. $'.$price.'<br />'; echo 'Sale $'.$sale.'<br />'; // This line below is my checkbox line where i want to submit on change aka when checked or unchecked echo 'Slide Show: <input type="checkbox" value="0" name="location['.$id.']"'.$set_checked.'onchange="this.form.submit();"/><br />'; echo '<input type="button" value="Edit" name="edit" onclick="window.location.href=\'specials.php?action=edit&id='.$id.'\'">'; echo '<input type="button" value="Delete" name="Delete" onclick="window.location.href=\'specials.php?action=delete&id='.$id.'\'">'; echo '<input type="hidden" name="id[]" value='.$id.' />'; echo '</div>'; $counter++; } echo '</div>'; echo '</form>'; ?> My script does send the POST data. I think in your case I'm not sure if the submit button gets sent along with it, but I could be wrong, but by the looks of it, that looks important. Quote Link to comment Share on other sites More sharing options...
Guber-X Posted April 19, 2012 Author Share Posted April 19, 2012 well i have been doing a lot of research on this and from what i understand, the onchange with either this.form.submit() or document.getElementById().submit() do not work with the post handler. my way of testing this was I put the "die;" syntax at the end of my submit query. so if i was to check a checkbox my page would basicly show up blank afterwards. and as a result, it displayed everything. so it is not sending anything to the isset($_POST['submit']). i heard something about using jQuery for this, but i dont understand jQuery enough to even get started haha Quote Link to comment Share on other sites More sharing options...
Barand Posted April 14, 2013 Share Posted April 14, 2013 Try "onclick" instead of "onchange" for the checkbox. Not sure checkboxes support onchange events. I also see you are processing $_POST['submit']. If you have a form element named submit (bad idea for this reason) you'll have problems with form.submit(). Whether or not a submit button is posted when not clicked is browser-dependent 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.