0

this is my javascript part:

   <script>
    $(document).ready(function(){
      $(".annotator-save").click(function () {
      var value=$('#jid').text();
         $.ajax({ url: '/response/insert',
           data: {val: value},   i want this to directly go to the function 
                                  insert.
                                  i.e the text has to be displayed.
           type: 'post',
            success: function(output) {
                      alert(output);
                  }
   });
  </script>

what is wrong with my ajax code?

and my php function is-
<?php
  function insert()
  {
      echo $_POST_['val'];
      return $_POST_['val'];
   }
 ?>

 it is entering the php file but not the insert function
Manish Jesani
  • 1,339
  • 5
  • 20
  • 36

3 Answers3

0

You are declaring the function but not calling it. i.e.

<?php
  function insert()
  {
      echo $_POST_['val'];
      return $_POST_['val'];
   }
insert();
?>
Gavin Simpson
  • 2,766
  • 3
  • 30
  • 39
  • 1
    yes that is correct if you wanna run only that function in the file. If you have more than one function in the file you wanna execute, you cannot use this method. – bayblade567 Jan 17 '15 at 06:18
  • Too true, now that the question has been cleaned up... refer to http://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function – Gavin Simpson Jan 18 '15 at 06:19
0

If in a case that you might have more than 1 function in that particular file, you might want to do something like this.

$.ajax({ url: '/response/insert', 
         type: 'POST',
        data: {val: value, action : 'INSERT' },
        success: function(){}
       });


 // in your php
  if($_POST['action']=='INSERT'){
       insert($_POST['val']);
  }
  elseif........//all your actions. Try to state all your actions, return error on else

  function insert($val){}
  function delete(){}
user2478240
  • 369
  • 3
  • 15
0

$.ajax({ url: '/response/insert',

Pls try to use full URL like as:http://localhost/masujan.com/index.php/admin_home/view_portfolio I think it work. oh! no need to return this > return $POST['val'];