Typo3 jQuery OnClick Events - javascript

i need some help for my fresh installed Typo3 Website.
I want to set new Values within my DataBase via jQuery onClick Event.
So i guess i need to make use of this class: TYPO3\CMS\Core\Database\ConnectionPool.
Can someone Guide me how i can make use of this class within JavaScript.
I already tried the following, but i can't get it to work
define(['jquery', 'TYPO3\CMS\Core\Database\ConnectionPool'], function($, conPool) {
if(conPool.foo == 'bar'){
conPool.init();
}
});
Typo3 source
I appreciating any Help. Thanks

Your frontend should not be allowed to communicate directly with the database of your TYPO3 instance.
If you allow it anyone can inspect your javascript and get credentials to access your database - and do anything they like. :(
The correct way is a server request with the data you want to change.
then some PHP can compute the data (verify, adapt, ...) and change the value in the database (as PHP has the local credentials to access the database). Then it's up to you to give a notice in the front end if that operation was successful or not.
Such a server-request could be a complete page request, but a simple AJAX-call would be enough. Though you does not need to rebuild the complete page. From the Return of that AJAX request you can show only a notice about failure or success as a flash-message or similar.

Related

PHP to XMLHttpRequest SQL Delete

So I have a table that is populated using AJAX show on a webpage and am able to generate it live from database results, with this though I want to make more of a friendly web front-end and be able to delete from the database, but I want something that will live update, and am wondering how I can turn something like this
Delete
Into an XMLHttpRequest Delete.
The way the above works is by adding that unique IP to the URL then the PHP grabs it and refers it back to the database to then to delete it.
I know the above method is unsafe but it's for a college project to show we can do it.
Any help would be greatly appreciated!
Add a "click" eventlistener to the hyperlink element, which executes a function where you send an XMLHttpRequest to your hostRemove.php
Have that php script return some value(s), that you check in your XMLHttpRequest's OnReadyStateChanged callback function, and to which you react appropriately (by deleting the concerning row through DOM manipulation, showing an error message if failed, ...).
Don't forget to catch the event when you define eventlistener's function, and call PreventDefault on it. Alternatively, you could use a button instead of a hyperlink.

updating view when data in database changes

I have a problem with project iam working on. In one route(program/messages) of my application, user can create and edit messages(using ckeditor textarea). These messages are saved in database. On another route(program/display) the application generates html site with messages(retrieved from database) created by user. The problem is that i need to update the display view(without site refresh ofc), when user change something in messages data(edit, or create new / delete). Any solution? Iam using codeigniter for backend.
Your view page must contain an ajax script. Which will check for database changes upon certain interval. That's all. I think ajax is new for you. Please grab a bit more AJAX concept. It's pretty handy ..
You can see W3school --
http://www.w3schools.com/ajax/default.asp
If you are in a hurry .. then ..
https://thenewboston.com/videos.php?cat=61
You can use
Jquery ajax() function
Javascript setInterval() function
Set the interval to certain time which will execute the ajax function to see if there is certain change in the database. if there is a change then update the view in success of ajax call.
There is another solution by using triggers in the database. But I am not quite sure about this.

How do I measure the progress of an ajax call? In Php with jquery/ajax

I have an AJAX call that is running a long PHP script where it has 20 different results, I would like to show when each step in the script is done.
Like so 1/20 done, 2/20 done, 3/20 done.
Latest 29-12-2015 03:17.
I tried to create the JSON file as so (jsonFileName_uniqueTimeStampHere.json) by PHP, but the time taken to create the file with PHP, result in a 404 file not found error!
Because when the AJAX call is running it comes to the progress call before the file has been created, I know I can't create the file with JavaScript but is there anyway to create.
The file before the success callback from jQuery AJAX?
What would be the best way to show progress information while AJAX call is running.
The way I have it now, I have a JSON file saved on the server that gets updated with the latest state that has completed, but if multiple users is running the same script the JSON file with the state gets overwritten.
Should I save the state of each progress in DB and then receive it with multiple calls to a PHP method that get state that has been completed?
Should I keep using the current method I use and add a userID to the JSON file so it is unique on each call to the file?
How should I go about doing it the same way as Seositecheckup?
What is the best way to make a progress with AJAX and PHP?
Please tell me if you need any more information.
I have looked around and don't feel like the info or half of info, there is to find online has been enough to do this myself.
I would like to use jQuery AJAX and not XMLHttpRequest, I'm looking for something similar to seositecheckup.com, when you scan a page you can see the state update on each completed function in the console and is done with different AJAX calls. How is that possible?
Should I forget about using jQuery and keep focus on plain JavaScript instead?
Right now I have a setup with jQuery that works the problem is, that I use a JSON file to get the result from and it gets overwritten when multiple users request the same script, is it possible to store the state in db instead and receive it from there with some unique identifier?
In the future I would like to make it possible to put the script into a queue that could be run and when the script ends it should send an e-mail to the user.
The HTTP way of handling requests that may take a long time is for requests to return a 202 and the body of the response should contain the URL where the user can query for the result.
#Request
POST /some/entitities
...
#Response
HTTP/1.0 202 Accepted
/jobs/{jobId}
The user can then poll /jobs/{jobId} which can return a number to represent progress. Do you have to use this? No, but if you do, others developers can immediately recognize what is going on.
Even if you don't use the approach I recommend, you will also have to keep track of job progress in your database and have a separate AJAX call to find out the current progress.

Using JavaScript to send String to Servlet, and Results from servlet back to JavaScript

First of all: sorry for my bad grammer. English isn't my native language, but i will try to exlpain my problem as simple as i can.
I'm working on a web-application, where user can enter a link. (Question 1) This link should be send to the server/servlet and will be progressed to other things. (Question 2) After the progression, the servlet will send a json-array (?) back to the javascript-part of my app.
I'm completly new to this kind of stuff, but its very important to me, to find out how this works or better, how i can make this work. Its actually very simple, but i used plenty of weeks and cant figure it out.
The application is using the SAP UI5-libs (Question 3), where i would also like to know, if there is any possible way, to parse JSON with the UI5 libs.
I hope, i could explain my problem good enough, so i can get some help. Thanks to all!
The 'sending' of the string to the server/servlet would happen via ajax in either POST or GET form. That is up to you.
I recommend you use a javascript plugin like jQuery (JQuery Ajax API) because the regular ajax code is a bit messy.
As for the servlet/server communicating back to the client is as simple as writing to the page. In a typical servlet context it would be something like
out.print("This is a message");
where Ajax automatically returns the content of the entire page upon callback.
So in conclusion:
Consider test.jsp your servlet. I wish to send "Hi" from the client (being the browser) via GET to the servlet and I want the servlet to say "Hello" back.
I would open an ajax request of type GET to the url "test.jsp?param=Hi". In the servlet I receive this page request and process it. The servlet discards the parameter because it is not used and outputs "Hello" to the page.
In the client the ajax will have returned "Hello" and I can use this to put it into a var or whatever and all of this happened while not refreshing and not navigating in the original document where I did the javascript.
Another way is using websockets where you basically use sockets in javascript to send and receive any kind of data.
Also please check out this possible duplicate question: How to send a string to a servlet from javascript using xmlhttprequest

How do you make a link perform an action without reloading a page?

The clearest example of this I could think of is the Reddit Upvote/downvote buttons how when you click the button, the value for upvotes is updated, the upvote button lights up, and the page DOES NOT reload, you just stay exactly where you are on the page.
I am trying to make a feature similar to this and I can totally figure out how to do it with reloading, but I want it to not reload so the user experience isn't disrupted.
Is it possible to do this with php? or would I need to use javascript or something?
The action I would need it to perform would be a basic update query in the database.
This would be done with an Ajax call to your php script. Ajax is designed for these asynchronous updates and/or reloads.
Another way you can do this is with HTML5 WebSockets. You could have the client send a trigger to the server when the user clicks the upvote, and then the server could update and push back the data. It would, however, be a bit overfill for this.
If what you want to do is to contact a server to either send it some state or to retrieve some state from the server (or both), then you would use AJAX with javascript in order to contact the server without reloading the page. You can then also use javascript to update the state of your page after the operation. That is generally what the Reddit page you refer to is doing.
Conceptually, you'd set up your page like this:
Put the link on the page.
With javascript install an event handler so you are notified of a click on the link.
When the link is clicked, your event handler will be called.
Prevent the default behavior of the link so the browser doesn't navigate to a new page.
Then, in the event handler, send your data to the server using AJAX. You will obviously need a URL on your server and server process that can accept and process the data for you and return a value if you need to.
If you need the response from the server, then set up a callback function for when the AJAX call completes (this will be some indeterminate time in the future).
Then, if you need to change the current page in any way (like show one more upvote), then you can modify the current page with javascript to show that new state.
Ajax is easier to use with a library (like jQuery) that contains some ajax support code, but you can certainly implement it in plain javascript too.
Here's one example of ajax with plain javscript. You can find many other examples with Google.
This MDN tutorial on AJAX seems pretty helpful too to show you how it works.
You could use JavaScript to do this. Here's a quick sample:
Vote Up
Simple solution in JavaScript:
var el = document.getElementById("upvoteBtn");
el.addEventListener("click", onVoteClick);
function onVoteClick(e) {
e.preventDefault();
// do something
}
Here's a fiddle.
NOTE: I see you'd be updating the database. In that case, you would have to use AJAX in the onVoteClick function (or use XMLHttpRequest) for this. JavaScript is a client-side programming language and will not be able to communicate to the server without the use of AJAX or XMLHttpRequest. Using the jQuery library, you should be able to write AJAX pretty easy.
It's called AJAX.
With AJAX you can send a request in the background.
The easiest way is to use the jquery libary for this.
You can also output some data as JSON back to the script if you want to take some other actions depending on the result from that query.
A good tutorial is this one.
It also explains how this requests (called: XMLHttpRequest) work.
You need to use Javascript's XMLHttpRequest
You can use AJAX...
It allows you to use JavaScript (client side) to call server side functions. Here's a good example.

Categories