Ways to store dynamic data produced by jquery [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am learning web development. I have come across a part in jquery where i can dynamically produce html elements like div table etc. But i wanted to know how to store these dynamically produced data. What are all the ways to store these jquery dynamic data?? And if i want to store it in mysql how to do?? It will be helpful if you have any reference links or code. Pls help me with this

The dynamic data often comes from a JSON string. JSON represents an object (or a set of objects) that can be used directly in Javascript.
Using Jquery, you can get JSON with this utility method : getJSON( url [, data ] [, success ] )
So you have to provide an URL responding with JSON, to be used as the url parameter in getJSON function.
It can be either a static file (ie http://domain:port/data/myData.json) or a dynamic content generated by a server side process in the language of your choice (PHP, Java, JS with NodeJS...), ie http://domain:port/myData.php?filter1=value1.
Note : JSON is the most standard format for transferring objects to Javascript, but it could any format (CSV, XML, ...)
In the callback method of getJSON, you will be provided a plain Javascript object that will contains everything that was in your JSON file, and you could use it to produce whatever you want (html elements like div table etc).
If you need to request a SQL backend, you will have to use a server side process to do the SQL request and map it back to a JSON object.

Related

How to email a parsed JSON using a script [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 have a script that gets and parses a JSON, I want to email the JSON values directly from the script when the page loads. I already have everything set up but do not know the best approach about doing this. I usually use forms to send information but this is a little different.
I am not looking for someone to hold my hand and show me how, I just want to know the different options and I can figure it out myself.
I don't think there is any native javascript functionality for sending emails. I would use a service like https://www.emailjs.com/ or the Gmail javascript API if it was totally necessary to do this from javascript.
If you want to automatically send an email using plain javascript from the browser, you can't. You'll have to setup node & use something like nodemailer:
https://nodemailer.com/
If this isn't the case, you can use window.open and pass the email data in this way. It will open the default email client on your computer & pre fill an email with the parsed information. Like so:
window.open('mailto:your#email.com?subject=your_subj&body='+YOUR_JSON_HERE);
Make sure your passing json and not a javascript object. If you're passing a js object it will return [object object]. If this is the case you'll need to stringify the js object, like so:
JSON.stringify(JS_OBJ)

PHP Get Values from a URL using GET[] [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 develop products filters for an online store I am working on. An example of what I mean is http://www.riverisland.com/men/just-arrived. I have managed to get a JavaScript to populate the URL when the sizes are clicked on but failed to get them remove value from URL when unchecked.
My main question here is this. Assuming I have my URL as:
http://127.0.0.1/shop/dresses/?s=1&s=2&s=3
How do I get my PHP to extract the values from the URL?
How do I format a SQL query to search the values gotten from the URL using any sample query?
An easier solution is this.
Format your URL like http://127.0.0.1/shop/dresses/?s=1,2,3 as suggested by #Andrey.Popov. Then do the below.
if(isset($_GET['s']) && !empty($_GET['s']))
{
$e = sanitizeFunction($_GET['s']);
$d=explode(',',$e);
}
$d now has all your $_GET['s'] values.
That's the easier way I have figured out and it works!
In order to benefit from $_GET and other superglobals you have to follow the rules explained at Variables From External Sources. Since you've chosen to have several parameters with the same name and they do not contain properly paired square brackets you're basically on your own. The manual steps you must reproduce include:
Extract the raw query string from $_SERVER['QUERY_STRING'], e.g.:
$query_string = filter_input(INPUT_SERVER, 'QUERY_STRING');
Parse out the string. As far as I know, there aren't built-in functions that do exactly this so I'd either google for a good third-party library or write a simple parser with regular expressions or good old explode().
Decode the URL-encoded values with urldecode()

Javascript(Ajax) vs PHP: Generating HTML Elements [duplicate]

This question already has answers here:
JSON vs HTML Ajax response
(3 answers)
Closed 9 years ago.
Does generating html elements such as table rows with js through a ajax callback function really any slower than php(echo "< tr>< /tr>)? The browser still has to render client-side the information into the table whether its done in php or js.
I couldn't find much information on this subject, so I wanted to get some opinions here. Are there any arguments to not use js to generate elements dynamically from a ajax call?
Note: I originally forgot to mentioned that the table rows would be data from a database.
In your case using ajax is time wasting because it should send request to server, and server should process it, then responding with the result, while using native JS (only javascript) is faster because the server is not included, and using direct print when page loading is fastest.
But if you need to work dynamic tables, you can work on JavaScript without AJAX call, I don't see any reason here to use AJAX for building HTML table.
Its perfectly fine to generate html elements using ajax call. There might arise occasion where you HAVE to return the DYNAMICALLY generated HTML elements.For this you cannot just print it client side since you need the info from the db.
If you have the info to be presented in a tabular form beforehand, meaning before the page is built, then you should use PHP.
If, the data is to be requested at runtime from the server, then AJAX should be used.
If you just want to built tables with no data (or repeating data which is already available on the page) then, using plain JavaScript is the best option.
Suppose you have an existing page with a table
| A | A | A |
| A | A | A |
Now you place an ajax-call reading some data from the server. If you want to replace the whole table it might be usefull to let php create the table, return it to the browser en let javascript replace the entire table.
If you only want the second row to change, you might want php to return an array with values and let javascript replace the cell-contents based on that response.
But one way or the other, you still have to go the circle browser => server => browser. So the few moments javascript takes to alter the page don't add too much time to that.

Getting HTML or JSON response from ajax call, Which is good? [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 9 years ago.
Improve this question
I have a functionality in my project where I have implemented the search functionality.
On submitting the form via ajax, I need to show all the results in a division.
There are two ways I can do this.
Getting the JSON data as ajax response and bind it to the HTML elements.
I can also get the completely formatted HTML response from the ajax call and can directly bind it to the result div on the search page.
So which way is suggestible ?
To make a service (server-side script) the most re-usable or even make it into an API - the suggested way is to return JSON data (converted from data models) to the front end, where using JavaScript you can populate the data to the HTML.
As for the HTML - you can certainly make the server return the response as HTML (setting the correct mime & content type in headers) but this gives the server control over the UI layer and the separation between the interface and the server/db is not balanced properly...
Either option is fine, depending on your how much html there is and how much server-side processing you need to do on the HTML. If it is just a div and a value that needs to be inserted, then I say just go with JSON. The JSON approach will be more lightweight (consumes less bandwidth and keeps the role of the server as an API that is transferable to non web-page requests).
If you need to do a lot of server-side processing and assembling and what you are returning is really a sub-page, then you might consider html from the server. In this case have a partial html file that you read and send (inserting data where relevant) rather than building the html from strings on the fly. If you have a partial file, then you can edit and check it with standard html editors and you can see the html layout easily and it keeps the UI aspects separate from the business logic.
I'd send it as JSON and build the HTML client side for a few reasons:
The JSON payload would be lighter and therefore faster to send over the wire
The API becomes more reusable (if you ever wanted to hook up additional clients that render differently etc.)
If you build the HTML on the client side then it's probably easier to take advantage of templating libraries (e.g. JQuery Templates) or even better, directly binding the data to the UI (such as Knockout)
I always do the JSON response. To me it looks like a much more consistent and flexible way since you can return more data than only presentation. If you still want to return HTML you can also do it through a JSON response:
{
error: false
html: "<div>Done!</div>"
}
I'd send it as a JSON response, too.
As suggested by emiolioicai in his code, with JSON you can easily handle errors. For example:
{
error: true
error-message: wrong parameters
}
If you define your HTML client side, in the future you will be able to use the same AJAX request and customize HTML differently in another part of your website.

How to use JSON.stringify() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to consume data from an API using REST call .
I've succeeded to get data from the API :
You can check demo on jsfiddle : http://jsfiddle.net/zTXyq/33/
Now , I am trying to perform the work to get different resources from this API
I am reading these two articles :
- http://www.jquery4u.com/demos/ajax/
- http://welcome.totheinter.net/tutorials/model-view-controller-in-jquery/
I would like to know why do we use JSON.stringify() if we can show data in HTML pages .
Why do we use so-called 'class'/'model' to parse data using json ?
Regards ,
JSON is a data structure, which means it serves to transport variables, arrays and stuff.
Sending variables in html code would be silly - that is why we use JSON.
JSON.stringify() is an utility function, which is most commonly used for debugging, when you want to print some variable to console, but there are some other uses, too.
console.log( JSON.stringify(myObject) );
What the function does, is that it takes an object, array or about anything else, and makes a readable string of it, while simply using something like this ↓↓ would give you only [Object].
console.log( myObject );
If all you really want is to send some HTML to be displayed, then you don't need to bother with JSON at all, and just use jQuery.load() or something similar.
I hope you will agreee that in javascript code JSON objects are nice and easy to use, for example
var jsonObj { "p1": "1", "p2": "2"};
if ( jsonObj.p1 == '1' ) { // do something }
But if we want to pass that object around to non javascript code, for example to a PHP script, we cannot do it.
So the stringify comes into play, we stringify our JSON object i.e. convert the whole object to a string, now we can pass this around to anything that has the ability to convert it back to something useful. So PHP has the json_decode() function to convert a stringified json object/array to a PHP object/array so it becomes useful again.
Now when we want to pass data back from PHP to javascript we convert a PHP object/array to a json string using json_encode() and we can now pass an object/array back to a javascript as a string for it to use JSON.parse() on so it can convert it back to a usable object/array.
So in summary, stringify allows us to convert language specific formats for objects/arrays into a universally available format i.e. a string. Therefore any language that provides a method of converting this stringified format into its own language specific formats for objects/arrays can use this data and of course return it again via the stringified format.

Categories