使用AJAX将行添加到数据库

I have script that add test/row to the DB with AJAX

I added checkbox (#main) and I want it to be sent to the db too.

For some reseaon, checked or not, the checkbox always send me the same value to the DB Why is that?

//##### send add record Ajax request to response.php #########
$("#FormSubmit").click(function (e) {
    //  e.preventDefault();
        if($("#contentText").val()==='')
        {
            alert("Please enter some text!");
            return false;
        }
        alert($("#main").val());
        var myData = 'content_txt='+ $("#contentText").val() + "&main= " + $("#main").val(); //build a post data structure
        jQuery.ajax({
        type: "POST", // HTTP method POST or GET
        url: "response.php", //Where to make Ajax calls
        dataType:"text", // Data type, HTML, json etc.
        data:myData, //Form variables
        success:function(response){
            $("#responds").append(response);
            $("#contentText").val(''); //empty text field on successful
        },
        error:function (xhr, ajaxOptions, thrownError){
            alert(thrownError);
        }
        });
});

<div class="form_style">
    <textarea name="content_txt" id="contentText" cols="45" rows="5"></textarea>
    <input type="checkbox" name="main" id="main" />
    <button id="FormSubmit">Add record</button>
</div>

Use for checkbox element .is(':checked')

Example for your code:

$('#main').is(':checked');

Well it's value is always the same, isn't it? You apparently don't understand how input[type=checkbox] works.

Input type checkbox has a value set and that value is always the same (unless you change it of course, by javascript for example). Difference between the input[type=text] and input[type=checkbox] is that checkbox will not send it's value (which, again, is always the same) when the parent form is submitted.

What you are doing, again, is sending the value of the checkbox, which, again, is always the same.

Right course of action is something like this:

var v = $("#main:checked").val();

Which will return the value only if the checkbox is checked. I suppose this is what you need.

$("#main").val() would give you the value of the checkbox regardless of if it is checked or not, try .serialize() instead.

var myData = $("#contentText,#main").serialize(); //build a post data structure

Some info on getting values from checkbox with jquery:

var myData = 'content_txt='+ $("#contentText").val() + "&main= " + $("#main").is(":checked"); //build a post data structure