最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send data from view to controller in codeigniter - Stack Overflow

programmeradmin2浏览0评论

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">
<script src=".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.

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.">
<script src="https://apis.google./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.

Share Improve this question asked Mar 10, 2016 at 12:53 Ismail RubadIsmail Rubad 1,4782 gold badges13 silver badges27 bronze badges 1
  • Possible duplicate of Code igniter get ajax value from view to controller – JayKandari Commented Mar 10, 2016 at 13:20
Add a ment  | 

3 Answers 3

Reset to default 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>

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

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
  } 
发布评论

评论列表(0)

  1. 暂无评论