Ayan Aman Ayan Aman - 3 months ago 8
Ajax Question

Pass the value of checkbox via ajax, php, jquery

Good day. guys i got a problem.
I need to send my checkbox value via ajax to a php file

My question
i want to pass the value in both cases whenever it is check or not.
if it is checked then it should have pass the

value="true"

and if it is not check then simply the value in content should be passed. via ajax jquery

Note

i know code in jquery (AJAX) file should be updated with
if condition
but i don't know how to do it

Update -----

I have fix the issue of check conditon by following answers

but

in wallajax.php file when i use
if(isset($_POST["content"]))

then i am getting te value of content via ajax and it is also returing its value in page

but when i am using
if(isset($_POST["content"]) && isset($_POST["check"]) )

no value is retrieved as a result no return value showing on page

i cant understand, where is the problem happeing, is it on ajax script or wallajax.php file

my code.

<script type="text/javascript">
$(function() {

$("#px").click(function() {
var status2 = $("#status2").val();
var checkit= $("#checkit").is(':checked');
var dataString = 'content=' + status2 + '&check=' + checkit;
$.ajax({
type: "POST",
url: "wallajax.php",
data: dataString,
cache: false,
success: function(resol) {
$('#wallposts').prepend(resol);
console.log(resol)
}
}); return false;
});

});
</script>


wallajax.php

if(isset($_POST["content"]) && isset($_POST["check"]) )
{
---fetched value------feed value in databse------
}


index.php

<textarea name="content" id="status2"></textarea>
<input name="check" id="checkit" type="checkbox" value="true">check it</input>
<div id="px">Post</div>

Answer

Instead of

$("#checkit").val();

use

$("#checkit").prop('checked');

OR

$("#checkit").is(':checked');

I would suggest

var data = {'content': status2, 'check': checkit};

instead of

var dataString = 'content=' + status2 + '&check=' + checkit;