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 inherited a php site that is returning HTML pages. I always thought that the server returns data to the client and the client decides how to show the results.
Even though this is working, is this not a very tight coupling between the server and the client?
I would have thought a much better way to handle this is for the client code, javascript or gwt or what have you to ask for the needed data and the server returning that data only such as JSON object or a similar thing.
Thoughts on this?
It sounds like you could benefit from making an AJAX call (via js) to a php page, then manipulating the data (JSON object, string of comma delimited data, raw HTML, etc.) returned on the client side.
Sorry for the bad previous example, this example is a more sophisticated, modern example of how an ajax call should be made.
It appears that it is not at all uncommon for php scripts to return HTML. It does create tight coupling with the client application. Returning JSON does create a more loosely coupling with the client.
Related
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
Sorry in advance if what I am going to ask is silly, I am having trouble understanding the asynchronous HTTP responses. I am trying to use jQuery .ajax() with PHP and MySQL in the following context:
when page loads I will query and return all the applications for a particular id, and then I will use a while loop to output them;
also there is a button that allows the user to add new application for that particular id -> on click it:
shows a hidden form and gets the values in the inputs;
sends the data to PHP using the .ajax() method;
performs the insert.
and I am stuck at this point
I need to output the newly added application above the existing ones, but the query and while will do it only when an synchronous HTTP request is sent.
I tried to use the .success() but the HTML I need to output inside is really long and I am afraid it will be hard to maintain having outputs from both PHP while loop and jQuery.
Can you please help me understand an efficient way to do this? I have never dealt with asynchronous HTTP request before. Also, I am can't use any JavaScript templating libraries.
I would really appreciate your help!
An ajax postback typically implies some sort of JSON-based web api behind it. Examples of this on the internet abound.
https://www.lennu.net/jquery-ajax-example-with-json-response/
http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html
Your REST endpoint should return the minimal amount of JSON data you need. At which point you can use that data to update/bind to elements in your DOM inside your "done" or "success" callback.
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 created an app with PhoneGap but I need to load some stuff from another domain. I tried jQuery load with a xdomainload plugin but It did not work.
The stuff I need to show run on PHP as the back-end language. I'm really noob - I've seen a few tutorials about JSON but all of them did not work and I ended giving up. Is JSON the easiest way?
Thank you all
Generally speaking, PHP wont run client-side in Cordova. If you're trying to access some sort of backend service that you built using PHP, then that's doable with JSON. Whether it's "the easiest", makes this question to open for interpretation. Generally speaking, a common approach may be to use javascript to communicate with backend services which "talk" JSON between each other. This question might get closed for being to opinion based. But that's my 2 cents.
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 7 years ago.
Improve this question
Is it a common thing to mix PHP and JavaScript?
Lets say I declared a PHP function returning a string:
function myPhpFunction(some_string) {
return 'blablabla' . some_string;
}
Then once the DOM is loaded is it correct to do something like:
$(document).on('click', '.my_class', function() {
$(this).html(<?PHP myPhpFunction('some_string'); ?>);
});
Not exactly like that, but there are ways to achive what you want to do!
But first, some basics:
PHP is a server side langauge. It processes data, makes HTML, send the HTML and javascript back to the browser.
Javascript is a client side language. It works in the browser and not on the server side (apart from node, but we won't go into that).
What you want to do, is pass some information from your PHP to your javascript.
You would put the javascript code you have there, in a PHP file. The PHP code will then replace that section with the resulting string, which will result in that value being sent to the client in the HTML.
If your javascript function or event or whatever, is ran multiple times, it won't be able to run the PHP function again, as it only runs once to generate the text of the HTML which is sent back to the client.
Hopefully that all makes sense.
P.S. remember to search stackoverflow for your question before asking it. I'm pretty sure someone has asked and answered this question before. Normally it will be flagged, but I'm feeling nice today, so you get an answer.
Feel free to ask further questions if I don't make complete sense.
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'm very confused about how to do a game work in a browser like monopoly, backgammon, etc. I have knowledge in html, css, php, and my question is what should I start to study/learn to make a game play with anothers on the web, I think what I should start to learn is AJAX, but what else? How can I do the game? How should I arrange the mysql database info for the game... any tips is appreciated. Thanks.
A basic requirement would certainly be Javascript. That's what the "J" in AJAX actually means. You basically need two things: the client-side code dealing with the user input, sending requests to the server, dealing with the responses, managing all the graphics; and the server-side system handling requests triggered by the client code and providing adequate responses. How you should "do the game" and "arrange" the MySQL database strictly depends on the game you want to create. Of course your request is too generic to be able to provide you any meaningful help.
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
It could be considered as a general or a simple question but I really need to find out the best practice for handling ajax calls. I've seen a lot of different implementations and I'm not sure to choose which. A few requirements of my project is the following ;
The project is simply a social network startup. Therefore, I need an efficient data format to interpret and handle the data in the return. I chosed json type for efficiency purposes.
I'm handling the back-end ajax calls by using php. The ajax calls have such functions like add_task, follow_user, unfollow_user, comment_task, like_task, etc.
I'm not sure to choose an elegant way of requests. For instance, I'm not sure whether I should use GET or POST request to ajax_service.php. I'm also not sure to make my request data type (which will be sended to php file that handles ajax requests) as json or normal(just an array of data) get type.
Can you guys guide me which data type and which type of request should I use in this project ?
Generally, HTTP POST is used when you want to write/modify something to the server, GET when you want to read.
Data exchanged between PHP and JavaScript should be encoded as JSON.
You'll detect ajax requests by reading the X-Requested-With header. If it's value is XMLHttpRequest then its an ajax request.