Retrieving data from browser to JavaScript - javascript

I have just started out working with JS and I've managed to post data from a MySQL db to the website using node.js, jade and plain JS.
Now I'm trying to do the other way around, i.e. getting data from the website, to the JS code and then inserting it into the db.
What I'm thinking is simply making a textfield with a button. When I fill the textfield and press the button it is collected by the JS script and the inserted to the DB.
I am however having problems with Jade and the listener and I'm unable to even do a console.log using the listener.
This is what I've got so far in my .jade file.
extends layout
script.
var something = function() {
console.log('something')
}
block content
button(onclick='something()') Click
The website renders nicely, but nothing is printed when I click the button.
If someone could give a hint on how to fetch the data in my .js file that would also be appreciated.

In the context of the WWW there are two places that JavaScript can run.
On the server, e.g. with node.js
On the browser, embedded in a <script> element
Since you want to put the data into a database on the server, you want to go with option 1. So don't use a <script> element.
Use a <form> (you could use client side JS to read the data from the form and send it to the server (Ajax) but that seems overcomplicated for your needs and should be layered on top of a plain HTML solution if you were to go down that route).
form(action="/myendpoint" method="post")
label
| Data
textarea(name="foo")
button Submit
Then you just need to write server side code to retrieve that. The specifics of that will depend on how you are implementing the HTTP server in Node.
The question How do you extract POST data in Node.js? provides some starting points.

Related

How to display an animated icon during python function processing in django?

I am trying to develop an application with Django. I have a form in my HTML file which will pass same data to the server. At server side, my python function(called submit) will receive the posted values and process them and then redirect the user to a new page.
Question: How can I show a loading gif to the user until my python function is processing posted data and finished? I searched on the stackoverflow about this question and there are some answers (this and this and this) but the answers are for finishing Ajax functions, or there is no explanation how can I identify when my python function has been finished or is for flask framework.
Any help would be greatly appreciated.
This is rather simple. First you will have to download font-awesome.css from here http://fontawesome.io/.
Then create one element like <span class="fa fa-spinner"></span> this element will be hidden by default. Set it visible at the beginning of your ajax call and hide it once you have receive the response.
If you want to show your own image, the logic should be the same.

Sending HTML Div as a Email File Attachment

OK, I have a Form in my web application used to fill out some shipping details. Certain orders require printing this form, to accomplish this I have a Div on the page with the display style set to none. It contains our company logo, shipping information and a table that is dynamically populated with information from the form once the Print button is selected. I am using the InnerHTML of the Div to print with a JavaScript function to print the form.
My problem is I also need to optionally email this as a file attachment.
The only way I can see this working would be to save this Div to a file in my application and then attach it to the email.
I have had no success trying save the HTML in to a file though.
I am not stuck to this approach, I am looking for a direction to go in.
My only requirements are that the information is sent as an attachment and is visually appealing - including our logo etc.
Thanks for any help - I didn't include any code, didn't seem relevant.. let me know if you want my print function of anything else from the project.
You need to improve the design of your application. First, it should be the server side not the client sending the email or processing the order. So forget about JavaScript here unless you are using node.js on the server side. Your server side serves the HTML for the client to display, ideally it is dynamically generated from bits and pieces or using some sort of template. As soon as you have that you can use the same code that generates the HTML served to the client to generate HTML send via email. It may have differences but most building blocks can be reused. Having done that just use whatever email library is available, set the content type to HTML, provide the dynamically generated HTML as the body and off it goes. None of that is done in the client JavaScript.

Javascript control page and user view page

Currently I'm working on a project where a user enters a lot of data constantly for a hour long window. I'm looking to have one user control all the data via some control panel and then have a link they can distribute to other users that will allow them to view that data without the ability to edit it.
Right now I'm doing some extremely weird methods. I have an XHR request on the control page that fires whenever a field is finished being edited. From there the data is sent to a php file that converts the data into a simple text file. Then the distributed link file will load that file one time and translate it into the necessary format.
Some potential problems I've run into are it seems odd that I'm sending starting as javascript data then going to a php file then to a text file then translating the data all the way back into javascript data again. Another problem I've come into is I'm not sure of a way to force users to reload the page when a field is edited in the control panel after the user has opened the view page.
Have I totally gone overboard here? What are some better concepts I could employ to accomplish this task?
If i understand what you want to do this is how i will do this:
First the data entry
if you have lot of fields you better use a form wizard, i don't have a particular one in mind right now but there is lot of them just search jQuery Form wizard
Here is an example:
http://i.stack.imgur.com/Luk2b.jpg
The concept of the form wizard is to guide user via multiple page and also validate the data. And click save when and the end.
Then save date in database.
Display content
All you need to do is to create a global separate page to display your content.
Let see something like: http://yourserver.com/view/{id}
where id is the identifier of the particular row in your database.
i'm not sure if i totally understand what u about to do. i'm trying to make your work description shorter here:
want to build a website that one person can edit a single page's content in 1 hour, and others can view the content change in that 1 hour.
if this is what u want to build, here's the module:
teacher: the one who can edit the page
student: the one who can only view the page
server: information center
teacher client edits page -> teacher client sends update data to server -> server saves data -> server sends update notice to student client -> student client receives update notice -> student fetches update data from server
to make this module work well, i suggest try socket instead of http reqeust, just like online games or IMs do.
well, try socket.io

request downloading a file basing on criterias from javascript

I've got a web application (php/symfony2 - but it doesn't matter here) with heavy AJAX usage and quite a lot of javascript in the client side layer. The interface enables the user to filter the data he wants to display (periods, checkboxes, selects, etc.) and basing on those choice, a POST request goes to the server side for data and JSON is returned through AJAX.
Now, I want to add a functionality to download the data file basing on the criterias/filters that reside in the javascript layer - and I'm not sure how should I do it.
Let's say I've got a <a href="some_action_url"> tag that - if I click - it downloads the file. I know how to do all the server-side stuff. But I don't know how to pass the criteria parameters from javascript to the server-side controller a using the <a> tag.

How build a list dynamically and pass/save user selected values from an HTML form to server?

I think in this case I need to write a client side script that onpageload/onchange gets options from server & builds a list dynamically. & stores new options to server via some JavaScript script. I will prefer to save user input in a file on server. I will be very thankful for any help.
Here is the stack we use for our web app and that works more or less the way you describe:
the user load an HTML page
the page make an ajax call and get the options as a JSON(either it exists already in the database, or a new option set is generated)
the json is rendered using a JS templating engine (PURE in our case)
the user change something
the same JSON is modified and sent by a POST to the server
the server read that JSON and store it in the database(you would write the data to your file). And then go back to the step 4, to wait for another user change.

Categories