Odyssee
Odyssee

Reputation: 2463

Display php output via ajax

I have a php script to check if a domain is available. It echos the result and there are 4 possible outputs;

  1. Is not available
  2. Is available
  3. Is registered, but can be transferred
  4. Is not available for transfer

I am trying to use ajax to send the domain and the tld to the script How do I echo the output of the script in my html? From what I understand is that is is possible to display a message if the php gives a result, but not to extract the result and display that under my html form.

So basically this is what happens at the moment:

form  ->  $domain $tld -> AJAX -> GET -> domaincheck.php -> $dom = $_GET['domain'];
 $tld = $_GET['tld']; -> $domain = $dom . $tld; -> TransIP API -> $result

$result gets set by this piece of php

try
    {
        $availability = Transip_DomainService::checkAvailability($domain);
        switch($availability)
        {
      //check availability
            case Transip_DomainService::AVAILABILITY_INYOURACCOUNT:
                $result = htmlspecialchars($domain)
                            . ' is not available.';
            break;

            case Transip_DomainService::AVAILABILITY_UNAVAILABLE:
                $result = htmlspecialchars($domain)
                            . ' is not available for transfer.';
            break;

            case Transip_DomainService::AVAILABILITY_FREE:
                $result = htmlspecialchars($domain)
                            . ' is available for registration.';
            break;


            case Transip_DomainService::AVAILABILITY_NOTFREE:
                $result = htmlspecialchars($domain)
                            . ' is registered. If you are the owner,
                                    you could transfer it.';
            break;
        }
    }
    catch(SoapFault $e)
    {
    //error
        $result = 'An error occurred: ' . htmlspecialchars($e->getMessage());
    }
}
else
{
    $domain = '';
    $result = '';
}

I am absolutely no programming genius, more an UI designer on the loose. So any help is appreciated :)

Upvotes: 0

Views: 2643

Answers (1)

Logan Bentley
Logan Bentley

Reputation: 274

It is totally possible, and happens all over the web these days.

Basically what you need to do, is have the PHP script output that $result that you want, and then have the AJAX take it and place it into the html dom. Here's an example to get you started:

At the end of the PHP script, add something like:

echo json_encode(['result'=>$result]);
die();

This takes your $result variable and puts it in an array with 'result' as its index. You then encode it in JSON format, print it, and kill the script because it's finished. (note: there are other ways to do it than json encoding, but I find it the simplest and most scalable solution).

Then, have some javascript like this:

$.ajax({
    url: "your_url/with_the_php_script.php",
    type: "GET",
    dataType: "json",
    data: { domain: "yummypizza.com", tld: "the_moon" },
    success: function(data){
        if('results' in data){
            $('#html_element_for_the_results').html(data.results);
        }
        else{
            alert("Uh oh, no results!");
        }
    },
    error: function(data){
        alert("Uh oh! AJAX error!");
    }
})

You'll probably want to save this in a function and trigger it on the form submit or button press, however you're planning for the user to submit the data. Anyways, you have a few components: you have the url you're sending it to, the data you're sending (see how it's an object with attributes for both the domain and tld, the two $_GET variables in your PHP script, so that's the data it's sending). Then, when it's done and successful, it does the function in success.

With jquery, you can use the $(element).html('something here') format to replace the innerHTML content of something on the page. So, put the appropriate ID on wherever you want that $results to get shown and select it there in the success function.

Upvotes: 4

Related Questions