Trying to send data to database with AJAX (CI)
<p>i have a problem with my AJAX, not posting any data to my database, i have googled the internet dry, and now i ended up here.</p>

<p>I tried in Postman, inserting my Json data (seen below) and i did a POST to my method in my API controller(user/add_user), my controller then runs the query from my model, and it is inserted into the database correctly.</p>

<pre><code>{
"first_name": "Admin",
"last_name": "Panna",
"email": "Admin2@mail.com",
"password": "Admin1234",
"phone_number": "12345678",
"address": "Nowhere 1",
"city": "somwhere",
"country": "Germany",
"nationality": "Finnish",
"speak_danish": "1",
"colleague": "Dora The Explorer",
"task": "Security"
}
</code></pre>

<p>So the problem is really that my frontend, with my jquery ajax calls is not doing a POST, or atleast not right, because i cant see any data being send anywhere, and in the console tool in the browser, in the network tab, no POST method is being listed :(</p>

<p>Here is my function that loads the HTML in my API (just so you can see the HTML code)</p>

<pre><code>function registrationPage() {
var html =
    '&lt;div class="register-text container"&gt;'
        +'&lt;h3&gt;You are about to register as a volunteer for&lt;/h3&gt;'
        +'&lt;img src="' + RESS +'img/tinderbox_date.svg"&gt;'
    +'&lt;/div&gt;'
    +'&lt;div class="register-input container"&gt;'
        +'&lt;div&gt;'
            +'&lt;input id="email" type="email" name="email" placeholder="Email"&gt;'
            +'&lt;input id="password" type="password" name="password" placeholder="Password"&gt;'
            +'&lt;input id="first_name" type="text" name="first_name" placeholder="First Name"&gt;'
            +'&lt;input id="last_name" type="text" name="last_name" placeholder="Last Name"&gt;'
            +'&lt;select id="gender" name="gender" placeholder="Gender"&gt;'
                +'&lt;option value="" disabled selected&gt;Gender&lt;/option&gt;'
                +'&lt;option value="1"&gt;Female&lt;/option&gt;'
                +'&lt;option value="0"&gt;Male&lt;/option&gt;'
            +'&lt;/select&gt;'
            +'&lt;label for="dateofbirth"&gt;Date of Birth&lt;/label&gt;'
            +'&lt;input id="dateofbirth" placeholder="Date of Birth" type="date" name="dateofbirth" &gt;'
            +'&lt;select id="nationality" name="nationality" &gt;'
                +'&lt;option value="nationality" disabled selected&gt;Nationality&lt;/option&gt;'
                +'&lt;option value="Danish"&gt;Danish&lt;/option&gt;'
                +'&lt;option value="German"&gt;German&lt;/option&gt;'
                +'&lt;option value="norwegian"&gt;Norwegian&lt;/option&gt;'
            +'&lt;/select&gt;'
            +'&lt;div class="upload-image"&gt;'
                +'&lt;img src="images/picture.svg"&gt;'
                +'&lt;p&gt;Upload image&lt;/p&gt;'
            +'&lt;/div&gt;'
            +'&lt;input id="phonenumber" type="number" name="phonenumber" placeholder="Phonenumber"&gt;'
            +'&lt;input id="address" type="text" name="address" placeholder="Address"&gt;'
            +'&lt;select id="country" name="country" placeholder="Country"&gt;'
                +'&lt;option value="Denmark"&gt;Country&lt;/option&gt;'
                +'&lt;option value="Denmark"&gt;Denmark&lt;/option&gt;'
                +'&lt;option value="germany"&gt;Germany&lt;/option&gt;'
                +'&lt;option value="Norway"&gt;Norway&lt;/option&gt;'
            +'&lt;/select&gt;'
            +'&lt;input id="zipcode" type="number" name="zipcode" placeholder="Zip code"&gt;'
            +'&lt;input input="city" type="text" name="city" placeholder="City"&gt;'
            +'&lt;select id="speak_danish" name="speak_danish"&gt;'
                +'&lt;option value="danish" disabled selected&gt;Speak and understand Danish&lt;/option&gt;'
                +'&lt;option value="1"&gt;Yes&lt;/option&gt;'
                +'&lt;option value="0"&gt;No&lt;/option&gt;'
            +'&lt;/select&gt;'
            +'&lt;select id="task" name="task"&gt;'
                +'&lt;option value="tasks" label disabled selected&gt;Preferred work tasks&lt;/option&gt;'
                +'&lt;option value="fences"&gt;Building Fences&lt;/option&gt;'
                +'&lt;option value="bartender"&gt;Bartender&lt;/option&gt;'
                +'&lt;option value="it-work"&gt;IT Work&lt;/option&gt;'
            +'&lt;/select&gt;'
            +'&lt;input id="colleague" type="text" name="colleague" placeholder="I like to work with (name)"&gt;'
            +'&lt;button class="link-register-user" type="submit" value="REGISTER"&gt;Register&lt;/button&gt;'
        +'&lt;/div&gt;'
    +'&lt;/div&gt;';

jQuery('#main').html(html);
}
</code></pre>

<p>and the AJAX i have for it is here </p>

<pre><code>function register(e) {
e.preventDefault();

var emailVal = jQuery('#email').val();
var passwordVal = jQuery('#password').val();
var first_nameVal = jQuery('#first_name').val();
var last_nameVal = jQuery('#last_name').val();
var genderVal = jQuery('#gender').val();
var dateofbirthVal = jQuery('#dateofbirth').val();
var phone_numberVal = jQuery('#phone_number').val();
var addressVal = jQuery('#address').val();
var cityVal = jQuery('#zipcode').val();
var zipcodeVal = jQuery('#city').val();
var countryVal = jQuery('#country').val();
var nationalityVal = jQuery('#nationality').val();
var speak_danishVal = jQuery('#speak_danish').val();
var colleagueVal = jQuery('#colleague').val();
var taskVal = jQuery('#task').val();
var sendData = {
        "email": emailVal,
        "password": passwordVal,
        "first_name": first_nameVal,
        "last_name": last_nameVal,
        "gender": genderVal,
        "dateofbirth": dateofbirthVal,
        "phone_number": phone_numberVal,
        "address": addressVal,
        "city": cityVal,
        "zipcode": zipcodeVal,
        "country": countryVal,
        "nationality": nationalityVal,
        "speak_danish": speak_danishVal,
        "colleague": colleagueVal,
        "task": taskVal
};
    jQuery.ajax({
    url: URL + 'user/add_user',
    contentType: 'application/json',
    type: 'POST',
    data: JSON.stringify(sendData),
    success: function(data, status, response) {
        console.log('Success');
    },
    error: function(xhr, status, error) {
        var err = JSON.parse(xhr.responseText);
        console.log(sendData);
    }
}); 
}
</code></pre>

<p>Like i said i know my controller and model is working, since it works in POSTMAN, but none the less let me know if i should share that also, 
for you to be able to help better? :) </p>

<p>Btw plz bear with me, since my this is my very first posting on stack overflow ever, and i also am fairly new to coding in genral.</p>

<p>Thank you in advance awesome community! :)  </p>