Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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')"; 
share|improve this question
    
no, none! everything works, only the checkbox value just isn't submitted into the column.. –  tschembalo Nov 9 '14 at 15:15

1 Answer 1

up vote 0 down vote accepted

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?

share|improve this answer
    
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!! –  tschembalo Nov 9 '14 at 15:30
    
If you can reproduce your issue, simply ask another question :) glad to have helped. –  George Nov 9 '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. –  tschembalo Nov 9 '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 9 '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: <input type='checkbox' name='privatcheckbox' value='private'> 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 –  tschembalo Nov 9 '14 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.