2

I have textarea and three checkboxs in my mysql db by default textarea value as null and checkboxs values as zero(0).

when i enter some text in textarea i'm updating text value in my mysql db but i gotta stuck in checkbox things can some one suggest how to update checkbox checked value in mysql db

say for example if checkbox is checked/clicked i should be able to update my mysql db with value '1' if not the value will be '0'

https://jsfiddle.net/07wmpjqf/1/

db structure

ID  TEXT  ABC  XYZ  LMN
1   NULL   0    0    0

Thanks!

html

<div>
      <textarea class="lb_text" rows="6" cols="50" placeholder="Add text here..."></textarea>
</div>
<div>
    <label>
        <input type='checkbox'>ABC
    </label>
</div>
<div>
    <label>
        <input type='checkbox'>XYZ
    </label>
</div>
<div>
    <label>
        <input type='checkbox'>LMN
    </label>
</div>
    <div>
            <input type="submit" class="lb_save" value="submit">
        </div>

php

if(isset($_POST['lb_text']))
        {   
            $live_blog = mysqli_real_escape_string($_POST['lb_text']);
            $sql = "update demo set text='".$live_blog."'";
            $result = mysqli_query($con, $sql);

        }
    }   

jquery

$(function(){
    $(".lb_save").click(function(){
        var lb_text = $('.lb_text').val();
        if(lb_text == '')
        {
            alert("Enter Some Text...");
            $('.lb_text').focus();
        }
        else
        {
            $.ajax({
                type: "POST",
                url: "index.php",
                data:{
                    lb_text:lb_text,

                },
                success:function(response){
                    alert('successfully updated');
                }
            });
        }
        return false;
    });
});
7
  • in console what values are getting posted. Commented Oct 10, 2015 at 7:22
  • @NiranjanNRaju in console currently im not posting any value if i click checkbox value one should be updated in my mysql db any help is appreciated Thanks! Commented Oct 10, 2015 at 7:24
  • @NiranjanNRaju I'm updating my textarea entered text value but im unable to update my checked value as 1 in mysql db Commented Oct 10, 2015 at 7:25
  • its because you are not posting any value for checkbox in data of $.ajax Commented Oct 10, 2015 at 7:26
  • Yes im not posting currently im surfing like how to send checked value to data in ajax and in mysql update query will u help me out in how to pass checkbox checked value to ajax and in php update query Thanks! Commented Oct 10, 2015 at 7:27

1 Answer 1

1

add id to your check box and capture th value in jquery.

<div>
    <label>
        <input type='checkbox' id="abc">ABC
    </label>
</div>
<div>
    <label>
        <input type='checkbox' id="xyz">XYZ
    </label>
</div>
<div>
    <label>
        <input type='checkbox' id="lmn">LMN
    </label>
</div>

and change your ajax data like this,

data: {
    lb_text: lb_text,
    abc: $("#abc").is(':checked') ? 1 : 0,
    xyz: $("#xyz").is(':checked') ? 1 : 0,
    lmn: $("#lmn").is(':checked') ? 1 : 0,
},

and your query like this,

$live_blog = mysqli_real_escape_string($_POST['lb_text']);
$abc = $_POST['abc']
$xyz = $_POST['xyz']
$lmn = $_POST['lmn']
$sql = "update demo set text='".$live_blog."',ABC='".$abc."',XYZ='".$xyz."',LMN='".$lmn."'";
Sign up to request clarification or add additional context in comments.

3 Comments

one help, in php file before updating , print_r($_POST) and check q=what you r getting and post here.
Thank You So Much you solved my issue and made my day Thankful for ur valuable time and code Thanks!!!
After using print_r($_POST) i'm getting Array ( [lb_text] => sachin [abc] => 1 [xyz] => 1 [lmn] => 0 )

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.