AJAX using PHP - Different ways to structure? [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 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 ;)

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.

Getting updating in codebehind C# variable to asp.net [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 wrote a small code in c# that reads files from the hard drive and outputs that info to a webpage using asp.net.
Information about those files is being changed in the codebehind.
I managed to create variables to read and display the info on the webpage, but they change only after a refresh. When I used setTimeout it would always read the same variable, leaving me thinking that the codebehind does not get re-executed.
How would it be possible to have them updated live without needing to refresh the entire page?
This is somewhat vague but if you want updates to the values on your page after loading, you'll need to use some javascript to grab new values and then more javascript to update values on your page.
There are some great frameworks out there. Unless you have a lot of front-end logic you need to perform through javascript, I'd keep it as simple as possible and throw jQuery in there for the AJAX calls (fyi, this is not the only solution, there are 10+ different ways to skin this same cat).
If you want a bit more efficiency, you could look into using SignalR - which wraps long polling or web sockets (depending on browser capability) on the front end and signals those subscribing pages on the backend only when changes have occurred to data.
Link: http://www.asp.net/signalr
There is no "right" answer to your question so the best I or anybody can do is guide you in a direction. Hopefully this answer helps you.

Saving all javascript code in an online database [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 7 years ago.
Improve this question
I am really new to making an async game and I would like to know if you could just get the current gamestate and save it as it is or if I would have to grab all code seperatly and organising them into tables. It would be very useful if there was some kind of function that could save everything at once as I am building a pretty big game and it would take a long time grabbing every last piece of information.
So to summarise: I would like to grab the existing gamestate and put everything in a database, then grab everything again and restore that gamestate.
Thanks in advance!
You'll need to submit (or retrieve) the gamestate data to a server with JavaScript. You can use something like JQuery's ajax function for this.
For the server to do anything meaningful with your data, you'll need to use some kind of scripting language. PHP or Node.JS might be a good place to start if you're new to this.
Once the server gets the HTTP request, you can have it send a query to your database (like MySQL) and send back some kind of response.

Echoing HTML or generate it through JS [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 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.

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