4

How do i load data into textfields after clicking a checkbox & after doing it, i need to disable that textbox, but again as i uncheck it, need to remove that data & make the textbox enable to manually enter data. I tried this code,i'm new to jquery/ajax, can't figure out how to solve this problem.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

 <script>
$(document).ready(function(){
   $('#chk').click(function(){
     if($(this).is(":checked"))
       $("#txtaddress").removeAttr("disabled");
    else
      $("#txtaddress").attr("disabled" , "disabled");
         // here, i don't know how do i assign $row['address'] to txtaddress by
         // using php mysql 
    });
   });
 </script>

      <!-this is my html code-->
      <form>
        <input type="checkbox" id="chk" name="chk">Same as home address?
        <input id="txtaddress" name="txtaddress"  disabled type="text">
       </form>
4
  • For starters, .atte should be .attr. Then on the next line do $('#txtaddress").val('<?php echo $row[address];?>'); Commented Oct 3, 2013 at 11:27
  • i need to disable that checkbox, but again as i uncheck it, - how can you click disabled checkbox to make it uncheck again? its NOT possible Commented Oct 3, 2013 at 11:28
  • I think he means disable the text input when the checkbox is checked, at least, I think. Commented Oct 3, 2013 at 11:29
  • 1
    @JamieTaylor: sorry,it was textbox, i edited. Commented Oct 3, 2013 at 11:39

3 Answers 3

1

Try the following,

<script>
$(document).ready(function(){
        $('#chk').click(function(){
            if($(this).is(":checked"))
                 $("#txtaddress").val(""); //reset the textbox
                $("#txtaddress").removeAttr("disabled");

            else {
                $.ajax({
                    type: 'GET',
                    url: destinationUrl, // your url
                    success: function (data) { // your data ie $row['address'] from your php script
                        $("#txtaddress").val(data); //set the value
                        $("#txtaddress").attr("disabled", "disabled"); 
                    }
                });
            }
        });
});
</script>

From your php script you can echo $row['address'], so the data in success function can be put into textbox in ajax

Sign up to request clarification or add additional context in comments.

Comments

0

javascript code:

$(document).ready(function(){
 $('#chk').click(function(){
var txt =   $("#txtaddress");
 ($(this).is(":checked")) ? txt.attr("disabled" , true) :  txt.attr("disabled" , false);
 });
});

Comments

0

I hope this is what u meant.. Hope this might help... :)

    $('#chk').click(function(){
           if(! $(this).is(":checked")){
               $("#txtaddress").removeAttr("disabled");
                $("#txtaddress").val('');
           }
           else{
               $.ajax( {
                url: "get_row_details",
                type: "POST", //or may be "GET" as you wish
                data: 'username='+name, // Incase u want to send any data
               success: function(data) {
                   $("#txtaddress").val(data); //Here the data will be present in the input field that is obtained from the php file
               }
               });
               $("#txtaddress").attr("disabled" , "disabled");
           }
        });

From the php file echo the desired $row['address'].This value will be obtained at the success of the ajax function

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.