JavaScript - Is It Possible To Send A Checkbox Value To The Db When Checked
Is it possible to send a checkbox value to the DB when checked and or unchecked. I mean when you Physically chage it's state.
I am running a PHP project, the page I need this functionality on has a dynamically built table from the Database. What I need to do is send the value "1" to the DB if I check on one of the rows "Published" checkbox and vice versa, removing the value of 1 from the db and changing the state to unchecked. The reason I ask in the JS forum is that PHP is a server side language and as such may not submit the change in realtime. Any help and or suggestions is much appreciated. Similar TutorialsSo here's what i want to do: i have 2 checkboxes, when Checkbox A is checked, i want to automatically check the checkbox B. When A is unchecked, then uncheck B how can i do that? thanks a lot ! hi i was wondering if there was anyway i can check a check box with the help of javascript and have it add text from a database record to a text box without submitting the page with the help from php? i have the php coding done to get the info and edit it but cant have no idea how to do the javascript code thanks. PHP code: PHP Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <div id="user_pm"> <?php $sql="SELECT * FROM list WHERE username='usernamehere'"; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <table width="496" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action=""> <table width="495" border="0" cellpadding="3" cellspacing="1" bgcolor="#000000"> <tr> <td align="left" bgcolor="#3576B4" width="26"><font color="#FFFFFF" size="2"><strong></strong></td> <td align="left" bgcolor="#3576B4" width="95"><font color="#FFFFFF" size="2"><strong>From:</strong></font></td> <td align="left" bgcolor="#3576B4" width="52"><font color="#FFFFFF" size="2"><strong>To:</strong></font></td> <td align="left" bgcolor="#3576B4" width="161"><font color="#FFFFFF" size="2"><strong>Message:</strong></font></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center" bgcolor="#f8f8f8" width="26"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>" style="float: left"></td> <td bgcolor="#f8f8f8" width="95"><font size="2"><?php echo $rows['from']; ?></font></td> <td bgcolor="#f8f8f8" width="52"><font size="2"><?php echo $rows['to']; ?></font></td> <td bgcolor="#f8f8f8" width="161"><font size="2"><?php echo $rows['message']; ?></font></td> </tr> <?php } ?> <tr> <td colspan="6" align="center" bgcolor="#f8f8f8"> <input name="edit" type="submit" id="edit" value="Edit" style="float: left"></td> </tr> <?php $message = stripslashes($post['message']); $message = $_POST['message']; $checkbox = $_POST['checkbox']; $edit = $_POST['edit']; if($edit){ for($i=0;$i<$count;$i++){ $edit_id = $checkbox[$i]; mysql_query("UPDATE list SET message='$message' WHERE id='$edit_id' And username='usernamehere'"); } if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;url=index.php\">\n"; } } mysql_close(); ?> </table> </form> </td> </tr> </table> </div> </body> </html> Sorted. Close thread please. New problem in post below VVV I'm trying to find the best approach for saving the checked value of a checkbox in SQL With this script the FieldName "Active" toggles yes/no onClick... That we can post to the database no problem... What I am having trouble with is saving the checked value. I want to make the FieldName Active =hidden, and only display the checkbox with correct value checked yes/no. Code: <script type="text/javascript"> var YesOrNo = (function() { if(document.select.checkbox.checked) { document.select.Active.value = 'yes'; } else { document.select.Active.value = 'no'; } }); </script> <form name="select"> <input type="hidden" name="Active" size="10" /> <input type="checkbox" name="checkbox" onclick="YesOrNo();" value="on" /> </form> I am using 2-3 checkboxes in my VB.Net program.When I run the code in Firefox, I can not check or uncheck the checkboxes.I am using firebug,Still it does not show any error. It works fine in google Chrome.. Plz reply......... Hi, Have searched the forum and found a soloution that probably only needs some modification. I have a number of question with related alternatives in a db that I display on a webpage. I wan't to force the user to select an alternative. I use checkboxes because sometimes more then one answer is correct. I have found a bit of code that lets me do this. The problem is that sometimes the alterative to the question is 3 and sometimes 5 (I don't know beforehand how many alterantives a question has). So is there a way to modify the code to work if there are 3 checkboxes or 5 checkboxes and so on...? The code below only works with a fixed number of alternatives. Code: <form action="../" onsubmit="return checkCheckBoxes(this);"> <p><input type="CHECKBOX" name="CHECKBOX_1" value="This..."> This...</p> <p><input type="CHECKBOX" name="CHECKBOX_2" value="That..."> That...</p> <p><input type="CHECKBOX" name="CHECKBOX_3" value="...and The Other"> ...and The Other</p> <p><input type="SUBMIT" value="Submit!"></p> </form> Code: <script type="text/javascript" language="JavaScript"> <!-- function checkCheckBoxes(theForm) { if ( theForm.CHECKBOX_1.checked == false && theForm.CHECKBOX_2.checked == false && theForm.CHECKBOX_3.checked == false) { alert ('You didn\'t choose any of the checkboxes!'); return false; } else { return true; } } //--> </script> Need a javascript function... If a checkbox is checked, then corresponding dropdown selection is must... Please help me with this... Thanks Reply With Quote hi guys, i need a javascript for my checkbox 1st: need to check the checkbox to go to next page or else stay at current page with an alert popout "Please check the checkbox" can some one help me please? Hi I am very new to Javascript and I was wondering if anyone could help. This is my jQuery code: Code: $(document).ready(function(){ if("input.cf_flt:checked") { $(".filterbox").show() } else { $(".filterbox").hide() } $(".filtertoggle").click(function() { $(this).next(".filterbox").slideToggle(500); }); }); HTML (sorry, quite a lot) Code: <h3 class="filtertoggle">Filter These Results <span>Click Here</span></h3> <div class="filterbox"> <form name="cf_form" action="index.php" method="get" class="cf_form topfilter"> <div class="cf_flt_wrapper"> <div class="cf_flt_header" id="cfhead_0"> Grape </div> <div class="cf_wrapper_inner" id="cf_wrapper_inner_0"> <ul class="cf_filters_list"><li><input onclick="window.location='/eden/index.php?custom_f_17[0]=50726f63616e69636f2c2056657264656c6c6f2c2047726563686574746f2c204d616c766173696120546f7363616e61&option=com_customfilters&view=products';" type="checkbox" name="custom_f_17[]"class="cf_flt" id="flt_50726f63616e69636f2c2056657264656c6c6f2c2047726563686574746f2c204d616c766173696120546f7363616e61" value="50726f63616e69636f2c2056657264656c6c6f2c2047726563686574746f2c204d616c766173696120546f7363616e61" /><label class="" for="flt_50726f63616e69636f2c2056657264656c6c6f2c2047726563686574746f2c204d616c766173696120546f7363616e61"><a href="/eden/index.php?custom_f_17[0]=50726f63616e69636f2c2056657264656c6c6f2c2047726563686574746f2c204d616c766173696120546f7363616e61&option=com_customfilters&view=products" class="cf_option ">Procanico, Verdello, Grechetto, Malvasia Toscana</a></label></li><li><input onclick="window.location='/eden/index.php?custom_f_17[0]=53616e67696f76657365&option=com_customfilters&view=products';" type="checkbox" name="custom_f_17[]"class="cf_flt" id="flt_53616e67696f76657365" value="53616e67696f76657365" /><label class="" for="flt_53616e67696f76657365"><a href="/eden/index.php?custom_f_17[0]=53616e67696f76657365&option=com_customfilters&view=products" class="cf_option ">Sangiovese</a></label></li></ul> </div> </div> <div class="cf_flt_wrapper"> <div class="cf_flt_header" id="cfhead_1"> Bottle Size </div> <div class="cf_wrapper_inner" id="cf_wrapper_inner_1"> <ul class="cf_filters_list"><li><input onclick="window.location='/eden/index.php?custom_f_18[0]=3735636c&option=com_customfilters&view=products';" type="checkbox" name="custom_f_18[]"class="cf_flt" id="flt_3735636c" value="3735636c" /><label class="" for="flt_3735636c"><a href="/eden/index.php?custom_f_18[0]=3735636c&option=com_customfilters&view=products" class="cf_option ">75cl</a></label></li></ul> </div> </div> <div class="cf_flt_wrapper"> <div class="cf_flt_header" id="cfhead_2"> ABV </div> <div class="cf_wrapper_inner" id="cf_wrapper_inner_2"> <ul class="cf_filters_list"><li><input onclick="window.location='/eden/index.php?custom_f_16[0]=31322e3525&option=com_customfilters&view=products';" type="checkbox" name="custom_f_16[]"class="cf_flt" id="flt_31322e3525" value="31322e3525" /><label class="" for="flt_31322e3525"><a href="/eden/index.php?custom_f_16[0]=31322e3525&option=com_customfilters&view=products" class="cf_option ">12.5%</a></label></li><li><input onclick="window.location='/eden/index.php?custom_f_16[0]=313325&option=com_customfilters&view=products';" type="checkbox" name="custom_f_16[]"class="cf_flt" id="flt_313325" value="313325" /><label class="" for="flt_313325"><a href="/eden/index.php?custom_f_16[0]=313325&option=com_customfilters&view=products" class="cf_option ">13%</a></label></li><li><input onclick="window.location='/eden/index.php?custom_f_16[0]=31332e3525&option=com_customfilters&view=products';" type="checkbox" name="custom_f_16[]"class="cf_flt" id="flt_31332e3525" value="31332e3525" /><label class="" for="flt_31332e3525"><a href="/eden/index.php?custom_f_16[0]=31332e3525&option=com_customfilters&view=products" class="cf_option ">13.5%</a></label></li></ul> </div> </div> <div class="cf_flt_wrapper"> <div class="cf_flt_header" id="cfhead_3"> Vintage </div> <div class="cf_wrapper_inner" id="cf_wrapper_inner_3"> <ul class="cf_filters_list"><li><input onclick="window.location='/eden/index.php?custom_f_15[0]=32303038&option=com_customfilters&view=products';" type="checkbox" name="custom_f_15[]"class="cf_flt" id="flt_32303038" value="32303038" /><label class="" for="flt_32303038"><a href="/eden/index.php?custom_f_15[0]=32303038&option=com_customfilters&view=products" class="cf_option ">2008</a></label></li><li><input onclick="window.location='/eden/index.php?custom_f_15[0]=32303130&option=com_customfilters&view=products';" type="checkbox" name="custom_f_15[]"class="cf_flt" id="flt_32303130" value="32303130" /><label class="" for="flt_32303130"><a href="/eden/index.php?custom_f_15[0]=32303130&option=com_customfilters&view=products" class="cf_option ">2010</a></label></li></ul> </div> </div> <input type="hidden" name="option" value="com_customfilters" /> <input type="hidden" name="view" value="products" /> </form> </div> Basically if the checkbox with a class of cf_flt is checked then I want the div '.filterbox' to show. if the checkbox is unchecked then I want it to hide. As well as this I have a toggle button called '.filtertoggle' which toggles the '.filterbox' At the moment the '.filterbox' shows regardless of whether or not the checkbox is checked. The toggle button works correctly. If you know how to fix this then I would be very grateful. Thank you. hello I have a list of checkbox called de[] i use [] for create array in php so i have a button called "delete" so i want to activate the button if almost one of checkbox has been checked other way the input button will be disable. sorry for my english jejejeje. Regards ok so here is my situation i have 4 hidden fields in a form called "searchform" Code: <input type="hidden" name="forumchoice[]" value="1" checked /> <input type="hidden" name="forumchoice[]" value="2" checked /> <input type="hidden" name="forumchoice[]" value="3" checked /> <input type="hidden" name="forumchoice[]" value="4" checked /> Now here's what i want to do: i have two checkbox Code: <input onclick="??????????" type="checkbox" name="checkbox1" id="checkbox1"/> <input onclick="??????????" type="checkbox" name="checkbox2" id="checkbox2"/> - when checkbox1 is checked, i want to activate the hidden inputs 1 & 2. if it is unchecked, then desactivate hidden inputs 1&2 - when checkbox2 is checked, i want to activate the hidden inputs 3 & 4. if it is unchecked, then desactivate hidden inputs 3&4 could someone help me with the "onclick" code i would need to do this ?? thanks a lot!! I've not really an idea how to do this. I know how to do it in PHP, but I need it dynamic <input type="checkbox" name="facebook" value="1" /> if($_POST['facebook'] == 1) echo "<input type='text' name='facebook_url' />"; Could someone tell me how I should do this. Thanks. Why my checkbox remain checked even i refresh my browser ?
I was wondering if anyone could help me with getting this working. I'm using html to create a table which has at least 1 row but no max number of rows - as they can be added/removed by the user - and 4 columns, 1st contains a checkbox and the last 2 contain drop down menus. I wish to be able to store the values of those menus in an array only if that row's checkbox has been checked. e.g if the first row has 1 and A as it's dropdown values and the second row has 2 and B. If only row 1 has been checked the array should only contain [1,A]. Instead mine holds all the values including the non-checked ones i.e [1,A,2,B]. My code for javascript function and html table are below: Code: function calculate(textID){ var table = document.getElementById('course'); //id of table var rowCount = table.rows.length; var array = []; //array to hold the values var c = 0; dmenus = document.getElementByTagName("select"); //get the drop down menus for(var a = 1; a < rowCount; i++){ //a = 1 as the 1st row contains column headings var row = table.rows[a]; var check = row.cells[0].childNodes[0]; if(null != check && true == check.checked){ for(var b = 0; b < dmenus.length; b++){ val = dmenus[b].options[dmenus[b].selectedIndex].value; array[c] = val; //set index of array to equal value of dropdown box c++; } }else{ b++; } } Html code for table: Code: <table id="course"> <tr> <td><input type="checkbox" name="ucheck" id="ucheck" onclick="checkAll('course')"/></td> <th style="color:white">Course Title</th> <th style="color:white">Credits</th> <th style="color:white">Grade</th> </tr> <tr> <td><input type="checkbox" name="tick" id="tick"/></td> <td><input type="text"/></td> <td> <select name="credits" id="credits"> <option...</select></td> <td> <select name="grade" id="grade"> <option...</td> It works only for the first row but if there are more than 1 row it doesn't do what it's supposed to. The coding platform I'm working on does not seem to support jquery so javascript code will be most appreciated. Thanks Hello, i'm working on a 3 page survey. When hitting next, previous, or submit it passes the values of all the questions to the next page. I've got the whole thing working correcting except for one thing: When the box is "not" checked it passes no value. I'm needing it to have a value of "1" when checked and a value of "0" when not checked, and currently when its not checked and i pass the info it leaves it blank. I'd post the whole code to one of the pages but it's long , so i'll post the snipits of the code. Code: <script type="text/javascript"> /* <![CDATA[ */ function processQueryString() { var formData = location.search; formData = formData.substring(1, formData.length); while (formData.indexOf("+") != -1) { formData = formData.replace("+", " "); } formData = unescape(formData); var formArray = formData.split("&"); for (var i=0; i < formArray.length; ++i) { //document.writeln(formArray[i] + "<br />"); var sDataName = formArray[i].split("=") switch (sDataName[0]) { case ".lib_use": for (var j=0; j < document.getElementsByName(".lib_use").length; ++j) { if (document.getElementsByName(".lib_use").item(j).value == sDataName[1]) { document.getElementsByName(".lib_use").item(j).checked = true; //alert("lib_use set"); } } break; case ".lib_comp": if (sDataName[1] == 1) { document.getElementsByName(".lib_comp").checked = true; document.getElementsByName(".lib_comp").value= 1; } else { document.getElementsByName(".lib_comp").checked = false; document.getElementsByName(".lib_comp").value= 0; } break; default: alert("not caught = " + sDataName[0]); continue; } } } /* ]]> */ </script> <input type="checkbox" name=".lib_comp" id="lib_comp" value="1" /> The first case that i showed in my code is a radio button, and it passes correctly, i just wanted to show the "format" i was using in a working sense. The 2nd case is an example of the check boxes. Thanks for looking at this, and giving any suggestions you might have! I have a bunch of checkboxes like below that the user can check some or all and click the button and see the values of all the selected checkboxes. How can I do that? Code: <script> function alertValues(){ } </script> <input type="checkbox" class ="normal2" value="131971" name="list[]" > <input type="checkbox" class ="normal2" value="131973" name="list[]" > <input type="checkbox" class ="normal2" value="131975" name="list[]" > <input type="checkbox" class ="normal2" value="131977" name="list[]" > <input type="button" onClick="alertValues()" So I have a form. Unfortunately, I'm not using a <form> tag, it's all div. Code: <div id="mainForm"> <div class="label"><input type="checkbox" id="LeftFixed" />Left Bar Always On Screen</div> What I'm trying to do is if that box is checked, execute a javascript. How do I do that? so if checked, execute 'sources/leftbar.js' or I could even just copy the code so it's inline, and not called. Is there a way to do this? Hello! Thanks in advance for your help. I want to begin with the box checked (Yes, I want to subscribe to magazine) 1. The box is checked then go to email 1 (Thanks for subscribing) 2. The box has been unchecked then go to email 2 (Perhaps you need more info) Pat Hi, I'm using JavaScript to check (or leave empty) a checkbox based on stored data. Code: for (var q=0;q<document.Simple.Subject.length;q++) { if (document.Simple.Subject[q].value == document.Restored.Subject.value) { document.Simple.Subject[q].checked = true; break; } It works perfectly, but I've discovered that it adds a simple "checked" to the form: Code: <input type="checkbox" name="Subject" value="Math" checked /> ... I thought I read that checked is not proper... it the attribute should really be checked="checked" . Is there a way to tweak this with JavaScript? Thanks. ~Wayne Thanks for clicking on this to possibly help me. I've been trying for hours to get my page to do one calculations when a radio is clicked... or to do another if a different radio is clicked. any help would be GREATLY appreciated. here is a simplified version of the code (it'd be pointless to give you the whole code, it's way too big.) Code: <html> <head> <title>This doesn't work...</title> </head> <body> <script type="text/javascript"> function doit(){ var firstnum = form.num1.value - form.num2.value; var secondnum = form.num1.value + form.num2.value; if (form.op1.checked){ form.results.value = firstnum; } if (form.op2.checked){ form.results.value = secondnum; } } </script> <form name="form" method="post"> Num 1: <input type="text" name="num1" /><br /> Num 2: <input type="text" name="num2" /><br /> Option One <input type="radio" name="op1" /><br /> Option Two <input type="radio" name="op2" /><br /> <input type="submit" onClick="doit" /><br /> Results: <input type="text" name="results" /><br /> </form> </body> </html> |