#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    PHP + AJAX - Database Connection Test


    Hello everyone! I'm so excited I found these forums, I feel like I have a lot to learn here, but anyways, onwards with the question.

    I'm looking to make a little CMS for myself to help me out a bit in learning, and just ease of use. I'm in the process of making an installation process, and I'm up to the database connection aspect. I want to use AJAX and PHP to get a connection to the specified $_GET variables filled in, and test to make sure we can connect to the database given. How would I go about checking if the connection was successful?

    And how else could I go about doing this other then AJAX if anyone knows?

    Thanks to all who help!

    - Necro
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    The only alternative to AJAX is submitting the form to another page as you would normally do.

    The most effective way to communicate between JavaScript and PHP is using JSON. PHP provides the json_encode function which allows you to easily transfer data from PHP to JavaScript in response to an AJAX request.

    As far as how you would literally go about checking if the connection was successful; you would just use an if statement if you're using mysqli and an exception try/catch block if you're using PDO.

    There are really three parts to something like this.
    1. In JavaScript, gather the relevant form values and send them to a PHP script using AJAX. This code would likely be located in some sort of submit event handler.
    2. In the PHP script, attempt to connect to the database using the submitted values and output a particular value indicating whether the connection was successful or not. This code would use the json_encode function to output the result value in a format that JavaScript can easily understand.
    3. In JavaScript, handle the response from the PHP script and update the page markup as necessary to reflect the result of the connection attempt. This code would be located in the callback function for the AJAX request.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo