Echoing HTML or generate it through JS [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have a functional project that is basically a database table editor. You load a table and you can get the rows info, edit it, add, delete, add child rows, etc.
I am rewriting the entire thing because it is very unorganized. I am attempting to create a class based system (yes I had 0 classes). This is my first class based project and, that being another issue, I would like to know the best (performance, maintainability, ease-of-writing) way to generate dynamic HTML. I use Javascript and PHP
Really the options, when outputting dynamic HTML are:
Echo it directly, or JSON encoded, from a PHP function or class
Build it from Javascript and append things to the document
I can either make a request from JS and append the data directly from PHP, or I can just get back raw data from PHP (database) and build it using JS. I really don't know what the optimal way to go here is. Currently I am raw echoing my tables and editing forms, and it works, but it's a mess.

To provide data (via php) to your WebApp, you should build a REST-Service (in php simply a new php file), perform the database requests in the php file and give the data as JSON String back to the client.
So in your client App you can make a AJAX request to get your data and parse the responseText with JSON.parse(responsetext) to a javascript object. Now you can access the data in the object.
For building webapps with databindings you should have a look on AngularJS. Angular is using a very interesting model to build nice Apps without thinking to much about DOM.
Best regards
Dustin
P.S.: You should also have a look on nodeJS. It's much faster than php and you can code javascript on client and serverside. Furthermore it has a bunch of features like Socket.IO where php cannot hold.

Related

Which is faster ..passing variables to a javascript function or running a MySql query to fetch the values? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I wish to know the following: Which one of the following is faster- passing variables to a javascript function and then passing it on to a php file to carry out some operation or running a MySql query to fetch the variables in the php file without passing them? ..Thanks a lot!
For pretty similar request .. if the related value are already available to te client and you need this to some other page then tecnically
the javascript ajax request involve only the internet transfer of the call and the transfer of the result .. the use of database access in addition to sending the request and receiving data also implies the access to the database so normally should be more fast ths ajax method
Unless you have a specific reason for using Javascript, for instance, updating displayed records without reloading the page, use PHP directly. You can always run the php function in a class on a separate file if you wish to keep your html and php separate.
My personal experience of ajax, which you should remember still has to access PHP in order to execute server-side requests, is that it can appear slow. Whether or not it actually is slower, it leaves the page in place whilst executing the code and so might present that illusion to the user.
Also, I think the simplest solution is the best, so if you don't need ajax you shouldn't use it, but if you do need it then there is no reason not to do so that I am aware of.

AJAX using PHP - Different ways to structure? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Have a question. I am learning AJAX right now and I see that on w3schools the way they have the PHP and MySQL AJAX stuff as examples is the AJAX makes a call to a separate PHP file on the server and then that PHP sends back whatever you want to pull. The example I see is a table is returned with rows and columns based on the query.
My question is, does using AJAX with PHP and MySQL require you to set it up this way (meaning sending data to a separate php page and then getting a return on the current page the AJAX was called from/started at)?
I have a project where I currently have multiple php pages. Some of them being only 'processors' where they take in data and process it but do not spit out any html/text for the users and I have redirects set up to take the user back to a page, BUT then some of my php files have both html AND php handling the processing and spitting out stuff for the user to see on that page.
Ideally I would like to use AJAX and for the pages that have both HTML and PHP (user facing stuff on the page) I would like for AJAX to call the page its on and spit out the returned data from the database. It is not a hard requirement but think it would save me time not having to refactor a lot of the structure, but if that is the only way or is a better design/practice then I will surely go that way.
Hope this question made sense, and as always thanks for all help and ideas.
I'm not 100% sure about your problem here, but I think a good way to organize your AJAX/php code is to create a kind of php API ( in a php file ), with a bunch of functions doing all the back end stuff, and then, in your AJAX calls, you specify which function you want to use.
Hope it can helps a bit ;)

create PDF file in JS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am developing a web application using client side MVC. The technology stack is Backbone JS, HTML5 and Spring at the Server side.
I have a requirement where I do some data manipulation at the client side using Javascript by using JSON files as the data payload kept on the web server directly. This is a high traffic part of the app and I don't want to hit my app server for such siple data manipulations.
Now on the same module - I have a requirement where I need to generate a PDF file which effectively contains a static template and then I need to fill the template using effectively the same data that I already have at client side. I need to generate the PDF and let the user download it.
Any idea how can I acheive it completely at the client side in a clean and robust way.
From what I understand is you have an html template that you want to fill with certain data and then render it as a pdf?
For client side, have tried looking at something like jsPdf (http://parall.ax/products/jspdf)? The html renderer is still in early stages but it seems to work decently.
As Bogdan pointed out, a backend solution is also possible. You could look at pd4ml (http://pd4ml.com/) or even call into something like pandoc (http://johnmacfarlane.net/pandoc/) or even phantomjs (http://phantomjs.org/) to perform the conversion and then pass the generated pdf back to the client.
If it is not an html template, I am sure a number of the above solution should work regardless.

Writing an API using bottle/flask to build data visualizations [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to write an API that will read the data fed through a UI, filter the data and feed the data to a set of Javascript (D3) visualizations that I created and output the result as SVG/PNG/PDF.
How am I going to implement this ?
Let me know if more details are required for you to understand and answer the question.
Any help would be highly appreciated.
There is the HTML 5 drag and drop option that is already explained. But if you insist on getting a remote server involved then when you get data through you UI you need to save it in a file or db and then in your back end you clean the data (I assume your cleaning process is happening in Python for example) and then you have a cleaned data ready to be passed. Here you need flask and templates to pass the data to your HTML and evoke D3. If you take a look at Flask quick start then you'll see how flask comes in handy to mediate communication between a backend code and your HTML. From there you get a better idea. I also suggest reading this blog that explains a mini blog application using flask this covers API, Flask part of your question very well.

Passing data from one element to another on the same html page [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi I am trying to develop a mobile app. The problem I am having is this, when the user fills in a form on the html page I want the data to get written to an unordered list on the same page. I want when the next user fills data for that dat to get written to the second list item in the list and so on
.
Thanks in advance for any help you may be able to give with this problem
If I understood correctly, you're trying to make an app that stores the data of each submitting of the forum and returns them to all users that come across the page?
For such tasks you'll need, at least:
A web server that runs PHP, Python or other language to your liking*;
A database to connect the server-side with (MySQL or PostgreSQL usually);
What you need to do:
Learn about HTTP POST and basic SQL input/output if you haven't;
Learn AJAX if you haven't;
Store the user input in the database and have a server script retrieve it on call;
Update the dynamic page bit (the unordered list) with AJAX (loading a XML file with the PHP script that writes the database info into it should do) every minute or so using a timeout function.
I'll post links to all I've mentioned in a minute.
*NodeJS is getting popular for being asynchronous (making it easier for dynamic pages to interact with the server) and allowing programmers to use the same language client- and server-side, but I have never tried it to tell you how it fares. Edit: says John NaN: NodeJS is not recommended. Again, I don't know it myself; that is why it's on a note and not upper in the post. Good luck!
*Bonus note: don't trust W3Schools most of the time. The AJAX tutorial I linked to, however, isn't that bad and it's easy to follow.

Categories

Resources