-1

I tried to find help via the search function on here but all the answers given to similar problems were too elaborate for me to understand, i.e. the example code was too complex for me to extract the parts which could have been relevant for my problem :(

I have a html form which sends userinput on a specific row in a datatable via an ajax-request to a php file, where the input gets inserted into my sqldb. I have no problem sending the textinput entered by a user and also transferring additional infos like the specific row they were on, or the network account of the user. But i now want to add a checkbox, so the users can choose whether their comment is private or public. However i somehow cannot transmit the value from the checkbox, there is no error but also no checkboxdata inserted into the db. Do i have to handle checkboxes differently than textareas? I'd be very grateful for help!

My code looks as follows: Html:

        function insertTextarea() {

                        var boardInfo = $( "<form id='boardComment'><textarea rows='2' cols='30'>Notizen? Fragen? Kommentare?</textarea>Privat:<input type='checkbox' name='privatcheckbox' value='private'><input type='submit' value='Submit'><input type='reset' value='Cancel'></form>");   

                        $( this ).parent().append(boardInfo);
                        $("tbody img").hide();

                        $("#boardComment").on( "submit", function( event ) {
                            event.preventDefault(); 
                            var change_id = {};
                            change_id['id'] = $(this).parent().attr("id");
                            change_id['comment'] = $(this).find("textarea").val();
                            change_id['privatecheckbox'] = $(this).find("checkbox").val();

                            if( $(this).find("textarea").val() ) {
                                $.ajax({
                                    type: "POST",
                                    url: "boardinfo.php",
                                    cache: false,
                                    data: change_id,
                                    success: function( response2 ) {
                                        alert("Your comment has been saved!");
                                        $("tbody img").show();
                                        $("#" + change_id['id']).find("form").remove();
                                    }
                                }); 
                            };
                        });

and this is the php part:

$id = mysql_real_escape_string($_POST['id']);
$comment = mysql_real_escape_string($_POST['comment']);
$privatecheckbox = mysql_real_escape_string($_POST['privatecheckbox']);
$sql="INSERT INTO cerberus_board_info (BOARD_INFO_COMMENTS, BOARD_INFO_USER, BOARD_INFO_CHANGE_ID, BOARD_INFO_ENTRY_CHANNEL, BOARD_INFO_PRIVACY_LEVEL) VALUES ('$comment', '$ldapdata', '$id', 'Portal', '$privatecheckbox')"; 
sardine
  • 157
  • 1
  • 17

1 Answers1

0

The following line:

change_id['privatecheckbox'] = $(this).find("checkbox").val();

Searches for a element with the tagname checkbox. Such an element doesn't exist, I believe you are trying to search for an <input> element with a type of checkbox.

The following should work for you:

change_id['privatecheckbox'] = $(this).find("input[type=checkbox]").val();

Or even better, the :checkbox pseudo selector:

change_id['privatecheckbox'] = $(this).find(":checkbox").val();

On a final note: Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
George
  • 36,413
  • 9
  • 66
  • 103
  • Thank you so much!!! This looks very plausible. However, ugh i meanwhile tried to do some edits of my own and now i get a different problem, it always inserting a default value (regardless of checkbox checked or not) - which it didn't before. I also can't replicate how i did that, so i have to hit ctrl+z for a while until i can tell if it works :/ but thank you so much anyway!! – sardine Nov 09 '14 at 15:30
  • If you can reproduce your issue, simply ask another question :) glad to have helped. – George Nov 09 '14 at 15:33
  • edit: i restored it to the point of the above code and it still doesn't work. it inserts everything apart from the checkbox value, which just stays empty, sigh. – sardine Nov 09 '14 at 15:35
  • Have you checked that `change_id['privatecheckbox']` has value prior to your request? Also, you can take advantage of jQuery's `.serialize()` here. – George Nov 09 '14 at 15:51
  • do you mean a default value set in the db? i'm not quite sure i understand! i defined the value in the html checkbox part as follows: and i want to transmit value='private' only if checked. but it just doesn't transmit anything :/ thank you so much again for ongoing help!!! <3 – sardine Nov 09 '14 at 16:13
  • No I mean checking the value of `$(this).find("checkbox").val()` before the request is made (before `$.ajax()`). – George Nov 10 '14 at 09:03
  • yes, the default value is 'private'! I finally got it to write in the db - but, lol, it then -always- wrote in the db, regardless of the checkbox being checked or not. I now saw via http://stackoverflow.com/questions/5621324/jquery-val-and-checkboxes that this is connected to the val() function of jquery :) - again, thank you so very much for the ongoing help, i think i can manage now! :D – sardine Nov 10 '14 at 09:16
  • false alarm, didn't solve it at all, geez i'm in despair, the value is always submitted as 'on' - regardless of what i do, it always submits the on-value into the db. i don't understand because everything i find for help clearly states, unchecked checkboxes ARE NOT 'on'. i'm going to have to open up a new thread X( – sardine Nov 10 '14 at 12:13