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 8 years ago.
Improve this question
i am trying to clean up my code..
is there a way to run this code with Ajax or jquery rather than placing the code in the html page itself..thanks in advance
<?php $acct = new acct; echo $acct->accountProfile("accountphone"); ?>
You can have js execute an ajax function (via $.post) on a load event like $( document ).ready(function() {//code}, using something like $("#div").html(data); to populate data into a container.
That said, I don't think that's necessarily more organized than putting it into your HTML code itself. You don't want to process LOGIC too much in your view, but the nature of web development is that it is "messy" (IMO) and that you have to often just insert PHP all over the place, to do singular functions/echos as you seem to be doing.
Yes, you could put all your PHP code in a separated PHP file, eg.
PrintAccount.php
$acct = new acct;
echo $acct->accountProfile("accountphone");
And at your HTML , via AJAX (I'm using ajax with jquery in this example) you "print" php file response in your html
index.html
<div id="accountphone"></div>
<script>
$.ajax({
type: "POST",
url: "PrintAccount.php",
success: function(data){
$("#accountphone").html(data);
}
});
</script>
Now, my personal opinion: This is good? depends, if you have a lot of PHP code is actually good doing this, you could improve it as real services like REST, it'll be awesome for clean code at HTML files. You have also another option, using MVC, so most of code will be written at Controller, and just a few at View. Now, about your given example, with just a small amount of PHP code this is totally unnecessary, you are complicating too much a simple task.
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 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 9 years ago.
Improve this question
Sorry I'm a newbie in web programming. I have a problem to send array from Javascript / JS array to my function in controller and load new view.
I try to use ajax and I can read that array but ajax not load new view, how to do that?
Please tell me the correct way to send javascript array to controller and load new view.
Please help.
Here's my javascript:
$.ajax({
type: "POST",
url: "<?= base_url() ?>index.php/test/coba",
data: {
test: "test"
}
});
and my controller test.php
function coba() {
$data['test'] = $this->input->post('test');
$this->load->view('newview',$data);
}
Please help
AJAX is meant to send or receive data from the client side to the server side. You can't display a view by using an AJAX method, what you could do, however, is receive values that could then be displayed by using Javascript code.
But AJAX is mainly used to give the client the possibility to interact with the database, which doesn't seem to be what you are trying to do, are you sure you need to use AJAX?
If you need to display something depending on user behaviour, without requiring database calls, then Javascript should be all you need. You could add the HTML code with PHP, and hide it, and when the user does the specific action, you can fill what needs to be filled and display your content.
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.
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 5 years ago.
Improve this question
I want to create a dashboard for a bespoke web service we have so that we can monitor usage on a real-time basis. Something similar to the couchbase dashboard if you have seen that. If not, its basically a line graph that scrolls right to left with time along the bottom of the graph and usage on the y-axis.
To minimise data bandwidth I am assuming there is a way to pass just the changed data to update a graph rather than sending a new image/vector each time. And this would get drawn with a javascript charting library.
What I am unclear on is how to get the data from the server. Is the normal approach to have the javascript code poll the server using AJAX every second (if you are after second by second updates) or is some other method used?
I used PHP for the back end and HTML/JS for the client end.
And does anyone have any examples or links that could be helpful.
For getting the data from the backend, I think you can call it using Ajax, reload each second using setInterval(), making something looks like:
setInterval(function() {
$.ajax({
url: url of your backend server,
type: 'GET',
success: function (data) {
//data returned from the server, use it to draw your chart
}
});
}, 1000); // recall ajax every second
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
Is it possible to create a auction site in PHP using AJAX to refresh the page once a user has entered a higher amount of money.
I can handle the PHP side of it okay but I was wondering if AJAX can really be used so that it refreshes often without putting a lot of strain on server resources?
I also plan to use JQuery to implement AJAX as this makes the job a whole lot easier. Anybody have any code examples that you think could be used?
Any help would be appreciated/
Thanks!
var currentHighestBid=0;
setTimeOut(getHighestBid,5000); //5000ms wait before polling for a better price
function getHighestBid()
{
$.ajax(
url: url, // ur php end point,
type: "GET"
data: {} //json data if you want to send anything as a querystring parameter to your servre
dataType:"json"
success: function(response)
{
if(response.currentMaxBid>currentHighestBid)
{
currentHighestBid=response.currentMaxBid;
//code to update your markup
}
}
});
Hope that made sense..
Read abt json in php
Polling would work as "zzzz" mentioned. Comet (Push based instead of Polling) would be a nicer/better solution for this use case. However PHP is not really good at this with high traffic sites. Node.JS with Socket.IO would be a good solution for you :)