使用AJAX或JS获取域检查的结果,而不是刷新页面

时间:2015-07-26 03:39:10

标签: javascript php jquery ajax

我有来自托管服务提供商的API脚本,并且使用此.php脚本我只能检查域名是否可用。但是现在,如果我检查域名,页面将刷新,然后它将显示结果。

但是我希望结果能够在没有刷新的情况下显示出来。那可能吗?有人可以为我提供一个好的解决方案吗?

以下是结果部分:

<center> <font color="#FFF "> <?=$result?> </font> </center>`

这是整个.php脚本:

<?php

require_once('Transip/DomainService.php');

if(isset($_GET['domain']) && strlen($_GET['domain']) > 0) {
    $domain = $_GET['domain'];

    try {
        // Request the availability of a domain by using the Transip_DomainService API;
        // we can get the following different statusses back with different meanings.
        $availability = Transip_DomainService::checkAvailability($domain);
        switch($availability) {
            case Transip_DomainService::AVAILABILITY_INYOURACCOUNT:
                $result = htmlspecialchars($domain) 
                            . ' is helaas al <font color="red">bezet</font>, verhuizen is niet mogelijk  ';
            break;

            case Transip_DomainService::AVAILABILITY_UNAVAILABLE:
                $result = htmlspecialchars($domain) 
                            . ' is ge-lockt, verhuizen is niet mogelijk!';
            break;

            case Transip_DomainService::AVAILABILITY_FREE:
                $result = htmlspecialchars($domain) 
                            . ' is <font color="green">beschikbaar!</font> <br><a href="http://xcoder.eu/coming/login.html" target="_blank">Klik hier</a> en registreer direct je domeinnaam!  ';
            break;


            case Transip_DomainService::AVAILABILITY_NOTFREE:
                $result = htmlspecialchars($domain) 
                            . ' is helaas al <font color="red">bezet</font>, verhuizen is mogelijk  ';
            break;
        }
    }
    catch(SoapFault $e) {
        // It is possible that an error occurs when connecting to the TransIP Soap API,
        // those errors will be thrown as a SoapFault exception.
        $result = 'Oeps... Vul een geldige domeinnaam in. ';
    }
}
else {
    $domain = '';
    $result = '';
}
?>
<head>
<link rel="stylesheet" href="assets/css/main.css">
</head>
<body style="background:transparent">
<form name="domainChecker"  id="notifyMe"  class="news-form" style="">
    <div class="form-group">
        <div class="form-inline">
            <input  type="text" style="text-align:center;" name="domain" value="<?=htmlspecialchars($domain)?>" required type="text" id="mail-sub" placeholder="Check of jouw domeinnaam beschikbaar is!" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Check of jouw domeinnaam beschikbaar is!'" class="form-control email srequiredField"  required >
            <button class="btn btn-lg submit" style="background:#ffc000;border:#eeb300;color:black" type="submit">Controleer</button>
        </div>
    </div>
    <center><font color="#FFF "><?=$result?></font></center>
</form>
</body>

2 个答案:

答案 0 :(得分:1)

是的,这是可能的。如果你愿意,可以使用jQuery ......

使用您的Main.php UI文件添加一些调用例如:getDomainAvail.php的jQuery。

所以

  1. 创建getDomainAvail.php文件
  2. 新的getDomainAvail.php文件中的
  3. 复制了为域检查提供的所有代码,并使用生成的查找的打印状态对其进行修改。
  4. 然后返回Main.php,当你的jquery ajax调用返回时,结果/响应只是将它分配给Main.php页面中的result元素。
  5. 需要更多的例子吗?如果你愿意,我可以修改给你的代码。

    干杯

答案 1 :(得分:1)

我确信@Chad Collins会有一些有用的东西,但我正在编写这个快速的脚本,所以我想即使你已经接受了他的答案,我也会发布它。你可以试试......或者不是(我没试过,所以如果它不起作用,我会删除我的答案)

<强>的index.php

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="assets/css/main.css">
</head>
<body style="background:transparent">
<form name="domainChecker"  id="notifyMe"  class="news-form" style="">
    <div class="form-group">
        <div class="form-inline">
            <input type="text" style="text-align:center;" name="domain" value="" required type="text" id="mail-sub" placeholder="Check of jouw domeinnaam beschikbaar is!" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Check of jouw domeinnaam beschikbaar is!'" class="form-control email srequiredField"  required >
            <button class="btn btn-lg submit" style="background:#ffc000;border:#eeb300;color:black" type="submit">Controleer</button>
        </div>
    </div>
    <center><font color="#FFF " id="resp"></font></center>
</form>
<script>
    $("form").submit(function() {
        $.ajax({
                url: 'checkdomain.php',
                data: $(this).serialize(),
                type: 'post',
                success: function(response) {
                    var DataResp    =   JSON.parse(response);

                    // Access the domain with-> DataResp.domain
                    // Access the result with-> DataResp.result

                    if(DataResp.result)
                        $("#resp").html(DataResp.result);
                    else
                        $("#resp").html("Empty message here.");
                }
        });

        return false;
    });
</script>
</body>

<强> checkdomain.php

<?php
function CheckDomain($domain = false)
    {
        $domain = trim($domain);

        if(empty($domain))
            return array("domain"=>false,"result"=>false);

        require_once('Transip/DomainService.php');

            try {
                // Request the availability of a domain by using the Transip_DomainService API;
                // we can get the following different statusses back with different meanings.
                $availability = Transip_DomainService::checkAvailability($domain);
                switch($availability) {
                    case Transip_DomainService::AVAILABILITY_INYOURACCOUNT:
                        $result = htmlspecialchars($domain).' is helaas al <font color="red">bezet</font>, verhuizen is niet mogelijk  ';
                    break;

                    case Transip_DomainService::AVAILABILITY_UNAVAILABLE:
                        $result = htmlspecialchars($domain).' is ge-lockt, verhuizen is niet mogelijk!';
                    break;

                    case Transip_DomainService::AVAILABILITY_FREE:
                        $result = htmlspecialchars($domain).' is <font color="green">beschikbaar!</font> <br><a href="http://xcoder.eu/coming/login.html" target="_blank">Klik hier</a> en registreer direct je domeinnaam!  ';
                    break;


                    case Transip_DomainService::AVAILABILITY_NOTFREE:
                        $result = htmlspecialchars($domain).' is helaas al <font color="red">bezet</font>, verhuizen is mogelijk  ';
                    break;
                }
            }
            catch(SoapFault $e) {
                // It is possible that an error occurs when connecting to the TransIP Soap API,
                // those errors will be thrown as a SoapFault exception.
                $result = 'Oeps... Vul een geldige domeinnaam in. ';
            }

        // Sorry, my original post didn't return anything here! Whoops!
        $response['domain'] =   (!empty($domain))? $domain: "";
        $response['result'] =   (!empty($result))? $result:"";

        return $response;
    }

    echo json_encode(CheckDomain((isset($_POST['domain']))? $_POST['domain']:""));
?>