0

i have javascript like this

$(document).ready(function(){
    $("input[type='checkbox']").change(function(){
        var menu_id = this.value;
        if(this.checked) var statusvalue = "On";
        else var statusvalue = "Off";
        $.ajax({
            type    : "POST",
            url     : "admin/menu/editstatus",
            data    : "id=" + menu_id + "&value=" + statusvalue,
            success : function(data){
                $("#testing").html(data);
            }
        });
    });
});

and my controller named menu and have function editstatus()

public function editstatus()
{
    $data = "asd";
    echo $data;
}

but textbox with id testing not writen anything

3
  • code seems to be fine, can you check response in net call of browser?? Commented May 6, 2015 at 9:55
  • So where's the actual problem in controller or in ajax Commented May 6, 2015 at 9:55
  • check url weather is reach to right page or not? Commented May 6, 2015 at 9:56

4 Answers 4

1

change ajax function like below

$(document).ready(function(){
    $("input[type='checkbox']").change(function(){
        var menu_id = this.value;
        if(this.checked)
         {
          var statusvalue = "On";
         } 
        else 
        {
          var statusvalue = "Off";
        }
        $.ajax({
            type    : "POST",
            url     : "<?php echo base_url()?>admin/menu/editstatus",
            data    :{ 
                     id:menu_id,
                     value:statusvalue
                      }
            success : function(data){
               $("#testing").val(data);
            }
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

i use your code, the textbox fills with this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="w3.org/1999/xhtml"><head> <title>Admin Panel</title> <link rel="shortcut icon" href="localhost/big/img/Logo.png"> <link rel="stylesheet" href="localhost/big/css/admin-leftmenu.css"> <script src="localhost/big/js/jquery-2.1.1.js"></script> <script src="htt.............. etc
0

I think your url did not found.Try using base url

<?php echo base_url()?>admin/menu/editstatus

1 Comment

you need base_url() for getting the full url
0

You need to use .val() instead of .html()

Corrected code:

$("#testing").val(data);

http://api.jquery.com/val/

https://api.jquery.com/html/

1 Comment

i change to .val, and all of my code show in textbox
0
public function editstatus()
{
    $data = "asd";
    echo json_encode($data);
}

if $data is an array form e.g $data=array('name'=>'abd','id'=>'12') then , in Java script access like ,data.name or data.id

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.