Nassereddine Sadoun Nassereddine Sadoun - 3 years ago 163
Ajax Question

sending data to controller in AJAX using FormData Object

i'm working with CodeIgniter, and i'm trying to send some datas to a controller with AJAX by using a FormData object.

The problem is that when i try to get the datas in the controller by using $this->input->get_post("AttributeName"), the content of it is NULL.
here's the code:

The View:

<input type="text" name="firstName" id="new-member-firstName" class="new-event-text-input" autocomplete="off"/>
<input type="text" name="lastName" id="new-member-lastName" class="new-event-text-input" autocomplete="off"/>
<input type="text" name="title" id="new-member-title" class="new-event-text-input" autocomplete="off"/>
<input type="text" name="mail" id="new-member-mail" class="new-event-text- input" autocomplete="off"/>

The JQuery Script:

var formData = new FormData();
formData.append("affiliation", $("#selectedAffiliate").val());
formData.append("firstName", $('#new-member-firstName').val());
formData.append("lastName", $('#new-member-lastName').val());
formData.append("title", $('#new-member-title').val());
formData.append("mail", $('#new-member-mail').val());
url:$("#URL").attr("myURL") + NewEvent/DataWriters/DataWriterForComities/addMember',
   data:{ formData },
   cache: false,
   contentType: false,
   processData: false,
   datatype: 'html',
    alert("The Member has been registred");

The Controller:

public function addMember()
$member = new Member();
$member->setLastName( $this->input->get_post("lastName") );
$member->setFirstName( $this->input->get_post("firstName") );
$member->setMail( $this->input->get_post("mail") );
$member->setTitle( $this->input->get_post("title") );  
$memberId = $member->save();
echo $memberId;

what's the correct way to get these datas, because i checked everything else and it's fine.

Answer Source

you should use formData directly, no need to warp it in an object, as follow:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download