0

I'm trying to send data to the server API from a webpage but it keeps falling to 'No Connection!' as you will see in the code. Note:The server, database and the API are working, because I also use it on a phone application that do the same as I'm trying to do here which is post an event to the database. Here is the webpage code:

        function onAddEvent(){
        var title = document.getElementById("title").value;
        var desc = document.getElementById("desc").value;
        var date = document.getElementById("date").value;
        var userid = localStorage.getItem("userid");
        $.ajax({
            url: API_URL,
            type: 'POST',
            data: {eventname: title, eventdate: date, eventdesc: desc, user_id: userid},
            async: true, // set the property here
            success: function(data) {
                if(data.result == "success"){
                    alert("Add Event Successfully!");
                }
                else{
                    alert("Can't add event");
                }
            },
            error: function(xhr, error) {
                //It is falling here
                alert('No Connection!');
            }
        });
    }

And here is the PHP API that it will connect to:

    function addevent()
{
    $new_member_insert_data = array(
        'eventname' => $this->input->post('eventname'),
        'eventdate' => $this->input->post('eventdate'),
        'eventdesc' => $this->input->post('eventdesc'),
        'user_id' => $this->input->post('user_id')
    );
    $insert = $this->db->insert('event', $new_member_insert_data);
    return $insert;
}
Tarek Alabbar
  • 63
  • 1
  • 8

1 Answers1

0

Remove the code from the function or try calling the function in the API.

//Call the function from your API
addevent();

function addevent()
{
   $new_member_insert_data = array(
     'eventname' => $this->input->post('eventname'),
     'eventdate' => $this->input->post('eventdate'),
     'eventdesc' => $this->input->post('eventdesc'),
     'user_id' => $this->input->post('user_id')
   );
   $insert = $this->db->insert('event', $new_member_insert_data);
   return $insert;
}
redfades
  • 374
  • 3
  • 5