Code Igniter POST Variable

杀马特。学长 韩版系。学妹 提交于 2021-01-07 22:01:32

问题


I am brand new to Code Igniter but I can't seem to find a way to do this in the documentation, In normal PHP you can retrieve the contents of the entire POST variable like this

$_POST = $postedVars

But I can't seem to find something that does this in the same manner, I've tried

$arr = $this->input->post;

and

$arr = $this->input->post();

With no luck! Is this possible in CI? Thanks in advance!


回答1:


First, you need a form or something that sets the variables Then, you retrieve them.

$this->input->post('first_name').

You are missing the name of the variable!

The signup form:

echo form_open('CHome/signup');
$data=array('name'=>'first_name', 'id'=>'first_name','size'=>40,'value'=>set_value('first_name'));
echo "<p><label for='first_name'>First Name </label>";
echo form_input($data);
echo form_submit('submit','Make Account');

The Model:

 function addUser(){
    //you should use $this->input->post('first_name')
            $data=array(
                'first_name'=>db_clean(ucfirst($_POST['first_name'])), //db_clean is a custom func
                'last_name'=>db_clean(ucfirst($_POST['last_name'])),        
                'email'=>db_clean($_POST['email']),            
                'username'=>db_clean($_POST['username']),
                'password'=>db_clean(sha1($_POST['password1'])),            
                'type'=>db_clean('user'),
            );

            $this->db->insert('users',$data);
        }

Codeigniter stores sessions in cookies, it's all weird. I suggest just using PHP's native sessions, such as $_SESSION['first_name']. Make sure to write "session_start();" in your controller/model so you can use sessions!! (usually do it in the constructor)




回答2:


well firstly you need a form to populate the form fields with variables and a submit button to post the the variables from the form fields

     <?php
      $attributes = array('class' => 'form-horizontal', 'id' => 
    'myform',    'role'=>'form');
      echo form_open('Iris/login_in', $attributes); ?>
        <div class="row">
            <div class="col-xs-10 col-md-offset-1">
                <div class="row">
                    <div class ="col-xs-12">
                        <div class="form-group ">
                            <label for="email" class="text-warning" 
                             >Email Address</label>
                            <input type="email" class="form-control"
                                   name="email"
                                   placeholder="Your Email Address"
                                  required>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class ="col-xs-12">
                        <div class="form-group " >
                            <label for="password" class="text-
                            warning">Password</label>
                            <input type="password" class="form-
                             control"    name="password"
                                   placeholder="Your password"
                                   required>

                        </div>
                    </div>
                </div> 
                </form>



回答3:


You can set your data array to your post array like so. This is VERY usefull if you have many fields. Just make sure your input field name matches your database field name.

$data = $_POST;

If you have any additional elements in your post array (submit button, hidden fields, etc.) that will not be going into your database, remove them before running your insert.

unset($data['SubmitButton']);
unset($data['HiddenField1']);

$this->db->insert('users',$data);


来源:https://stackoverflow.com/questions/3232347/code-igniter-post-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!