简体   繁体   中英

how to get data from form serialize codeigniter controller

I try to get data from ajax pass from front page to my controller but i got only string to my controller so how can i separate it one by one.

this is form:

<form class="form-horizontal form" action="<?php print base_url() ?>insertCategory" method="post" enctype='multipart/form-data'>
          <div class="box-body">
            <div class="form-group">
              <label for="inputEmail3" class="col-sm-2 control-label">name</label>

              <div class="col-sm-10">
                <input type="text" name='name' required class="form-control" id="inputEmail3" placeholder="Name">
              </div>
            </div>
            <div class="form-group">
              <label for="inputEmail3" class="col-sm-2 control-label">name</label>

              <div class="col-sm-10">
                <input type="text" name='test' required class="form-control" id="inputEmail3" placeholder="Name">
              </div>
            </div>
            <div class="form-group">
              <label for="inputPassword3" class="col-sm-2 control-label">Category Detail</label>

              <div class="col-sm-10">
                <textarea id="txtEditor" ></textarea> 
              </div>
            </div>
            <div class="form-group">
              <label for="inputPassword3"  class="col-sm-2 control-label">Image</label>

              <div class="col-sm-10">
                <input type="file" name="img" required class="form-control" >
              </div>
            </div>
          </div>
          <!-- /.box-body -->
          <div class="box-footer">
            <button type="submit" class="btn btn-info pull-right submit">Submit</button>
          </div>
          <!-- /.box-footer -->
        </form>

this my code ajax:

$(".submit").click(function (e) {
  e.preventDefault();
var data = $('.form').serialize();
var val  = $("#txtEditor").Editor("getText");
var page = "<?php echo $page ?>";
var url  = "<?php echo base_url(); ?>insertCategory"
$.ajax({
            type: "POST",
            url: url,
            data:{ 
                "data"  : data
           },
            success: function (data) {
               console.log(data);
            }
        });
        return false;
});

and this is my controller:

public function insertCategory()
    {   
        if($this->session->userdata('logged_in')==1){
            $data = $this->input->post('data');
            var_dump($data);

and this my data respond by ajax:

C:\....\AdminController.php:308:string 'name=dsfdsfs&test=dfsdf' (length=23)

Use this instead,

var_dump($_POST);

it is posted as same as post work

For reference, use this code below.

 type: "POST",
    url: url,
    data: $('.form').serialize(),
    success: function (data) {
       console.log(data);
    }

Use

 data: $('.form').serialize()

instead of

var data = $('.form').serialize();
data:{ 
    "data"  : data
},

At the controller the contents of your form will be available in the $_POST array. So the "name" field on your form can be accessed with $_POST['name'] .

That said, you will be better off using CodeIgniter's methods to retrieve this data.

For example:

$name = $this->input->post('name');

If, for learning purposes, you wanted to see everything "posted" to the controller this will do the trick.

public function insertCategory()
{   
    if($this->session->userdata('logged_in')==1)
    {
        $posted = $this->input->post(); //get all $_POST items
        var_dump($posted);
    }
}

use parse_str(); that can return array after parse string

public function insertCategory()
{   
    if($this->session->userdata('logged_in')==1){
        $data = $this->input->post('data');
        parse_str($data, $parseData);
        var_dump($parseData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM