-1

I have an assignment brief for Uni which states that we must use AJAX and JSON to retrieve "stored" form data for a booking. It explicitly states that For this assignment it is not necessary to submit the form back to the server.

I had thought of using local web storage to save the form data but I'd likely get marked down for not showing use of AJAX/JSON. The other alternative would have been if javascript could have filesystem access (not using nodejs), but it doesn't...

So, from the brief below, how would you go about showing off AJAX/JSON features for storage/retrieval without using a back end server?

This assignment is focused on learning jQuery (but not including plugins or frameworks such as Bootstrap, Foundation or similar) and building components into a rich internet application (i.e. a single web page application) that demonstrates appropriate features of jQuery and how to include AJAX and JSON data.

You are to develop a web page to help a traveller organise the accommodation arrangements for a short city break and allow the person to review their ‘saved’ booking. The aim is to demonstrate that you can retrieve the booking information for the person and display all the appropriate fields with the returned JSON data into the form. You should allow the user to amend the form data and write the code to validate the fields in the client browser. For this assignment it is not necessary to submit the form back to the server. You should include a ‘Terms & Conditions’ cookie to note a new and returning user to their saved quotation.

Cheers Dave

Dave C
  • 457
  • 3
  • 10
  • 2
    For AJAX to work you will need to request the data from a server, you cannot make an AJAX request to the local file system for security reasons. To save the data you could just use `localStorage`. I'm confused why you're asking this though as the assignment explicitly states: `For this assignment it is not necessary to submit the form back to the server.` – Rory McCrossan Oct 12 '17 at 15:13
  • 1
    `For this assignment it is not necessary to submit the form back to the server.` means don't save the data to the database, just check that you have the `$_POST` and then return some **success** or **error** message! You'll make it more complicated trying to save to a file ;) – teeyo Oct 12 '17 at 15:14
  • 1
    You are being asked to _fetch_ data _from_ the server, but it is not required that you then send any changes back to the server. – Patrick Q Oct 12 '17 at 15:18
  • @RoryMcCrossan - precisely the reason I'm questioning the wording of the assignment... so assuming there is no server as they don't want it, how are you supposed to show off using AJAX/JSON. – Dave C Oct 12 '17 at 15:33
  • By *retrieving* the data... – Rory McCrossan Oct 12 '17 at 15:33
  • Cheers Rory, perhaps I was adding more complexity to it than is required... seems silly to load it from a hard coded file but so long as it gets the grades! – Dave C Oct 12 '17 at 15:50

1 Answers1

1

You need a server to do this. Like teeyo commented "For this assignment it is not necessary to submit the form back to the server." does not mean that you do not need a server.

Solution

You can get the data in the form with javascript(jQuery) and translate them into json format. You then send them via ajax to a server file(php). The server then stores them into a file and returns the filename to the javascript. You can now access the data with a new ajax request.

FORM -> jQuery { translates to json } -> sends json { via ajax } -> server { storing the data } -> sends filename { ajax response } -> jQuery

Useful links

How do I encode a JavaScript object as JSON?

http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php

  • Cool, I know how to do all that, just the way the assignment was worded I thought it was being asked to do it without the server in the middle (which isn't possible). Thanks – Dave C Oct 12 '17 at 15:35
  • Ok so the question is answered or can we help you with another problem? –  Oct 12 '17 at 15:37
  • It's all good thanks! I may have been over-complicating the problem :) – Dave C Oct 12 '17 at 15:46