如何在codeigniter中将数据从视图发送到控制器

时间:2022-02-28 12:51:40

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

我是代码点火器的新手。在我的视图中,我添加了此代码,以便在我的网站中启用Google登录。

<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.

如何将变量user_name和id发送到Controller,以便我可以调用Model将值插入数据库。

3 个解决方案

#1


2  

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

您可以使用ajax在获得信息后发布信息(使用下面的jquery):

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

#2


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

#3


2  

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

如果您的方法发布,则使用表单的正常Subbmission或使用Ajax

$this->input->post();

this method contain all data else by default get method

此方法包含默认get方法的所有其他数据

$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
  } 

#1


2  

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

您可以使用ajax在获得信息后发布信息(使用下面的jquery):

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

#2


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

#3


2  

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

如果您的方法发布,则使用表单的正常Subbmission或使用Ajax

$this->input->post();

this method contain all data else by default get method

此方法包含默认get方法的所有其他数据

$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
  }