Reading in a text file in Javascript or Jquery - javascript

Im new to Jquery and Javascript and im wondering how can i read information from a text file stored on my server and write back to this file again afterwards? I couldn't find much information after googling the problem
I would also like to read in the data line by line and store it in an array, is this possible?
Thanks,
Matt

I think you want to use some server side programming. If you want the data on the client, you can use javascript or jquery using some ajax solution.

You can read the file if server allows to get it via http but you cannot write to it
Read file can be done by simply $().load(url)

You could use a RESTful API in order to achieve this. Use HTTP GET (jQuery/AJAX) to get the contents of a file, edit what you wish, and use a HTTP PUT (jQuery/AJAX) to write the changes back to the server.

Read information - yes via ajax call. The javascript will get the text from the page - whatever it is html/php/asp and return it to the calling page.
Read line by line - it is possible if you write it yourself. There is no built-in function for this. But then the text should be divided by lines.
Write back - JavaScript can`t do it by itself. You need an ajax and serverside.

Related

can AJAX do anything else than load a JSON file?

I'm want to (or think I need to) use AJAX to accomplish what I intend.
When clicking on a specific link in a list of links, I want to fill the HTML markup below with content of specific subpages. The data is naturally somewhere in the database and actually easily accessible with the CMS's API (I'm using Processwire).
I'm quite new to coding and especially AJAX and all documentation I find online only mention it in combination with a JSON file that would be loaded via AJAX.
However, I don't have a JSON file on the server, that means, according to my understanding, I would need to
store the data I need in a multidimensional php array,
use json_decode to create and then save that JSON-file on the server,
load that file via AJAX and process through more JS.
Let alone keep that JSON-file updated (or create a new one and delete the old one?) since new content will arrive periodically. It seems unnecessarily complicated to me, but what do I know.
There's got to be a better way…
Any help is appreciated.
AJAX is simply a way to make a request to the web server for information.
When you make an AJAX request you ask for a response from a file on a server. So, you can send an AJAX request to a PHP script for-instance.
The PHP script could return anything, JSON is common and very widely used response format, but XML might be another one you've encountered.
So, your request for information is made using AJAX, and the response you get back is JSON.
You don't need to store a JSON file on your server. You just need to make an AJAX request that returns current data in JSON format.
AJAX allows you to do asynchronous HTTP requests.
You can of course ask for a json file, but you can also (for example) call an API.
I suggest you start by reading the the getting started guide for AJAX in MDN:
https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started

How to get the values of session using js

I want to know if the user has logged in or not and then display different elements of the navigation based on the username (login by email and password Not username). However, I have no idea how to deal with session. If php and html is separated in two documents, how can I store the required values using session in php document and then get them using javascript in html document? Or should I use cookies instead?
There are a several approaches to do this.
1) You can make a PHP file which will format your $_SESSION data, and all the other data you want as a JSON string (json_encode function in PHP lang). Then use echo to return it. Then use an AJAX request to get this file from javascript or JQuery, and you will receive the data you want. That's a bad approach for this purpose, from my point of view, because after loading the page you send another request to receive a static data, which will not change on the page like email or username.
2) Better approach: PHP is preprocessor hypertext. You can make an php file and write html tags in it, with php output(example: <div><?=$_SESSION['email']?></div>). Search for more info in google ("php inside html").
3) Much better. In modern web programming world its a mistake to use php inside html because you should think not only about how to make something work, you should think how you will maintain it after 3, 6, 12 months later, too. If you use just php inside html in a big project, then, with time, you realize that your code is hard to read and looks ugly. There are plugins that can make your view more readable and maintainable (Twig, Blade, Volt and others). I recommend you use one of them.
The session is a server side thing, you cannot access it using javascript. You can write an Http handler (that will share the sessionid if any) and return the value from there using AJAX

$.post() html to another html file

Is it at all possible to post HTML to an HTML file using jquery's post method.
for example:
$.post("database.html", "<div>Content</div>")
once a button is clicked I want to post to the database file and have this current file load in content from the database file using.
$.load("database.html")
Does anyone know how I could implement this method properly?
If I rewrite your question a bit: you want to pass data between two html pages. If you only use static html you cannot do that with javascript only. You have to send the data to a server and then use this data in your second html file.
But I think you quite misunderstood what http can or cannot do, I would recommend reading mozilla explanation
It is possible to pass HTML content in POST from one HTML page to another. But in order to use or process received the data at the second HTML page, you'll need a programming language.
Read the accepted answer to question How to read the post request parameters using javascript.

JavaScript GET to retrieve one variable

I haven't really written any javascript but am building an iOS application that will utilize JavaScriptCore's framework to read a javascript code to get a variable. What I'm looking to do is set up a GET (I think) so that I can retrieve JSON data from a url and then pull a specific string from the JSON data. Within the GET method, I'll need to add credentials and one parameter. What is the best practice to do this?
As per Rory's statement above the server you are requesting the json data from must either be on the same domain as your Application/Js code or support the CORS headers.
If the above is true, then you can either use JQUery as suggested above, or for a more minimalist approach the W3Schools has a tutorial on basic Ajax.
https://www.w3schools.com/xml/dom_httprequest.asp

Obtaining value obtained by javascript using PHP

I was able to extract user information from the social Graph API and display it on my site within tags . How do I now get this information, and use it in a PHP script? In other words, say I want to use PHP to calculate the weather, but only based on the preferred weather on a users facebook profile(hypothetical situation). Now, assuming I have obtained the users preferred weather using javascript, how do I use PHP to get that information in order to perform my calculations? I guess one way would be to use regular expressions to match the values on the same page, but that seems tedious. Any help/direction is appreciated.
Are you using php to access the Facebook API or the pre built HTML they provide? You should use php to retrieve the data, do what you need to do with the data, and then display it on a webpage, rather than the other way around.
You can use javascript to get the data, then send it to your php script using AJAX, but this is a round-about way of doing it.
You can make the call the to the Graph API directly from your PHP script. Something like this:
$r = new HttpRequest('https://socialgraph.googleapis.com/<method_name>', HttpRequest::METH_GET);
You will need to parse the data when you get the response -- json_decode() should do the trick.

Categories

Resources