0

i m trying to submit data using this code in codeigniter but can't submit data and create error.

error:POST http://localhost/managment/index.php/welcome/add 500 (Internal Server Error)

signupform is id of my sign up form which contain 6 fields and have a submit button

ajax i saved this file with .js extension

$(document).on("submit", "#signupform", function (e) {
    e.preventDefault();
    //var view_id=$("#id_hid").val();
    //alert(view_id);
    $.ajax({
        type: "post",

        data: $('#signupform').serialize(),
        dataType: "JSON",
        url: "../welcome/add",
        success: function (data)
        {
            //var json=$.parseJSON(data);
            alert(data);
        }
    });
});

controller

public function add() {
    //$data=array();
    $postData = array();
    //prepare post data
    $postData = array(
        'username' => $this->input->post('username'),
        'password' => $this->input->post('password'),
        'email' => $this->input->post('email'),
        'mobileno' => $this->input->post('mobileno'),
        'address' => $this->input->post('address')
    );

    // //insert post data

    $insert = $this->home_model->insert_form($postData);
    $data['msg'] = "data insert successfully";
    echo json_encode($data['msg']);
}

model

function insert_form($data)
{   
    $insert=$this->db->insert('emp',$data);
    if($insert)
    {
        return $this->db->insert_id();
    }
    else
    {
        return false;
    }
    //echo  json_encode($data);
}
meet
  • 1
  • 3

2 Answers2

2

Your Javascript Ajax function

      var frmData = $('#signupform').serialize();
      $.ajax({
        type:"post",

        data:{frmData:frmData},
        dataType: "JSON",
        url:"<?php echo base_url('index.php/your_controller_name/your_function_name')?>",       
        success:function(data)
         {

          }
        });

Your controller function

   public function add()
   {
        $postData=array();
        parse_str($this->input->post('frmData'),$postData);

        //prepare post data
        $postData = array(
            'username' => $postData['username'],
            'password' => $postData['password'],
            'email' => $postData['email'],
            'mobileno' => $postData['mobileno'],
            'address' => $postData['address']
        );       
   }

When you use serialize in java script then you must retrieve data using parse_str function of php then you will get exact post data from encoded string .

Amit Chauhan
  • 682
  • 7
  • 22
1

As you mentioned in error error: jquery.min.js:4 POST http://localhost/managment//index.php/welcome/add you can see worng direction of URL

Whats wrong

http://localhost/managment//index.php/welcome/add  
                          ^^

WHat should I can do for this

Change url:"../welcome/add", to url:"http://localhost/managment/index.php/welcome/add", `

or

var base = <?php echo base_url() ?>
url: base+"index.php/welcome/add"

Make sure

In application/config.php base Url should be http://localhost/managment/

Read How do I PHP-unserialize a jQuery-serialized form?

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85