0

I am having problem passing a javascript variable to php code.

The code that I am using for php is :

$image= $this->user_gallerymodel->viewAlbumImage();

I just need to pass javascript variable as a parameter in model function.

shift66
  • 11,760
  • 13
  • 50
  • 83

2 Answers2

1

using location.href you can call the controller .. this reloads the page

var jvVar="test";
window.location="controller/path/" +  jvVar;

wihtout reloading you can use ajax to send the var..

 var jvVar="test";
 $.post("controller/path/test",{value:jvVar},function(data){
           alert(data);
 });

and in you contorller say test().. get it by post..

 <?php
 function test(){
    $value=$this->input->post('value');
    echo $value;
 }
bipen
  • 36,319
  • 9
  • 49
  • 62
0

You could use an Ajax call where values contains the value you wish to send back.

$.ajax({
  url: "test.php",
  type: "post",
  data: values,
  success: function(){
      alert("success");
       $("#result").html('Value Saved');
  },
  error:function(){
      alert("failure");
      $("#result").html('there was an error while saving value');
  }   
}); 
karmafunk
  • 1,453
  • 12
  • 20