I have this code in Ajax/Jquery, where I am trying to request a PUT method to my API. In my code I am trying to send the data that has been updated in my text fields. This data should be received by the API and send it to the Model it should be used to update a records in my database based on the ID recived. For some reason, my REST API in the controller is not receiving the values from my text fields.
Code where I am sending the data:
$("#edit").on("click", function() {
var updatedData ={ question: $('#questionField').val(),
image: $('#imageField').val(),
answer1: $('#answer1Field').val(),
answer2: $('#answer2Field').val(),
answer3: $('#answer3Field').val(),
answer4: $('#answer4Field').val(),
};
$.ajax({
url : '../admincontroller/getdatabasedata/',
type: 'PUT',
dataType: 'json',
data: updatedData,
success: function(updatedQuestion)
{
JSON.stringify(updatedQuestion);
if(updatedQuestion.ok == 1) {
alert("Succesfully edited");
}
else{
alert(updatedQuestion.ok);
}
}
});
return false;
});
Code for my REST API where data should be received:
else if ($this->input->server('REQUEST_METHOD') == 'PUT' )
{
$id = $this->input->post('idField');
$question = $this->input->post('questionField');
$image = $this->input->post('imageField');
$answer1 = $this->input->post('answer1Field');
$answer2 = $this->input->post('answer2Field');
$answer3 = $this->input->post('answer3Field');
$answer4 = $this->input->post('answer4Field');
if($question != '' && $image != '' && $answer1 != '' && $answer2 != '' && $answer3 != '' && $answer4 != ''){
$this->adminmodel->updateQuestion($id,$question,$image,$answer1,$answer2,$answer3,$answer4);
echo json_encode(array('ok' => 1));
}
else{
echo json_encode(array('ok' => $question));
}
}
This what I am getting in the chrome network tab:
At the moment I am trying to alert to see if the data is passed, but I just get NULL. Any idea why? Thanks.