0

I am trying to send data to one of my function within a controller using ajax but the error at the bottom (in the image) wont let the variables pass.

I'm using the codeigniter framework and this ajax function is in my view.

$(document).ready(function(){

function myButton(id1, id2, id3){
    $.ajax({
      method: "POST",
      url: "<?php echo base_url() ?>controller/function/"+id1+"/"+di2+"/"+id3,
      data: {
        id1:id1,
        id2:id2,
        id3:id3
      }
      dataType : 'text',
      success:function(data){
        location.reload();
    });
}
}

<div align="center">
  <a>
    <button onclick="myButton('<?php echo $id1; ?>', '<?php echo $id2; ?>', '<?php echo $id3; ?>')">
      <img  src="<?php echo base_url();?>images/button.png" >
    </button>
  </a>
</div>

Uncaught ReferenceError: myButton is not defined(…)

enter image description here

1
  • echo should end with ` ; ` like this <?php echo base_url(); ?> Commented Oct 26, 2016 at 21:16

3 Answers 3

3

Define the function outside of $(document).ready()

Currently, the function isn't being defined until after the dom is loaded, meaning that there is no definition when the button is setup.

This should look like this:

<script>
var myButton = function(id1, id2, id3){
                $.ajax({
                   method: "POST",
                  url: "<?php echo base_url(); ?>controller/function/"+id1+"/"+id2+"/"+id3,
                  data: {
                    id1:id1,
                    id2:id2,
                    id3:id3
                  },
                dataType : 'text',
                  success:function(data){
                    location.reload();
                }
            });
};

    $(document).ready(function(){
//This isn't really needed
});
</script>



<div align="center">
  <a>
    <button onclick="myButton('<?php echo $id1; ?>', '<?php echo $id2; ?>', '<?php echo $id3; ?>')">
      <img  src="<?php echo base_url();?>images/button.png" >
    </button>
  </a>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

I updated the answer with an example that should work. See if your code matches
still doesn't work. Gives me the same error Uncaught ReferenceError: myButton is not defined(…)
Ok, looks like there were still some syntax errors in the js. I fixed the code here and have a pen at codepen.io/planc/pen/JRqobB demonstrating it working properly.
0

You're declaring myButton within the inner document.ready function. You need to declare it in the global scope.

<script>
// Define in global scope
function myButton(id1, id2, id3){
    $.ajax({
      method: "POST",
      url: "<?php echo base_url() ?>controller/function/"+id1+"/"+di2+"/"+id3,
      data: {
        id1:id1,
        id2:id2,
        id3:id3
      }
      dataType : 'text',
      success:function(data){
        location.reload();
    });
}
</script>

<div align="center">
  <a>
    <button onclick="myButton('<?php echo $id1; ?>', '<?php echo $id2; ?>', '<?php echo $id3; ?>')">
      <img  src="<?php echo base_url();?>images/button.png" >
    </button>
  </a>
</div>

1 Comment

there's really no need for the document.ready at all. function definitions don't need to be in them.
0

The answer may be one charcter ; so use <?php echo base_url(); ?> this will print invalid Javascript and may be more !!!

$(document).ready(function(){

function myButton(id1, id2, id3){
    $.ajax({
      method: "POST",
      url: "<?php echo base_url(); ?>controller/function/"+id1+"/"+di2+"/"+id3,
      data: {
        id1:id1,
        id2:id2,
        id3:id3
      }
      dataType : 'text',
      success:function(data){
        location.reload();
    });
}
}

<div align="center">
  <a>
    <button onclick="myButton('<?php echo $id1; ?>', '<?php echo $id2; ?>', '<?php echo $id3; ?>')">
      <img  src="<?php echo base_url();?>images/button.png" >
    </button>
  </a>
</div>

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.