Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Ajax Sample

simong1993

Gold Coder
Staff Team
Guardian
Hey all,

im trying to get my head around Ajax. Does anyone have a easy sample script that just connects to the database and updates a echo on the page when something changes. I just need something to take apart and see what makes it tick :D thanks all
 
If you are wanting to output JSON it's very easy!
PHP:
<?php
$arrayhere = ["you", "can", "json", "if", "you", "want", "to", "you", "can", "leave", "your", "friends", "behind"];
/*
 $sql = $database->prepare("SELECT * FROM table");
 // execute sql, store result, check num rows, then use json encode of a return arra
*/
exit(json_encode($arrayhere));
?>

You might want to also have a few different return values... like so:
PHP:
$data = array(array("id"=>55, "username"=>"GHOST"), array("id"=>102, "username"=>"malcolm"));
echo json_encode(array("success"=>$data ? true : false, "num_results"=>$data ? count($data) : 0, "data"=>$data, "message"=>$data ? "Fetched data successfully." : "We cannot fetch this information right now."));

I like to use something such as a success boolean (true/false, 1/0, etc)so that I can json_encode a return that has an error or no results:
array("success"=>false, "message"=>"We cannot fetch this information right now.");

When you use AJAX you will do something like this:
JavaScript:
ajax({
    method: "GET",
    url: "fetch.php",
    data: {
        key1: 'hi',
        key2: 5
    },
    success: function(data){ // means AJAX responded
        if(data.success){ } else { } // check if our JSON is reporting error or not
        console.log(data);
        alert(data.message);
    },
    fail: function(data){ // unsuccessful ajax attempt
        console.log(data);
    }
});
This example uses my custom AJAX JS function that I published on Github. Check it out!

Hope this helps! :)
 
Last edited:
i probably should have expanded a bit more lol. So value is 0 as its not done yet in the database, Python does it thing and updates the database so 0 is say 9.99 now. How would i make it so instead of refreshing the page it would pull the new 9.99 from the database and display it :D I think thats Ajax right lol
 
i probably should have expanded a bit more lol. So value is 0 as its not done yet in the database, Python does it thing and updates the database so 0 is say 9.99 now. How would i make it so instead of refreshing the page it would pull the new 9.99 from the database and display it :D I think thats Ajax right lol
Well, AJAX is a part of JavaScript specifically. PHP, Python, or other files just echo / print out information on the page. The JS will fetch the output / results of the file. json encode in PHP for example makes it so the PHP array is compatible with JS.

If you want to fetch a new value of something...
JavaScript:
setInterval(function(){
    // ajax goes here, it will call on a script that returns data or a value
},60000) // repeat every 60 seconds

Then let's imagine you fetch the value from the database in PHP and echo it out:
PHP:
$value = 9.99;
echo $value;

Your AJAX success would be like this, if you're only echoing out the value of it.
JavaScript:
success: function(returndata){
    alert("The new value is " + returndata);
}
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom