Ismail Rubad
Ismail Rubad

Reputation: 1468

How to send data from view to controller in codeigniter

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.

Upvotes: 2

Views: 3493

Answers (3)

Kisaragi
Kisaragi

Reputation: 2218

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>

Upvotes: 2

Maninderpreet Singh
Maninderpreet Singh

Reputation: 2587

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
  } 

Upvotes: 2

RK12
RK12

Reputation: 472

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 :)

Upvotes: 2

Related Questions