user3810730
user3810730

Reputation:

Ajax value can't reach delete function

I have this Ajax code where I am getting an ID from a input field and I am trying to send it via a delete request.

$("#removebutton").on("click", function() {
      var questionid = $('#questionid').val();
      $.ajax({
              url : '../admincontroller/getdatabasedata', 
              type: 'DELETE',
              data: questionid,
              success: function(response)
              {
                JSON.stringify(response);
                alert(response.ok);
              }
          });
    return false; 
  });

Here is where the request is sent:

else if ($this->input->server('REQUEST_METHOD') == 'DELETE')
    {
        $id = $this->input->get('questionid');
        if($id != ''){
            $this->adminmodel->deleteQuestion($id);
            echo json_encode(array('ok' => $id));
        }
        else{
            echo json_encode(array('ok' => $id));
        }   

    }

I am trying to echo $id to see if the value reaches the function, but it only echo UNDEFINED. Why can't my function receive the value?

Upvotes: 1

Views: 53

Answers (3)

miralong
miralong

Reputation: 842

try

data: {questionid: questionid},

in ajax request

Upvotes: 0

P. Gearman
P. Gearman

Reputation: 1166

I'm not sure that having "DELETE" in the type parameter of the ajax call is going to do what you want. Unless I miss my guess, it's expecting "GET", "POST", or "PUT". (The parameter 'type' is an alias for 'method'.)

However, you could try this in the ajax call.

$.ajax({
    url: '../admincontroller/getdatabasedata',
    type: 'POST',
    data: {
        request: 'DELETE',
        questionId: questionId
    },
    success: function(response) {
        ....
    }
}

Upvotes: 0

CollinD
CollinD

Reputation: 7573

You're not sending your data as a key/value set. You're just passing a scalar which is probably interepereted as a string. The PHP is completely unaware of the name questionid. To fix this, use an object as your request data.

In your JS, try this

var reqdata = {questionid: $('#questionid').val() }

And then in your $.ajax call:

data: reqdata,

Upvotes: 1

Related Questions