2

I am new to code-igniter. In my View I added this code to enable google login in my website.

<html lang="en">
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="720765566138-5c6jreo4r7ma6cm0hdblj5cmjtdiruk4.apps.googleusercontent.com">
<script src="https://apis.google.com/js/platform.js" async defer>     </script>
</head>
<body>
<div class="g-signin2" data-onsuccess="onSignIn" data-onfailure="onFailure" data-theme="dark"></div>
<script>
function onFailure(msg){ console.log(msg); }
function onSignIn(googleUser) {
    console.log("onSignIn");
    var profile = googleUser.getBasicProfile();
    var user_name = profile.getName();
    var id = googleUser.getAuthResponse().id;
};
</script>
</body>
</html>

How can I send the variable user_name and id to the Controller so that I can call the Model to insert the value to the database.

Ismail Rubad
  • 1,458
  • 2
  • 13
  • 27
  • Possible duplicate of [Code igniter get ajax value from view to controller](http://stackoverflow.com/questions/15584776/code-igniter-get-ajax-value-from-view-to-controller) – JayKandari Mar 10 '16 at 13:20

3 Answers3

2

You have to make controller's action and in this you have to pass variable like this :

function test{
    $user_name = "XYZ";
    $this->load->view('view_name', $user_name);
    }

If you want to pass more than one variable then use below :

function test{
        $user_name = "XYZ";
        $data = 123;
        $this->load->view('view_name', array("user_name"=>$user_name,"data"=>$data));
    }
May this will help you :)
RK12
  • 472
  • 3
  • 11
  • I wanted to send the the JavaScript variable user_name to controller from the view. – Ismail Rubad Mar 10 '16 at 13:15
  • Then pass the javascript value to hidden field and pass that variable in form action url.... And you can then get that variable by $this->input->get(); in controller. – RK12 Mar 10 '16 at 13:21
2

With normal Subbmission of your Form or using Ajax if your method is post

$this->input->post();

this method contain all data else by default get method

$this->input->get();

Make your form Like below

 <form action="baseurl/controller_name/method">
 add hidden field of user_id then sumbmit 
 </form>

In Your Controller

  function YOUR_METHOD()
  {
      print_r($this->input->get())//By default get method
  } 
Maninderpreet Singh
  • 2,569
  • 2
  • 17
  • 31
2

You can simply use ajax to post the information once you've gotten it (using jquery below):

<script>
    function onSignIn(googleUser)
   {
       var profile = googleUser.getBasicProfile();
       var user_name = profile.getName();
       var id = googleUser.getAuthResponse().id;
       $.ajax({
               'url': 'the/path/to/controller',
               'type': 'POST',
               'data':{'user_name': user_name, 'id':id},
               'success': function(){}, //up to you
               'error': function() // up to you
             })
    };
</script>
Kisaragi
  • 2,198
  • 3
  • 16
  • 28