Jquery / ajax / php loads data into a text field after clicking a checkbox

How to load data into text fields after clicking the checkbox, and after that I need to disable this text field, but again, when I remove it, I need to delete this data and make the text field turned on for manual data entry. I tried this code, I am new to jquery / ajax, I can not 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
source share
3 answers

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 
                        $("#txtaddress").val(data); //set the value
                        $("#txtaddress").attr("disabled", "disabled"); 
                    }
                });
            }
        });
});
</script>

From your phpscript, you can echo $row['address'], so the data in the success function can be placed in a text field inajax

+1
source

Javascript code:

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

, , . , ...:)

    $('#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");
           }
        });

php echo $row['address']. value success ajax function

0

All Articles