4

Here is my code.

 <form id='user'>
   <label for="name">Name :</label>
   <input type="text" id="name" name="name"/>
   <label for="email">Email :</label>
   <input type="text" id="email" name="email"/>
   <input type="submit"/>
 </form>
 <div id="output"></div>

jQuery:

$('#user').on('submit',function(event){
        event.preventDefault();
        var data=$(this).serialize();
        adddetails(data);
});
function adddetails(data){
        var url='http://localhost/projectname/index.php/users/adddat';
        $("#output").load(url,data,function(response,status){
        });
}

In the 'users' controller

function adddat(){
    $name=$this->input->post('name');
    $email=$this->input->post('email');
    echo "name: ".$name." and email: ".$email;
}

Here when I click on submit the output div is shown like this

name: and email:

Here I am not getting the post data of name and email. Could anyone suggest me an idea.

user3066006
  • 149
  • 1
  • 15

2 Answers2

2

Using JQuery's load function (JQuery API)

POST method is used if data is provided as an object; otherwise, GET is assumed.

Per JQuery's definition of the serialize function

The .serialize() method creates a text string in standard URL-encoded notation.

That said, you're attempting to get a POST variable when GET is assumed, as it's not an object being passed to the load function, it's a string.

Try using $this->input->get('parameter_name')

As a sidenote, you may be able to get the data points as function parameters: function addat($name, $email){ as codeigniter usually will accept that method of getting GET params.

Tony Shepherd
  • 115
  • 1
  • 8
1

serialize method create a string output. to Make a POST call you need to use JSON object.

$('#user').on('submit',function(event){
        event.preventDefault();
        var data=formToJSON('#user');
        adddetails(data);
});
function adddetails(data){
        var url='http://localhost/projectname/index.php/users/adddat';
        $("#output").load(url,data,function(response,status){
        });
}


function formToJSON( selector )
{
     var form = {};
     $(selector).find(':input[name]:enabled').each( function() {
         var self = $(this);
         var name = self.attr('name');
         if (form[name]) {
            form[name] = form[name] + ',' + self.val();
         }
         else {
            form[name] = self.val();
         }
     });

     return form;
}

Credit Convert form data to JavaScript object with jQuery

Community
  • 1
  • 1
jagad89
  • 2,603
  • 1
  • 24
  • 30