0

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>

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, can you lookout for wallajax.php page because in console value is passed but i think is not retriving in wallajax.php
0

You have two issues, your dataString does not include a "&" between variables and the checkbox value is not being captured correctly, below fixes both issues:

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

$("#px").click(function() {
    var status2 = $("#status2").val();
     var checkit= $("#checkit").prop('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>

There is also an issue with your php, you are explicitly "setting" the variables when you prepare the dataString in this way, so

isset($_POST['check']) && isset($_POST['content']) 

will always return true, a better way to get the data so that doesnt happen is to use jquery's .serialize() method on the form, although i dont see the form element in your html i assume there is one

var dataString = $('yourForm').serialize();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.