-1

I got a database with one table in it.
Table has Company and Time columns, and some more, but these two are important.
So the user makes an appointment through the filling of the form.
In the form I have 2 <select>s - Company And Time, so he chooses both from the selections.
He clicks a button and the form is stored in the database.
How do I use AJAX to retrieve all the hours(Time) that are in use, and then disable them accordingly.
For example: I made the appointment selected Nokia from Companies and 9:30 from Time dropdowns. Now You want to make the appointment with Nokia but the 9:30 time is disabled because it has already been used.
What would be the correct way to use AJAX for this:
this is my structure

 function MakeApp() {

        var AppWith = $("#CompanySelect").val();
        var AppTime = $("#TimeSelect").val();
        var Yritys = $("#YritysNtext").val();
        var Henkilonimi = $("#HenkilonimiText").val();
        var Asema = $("#AsemaText").val();
        var PuhelinNR = $("#PuhelinText").val();
        var EMail = $("#EMailText").val();
        var Keskustelun = $("#KeskustelunText").val();
        var app = { AppWithYritys: AppWith, AppTime: AppTime, YritysN: Yritys, Henkilonimi: Henkilonimi, Asema: Asema, PuhelinNR: PuhelinNR, EMail: EMail, Keskustelun: Keskustelun }

        var request = $.ajax({
            type: "POST",
            data: JSON.stringify(app),
            url: "/api/Appointments",
            contentType: "application/json",
            dataType: "html"
        });
        request.done(function (podaci) {
            if (podaci != -1) {
                alert("You Have successfully made an appointment");
                location.assign("BookAppointment.html");
            }
            else {
                $("#p1").html("Greska pri unosu");
            }
        });

        request.fail(function (gr) {
            $("#p1").html(gr.statusText);
        });
    };
Mayank Patel
  • 1,563
  • 1
  • 14
  • 19
MicroDev92
  • 169
  • 1
  • 15
  • 3
    AJAX doesn't have anything to do with retrieving data from a database. That's all happening behind whatever `/api/Appointments` is. Where is the actual problem occurring here? – David Feb 20 '17 at 13:27
  • I guess You didn't understand my question. I'm trying to fetch all the hours that are in use, depending on the company selected, and then disable it. Or did You understand it? I can't use Ajax for that? Then how can I do it through the controller? @David – MicroDev92 Feb 20 '17 at 13:33
  • 2
    It seems more likely that *you* aren't understanding your question. What currently *isn't* working in your code? What specific technical operation are you attempting and how is it not working as expected? We can't really do anything with high-level business requirements, so terms like "all the hours that are in use" or "the company selected" mean nothing here. But if you have a specific technical problem then we can help with that. – David Feb 20 '17 at 13:35
  • It's not a business requirement, it's a practice project actually, that has 3 companies and 3 hours. The code is working I just can't figure out what I have to add so that the application can "fetch all the hours(from a huge list of 9:00, 9:30, 10:00)" and then disable those that are in use? Is it possible? – MicroDev92 Feb 20 '17 at 13:39
  • Again, your business requirements (whether they're from an actual "business" or not is immaterial) don't mean anything to the technology. Focus on the specific *technical* problem. Where are you getting this data? What does the data look like? What operations are you performing on the data? Just giving us requirements and expecting us to write your code for you isn't going to get you far here. As for "is it possible", yes, it is possible to retrieve data from a database and perform logic based on that data. But there's nothing about that in your question. – David Feb 20 '17 at 13:41
  • well im not asking You to write the code for me, I'm asking You to help me write the code myself. I'm learning, and I use this website for helpful suggestion, I'm sorry if You think otherwise. People would usually suggest and explain their suggestions, it is one of the points of this website is it not? Since I have no school knowledge this is all I have apart from a few tutorials and msdn. And yes, my question was supposed to be your end sentence but I didn't know the correct way of expressing it, sorry. – MicroDev92 Feb 20 '17 at 13:47

2 Answers2

1

Actually it's your server job to manage data and database. AJAX is only a way to send information to a server aysnchronously. What you could do, is when you load the page, you retrieve only the occupied time with AJAX, you disable their options in your select, and whenever your server receive an request, it checks if there is an available place for the company and times.

I'm sorry i don't have a code for your since I think it's pretty clear. If it's not, feel free to comment, i'll try to help you the best i can.

Edit

Here I have a few lines of code, it's not complete since we are missing a few informations but it is the main algorythm.

Your server:

{GET}
public void getUnavailable() {
    //get all times from Databases for today's date., 
    //Encode them in JSON.
    //returns the times.
}

Lets assume that your JSON looks like this:

[
{
    "company": "Nokia",
    "times": [
        "9:30",
        "10:00",
        "10:30"
    ]
}
]

You need to retrieve your JSON and parse it to disable the time in the selected select:

$(document).ready(function(){
$.ajax({
    'url': API_URL + 'event/getUnavailable',
    'method': 'GET',
    'success': function(data) {
        $.each(data.data, function($index, $company){
            var select = /* .. Get the Select of the current company .. */
            $.each($company.times, function($index, $times){
                select./*.. Find the time associate with $time .. */.setDisable(true); // I don't know if setDisable is the correct function, you might want to check this out.
            })
        })
    },
    'error': function(error) {
        console.error(error.data);
    }
});

$('.myForm').submit(function(){
    // This is where you submit your data to your server.
    $.ajax({
        'url': API_URL + "event/create",
        'method': 'POST',
        'data': /* your data */,
        'success': function(){
            console.log('success');
        },
        'error': function(error) {
            console.error(error);
        }
    })
});
})

This is the most I can do with the info I have.

Rodia
  • 1,407
  • 8
  • 22
  • 29
Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • I understand what You're saying in theory, but my practice is bad, I'm trying to learn still, and this is practice. if you don't want to bother with the code you can maybe suggest some tutorial which would be helpful? – MicroDev92 Feb 20 '17 at 13:51
  • 1
    I don't have a tutorial in mind but i'll try to get you some code which you could understand. Give me a few minutes. – Nicolas Feb 20 '17 at 13:57
  • this could be of help, there is just this JSON part, I don't quite understand it, is it the way i fill my – MicroDev92 Feb 20 '17 at 14:26
  • and in getUnavailable() should I create a list and populate it with times that are already in the DB? then return the list? – MicroDev92 Feb 20 '17 at 14:47
  • Yea, that's exaclt what you should do, – Nicolas Feb 20 '17 at 14:52
  • 1
    It's fine don't worry, Programming isn't a easy field, but it's a fun one you will see ! – Nicolas Feb 20 '17 at 14:56
1

The real way to handle this, is whatever web technology you have behind /api/Appointments, is to return whatever appointments are available. Your variable names don't make much sense to me, so try to understand what the code below does.

$.get( "/api/Appointments", JSON.stringify(app) )
  .done(function( data ) {
    //note that the "data" variable holds your returned appointments
    //I would return a json document of available appointment times to filter your select


    //sample json would look something like this
    // { "availableAppointments": ["9:30 AM", "10:00 AM"] }


    // and then iterate through available appointments and populate your select
    for(var i = 0; i < data.availableAppointments.length; i++){
         $('#yourSelectId').append($('<option>', {
           value: 930,
           text: data.availableAppointments[i]
         }));
    }

  });

Please note this code may not be syntactically correct.

Here are some links that helped me answer this for you, in case they might help.

Adding options to a <select> using jQuery?

https://api.jquery.com/jquery.get/

Community
  • 1
  • 1
ddeamaral
  • 1,403
  • 2
  • 28
  • 43