I am making a website but I get a bit lost programming dynamic sites.
The user needs to enter x (inside a textbox), click submit, process in java (serverside) and present outcome as a report to the user (using javascript).
I'm at the moment using JSP to process the users input but now I need to pass the JSON code into the javascript. The javascript requires JSON data.
At the moment I have JSP which returns the necessary JSON code and the Javascript which works with hardcoded JSON code. I need to somehow store the returned JSON (from the JSP) in a variable and pass it to the Javascript. I have a vague understanding of AJAX - i'm just unsure if this is possible and how to link it all together.
Thank you.
Your JSP "page" can simply generate JSON directly. There's no reason the output from JSP has to be HTML. From the client, therefore, you POST to the server, the JSP runs, and the result (pure JSON from your JSP) is sent back to the client as the ajax response.
Sounds like the perfect place for AJAX - you can submit the request with javascript, and when it comes back process it further with javascript. Unless you want the page to refresh.
If you are using jQuery, you can look here to see how to implement it, should be relatively painless: http://api.jquery.com/jQuery.post/
Related
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
I couldn't really find anything online for what I was looking for.
Currently, I have some php code that grabs news feeds and every time the loop runs through, it stores it in an array slot {0,1,2} etc. The interesting part is, I don't know how to refresh the php rss grab function without refreshing the page.
Essentially I have index.php, and with code inside, and I'd like to re-run the php script in those arrows <> through javascript.
I know in javascript you can assign script names and call them in html, that's essential what I want to do but for php, is it possible?
Currently your RSS fetching logic is intertwined with your presentation logic. You need to separate them so the RSS logic can be called independently.
Consider a structure like this:
rss.php - script with logic to fetch RSS feed and package it up as PHP array
index.php - require(s) rss.php and wraps the results in HTML
api.php - another script which require(s) rss.php, but responds with JSON data that can be consumed by Javascript
Now you can present the RSS data when the page loads and then periodically call api.php via Javascript to update the results.
You could also forgo calling rss.php from index.php, merely have it present an HTML skeleton with the javascript and when the page loads, have the javascript call api.php right away to build the initial list. That way you don't have the presentation logic in two places.
PHP is a server-side language, meaning your script will be run by the server before it is returned to your client (the browser).
What you can do if you want to continuously get new data from your script is call your server periodically with JavaScript using an AJAX call and display that to your user.
See this and this.
I'm trying to write a simple web app that will read from a 1D barcode and insert the value to a MySQL database.
Ideally this website will access to a camera and just scan the barcodes, that are shown to it. There will be no further user interaction.
I have achieved scanning the barcodes and extracting the information in Javascript using ZXing. Now my research has shown me that you can't just insert a php inside javascript. So I must stop the infinite loop of the function and pass data to php, where it can be inserted to MySQL. However after I return from the function and update the database, I need to refresh the webpage to scan a new barcode.
The problem is here I don't want to refresh the webpage because the browser, that runs the webpage won't have any mouse/keyboard(user interaction). How can I call a javascript function infinite times without refreshing my browser?
After scaning, send data to php by ajax (try some javascript framework like jQuery or others...)
By javascript, you can refresh page in oncomplete state of ajax request without any keybord or mouse action.
I think your best chance is to take a look to javascript Ajax calls.
In client side
- The infinite loop call to a function that handles de ajax call. That ajax call should sent a GET o POST to a php page.
- You don't need to refresh the page. If you need to return some data, do it in the ajax response function.
In the server side
The .php handle the insertion of data to MySQL.
Recomendation:
Use jQuery, a javascript library: https://jquery.com/
Take a look:
Using Jquery Ajax to retrieve data from Mysql
I am working on a webapp. I have a webpage that sends some data to the server via form tag. The server returns a json array. I want to know how to parse that json array without loading
my page. The purpose is to keep my webpage as it is and load some values from json array to
some text fields below my form tag for further processing.
edit: I have tried some examples for reading from json array but how to prevent webpage realoading.
Please use Ajax for this. Ajax calls are done using Javascript as per your requirement. If you are using library like JQuery it is quite simple to use.
Link to JQuery - Get: http://api.jquery.com/jQuery.get/
One of the ideas to save you from changing anything in your existing code is handle onsubmit of the form. Send your get/post on submit via ajax and cancel the submit of the form.
I am curious if this is possible:
on the website:
using jquery to get value from a html tag and store the value as a variable
using ajax to call a file (xml, html or txt) on servside or external site for a php file
define the current value in the xml html or txt file
and add this value to the current value. = new value
store this new value in the xml html or txt (saving the file on server...)
Q1. Is this achievable?
Q2. If so, what file is best used/supported? (php would be my very last alternative option) I prefer xml and html...
Q3. step 1 to 4 i can get to work - but step 5 i have totally no experience in. Anyone can guide me the right direction for writing those documents?
You cannot write the file directly in the server using javascript.
What you can do, for example, is send the updated data to a page in the server (sending a request to a PHP page, or a MVC endpoint, or whatever server-side technology you want to use) and then have the server code update the file using that data.
You can do this with AJAX (eg. using jQuery with $.post) if you want it to happen in the background, or with a normal request (which will reload the page or take the user elsewhere depending on the server code).