Efficiently output query result with jQuery .ajax()? [closed] - php

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.

Related

how to get database data from a web page using PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In this case i want to extract data from this URL:
https://www.aeropuertosgap.com.mx/es/index.php?option=com_infovuelo&Itemid=746
I did use HTML_DOM but it (obviously) only extracts html data, it is just flight information!
You can't really access another person's database directly unless the airline provide some sort of API to help you access it.
The only real way I think you could do this is if you POST data to the form and then filter through the HTML and get the required data.
See How to set POST data to URL and get results in string variable
From looking at the HTML of the webpage, I can see the form is posted to: index.php?option=com_infovuelo&Itemid=746 and the results are under the div with id "info-vuelo-result".
Good Luck!
P.S. This is one weird question!
Agree with Yahya Uddin. You need to write robot, sending form data, then parse the response, using DOM queries.
Is this a theory question or you want us to develop the code for you?
IF THIS IS A THEORY QUESTION:
well I did something like this with a calendar where the provider did not have any API or anything else, so what I did was get the content with file_get_contents and using preg_match and preg_replace I achieved what I wanted to do with it.
Also check CURL.

How to implement a database like search form into a website? [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 9 years ago.
Improve this question
How can I go about implementing a database like search form into an HTML website? For example, there is a form to enter your zip code and after entering the zip code, it searches a .txt file (for example) and returns results that fall under that specific zip code.
Any help would be greatly appreciated.
Thank you
There are many approaches you could take. It's a very broad question.
As an example solution, you would need to have both client and server side code. The client would submit the form to a server. The server code could be written in a multitude of languages, PHP, ASP, JAVA etc.
The server side code would take the request parameters sent by the form, read the file, calculate any result and pass it back in the http response to the front end, which could be dealt with by displaying a new page or as part of an AJAX refresh.
As you can appreciate though, this is a very broad topic and there are many possible solutions.
Also as #Dagon points out, if you are not restricted to using a text file (although your title probably suggests you are) then using a MySQL or other such database could be a better solution, maybe even Solr as an indexing solution, depending on what you are searching for.

php web server and HTML [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 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.

What is the correct way of doing JQuery/AJAX and php, file structure? [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
what way should I structure my files when doing Jquery AJAX requests in PHP.
i.e.
showRecords.htm - contains jQuery/AJAX that gets data (HTML) from the php file below.
showRecords.php - contains php that does some processing, likely to get data from DB and echo some HTML.
or
one file:
showRecords.php - contains the JQuery/AJAX and is the php file and requests data from itself.
also you clever people out there, please note I am a beginner in JQuery
and AJAX so please forgive me if its a silly question or trivial.
Obviously not: one file.
I'd do it this way:
showRecords.php - form processing, db requests
templates/showSomeHtml.php - displays everything that your users can see
showRecords.js - jQuery/AJAX that gets data
And if the jQuery/Ajax modifies the HTML, you could also move that (the dynamic HTML) into the templates (read about javascript templates)
I think it's up to you. If you have a quick ajax call you'd like to do and you know it won't be used anywhere but there then I see no issue with having everything in one file. On the other hand, you might need to access that processing script in multiple locations on your site. If that's the case, it's not ideal to place it all in one file. I think two files avoids confusion. Keeps everything separate and neat.

best practice of defining type and format of the requests in ajax calls [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
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.

Categories