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

Javascript 使用AJAX或JS获取域检查的结果,而不是刷新页面,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个来自主机提供商的API脚本,通过这个.php脚本,我只能检查域名是否可用。但是现在如果我检查一个域名,页面将刷新,然后显示结果 但我希望结果显示时不刷新。可能吗?有人能给我一个好的解决方案吗 以下是结果部分: <center> <font color="#FFF "> <?=$result?> </font> </center>` <?php require_once('Transip/DomainService.ph

我有一个来自主机提供商的API脚本,通过这个
.php
脚本,我只能检查域名是否可用。但是现在如果我检查一个域名,页面将刷新,然后显示结果

但我希望结果显示时不刷新。可能吗?有人能给我一个好的解决方案吗

以下是结果部分:

<center> <font color="#FFF "> <?=$result?> </font> </center>`
<?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>

是的,这是可能的。如果您喜欢,请使用jQuery

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

所以

  • 创建getDomainAvail.php文件
  • 在新的getDomainAvail.php文件中,复制为域检查提供的所有代码,并使用结果查找的打印说明对其进行修改
  • 然后回到Main.php,当jquery ajax调用返回结果/响应时,只需将其分配给Main.php页面中的result元素 需要更多的例子吗?如果你愿意,我可能会修改给你的代码


    我肯定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>
    
    <?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']:""));
    ?>
    
    
    控制者
    $(“表格”)。提交(函数(){
    $.ajax({
    url:'checkdomain.php',
    数据:$(this).serialize(),
    键入:“post”,
    成功:功能(响应){
    var DataResp=JSON.parse(响应);
    //使用->DataResp.domain访问域
    //使用->DataResp.result访问结果
    if(数据响应结果)
    $(“#resp”).html(数据响应结果);
    其他的
    $(“#resp”).html(“此处为空消息”);
    }
    });
    返回false;
    });
    
    checkdomain.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>
    
    <?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']:""));
    ?>
    

    嗨,查德,谢谢你回复男人们!如果您能为我修改代码,我将不胜感激,这样我就可以在不刷新的情况下得到响应!如果我能为你做点什么,请告诉我!GreeetzThanks@Xcoder!拉斯克拉特在下面的回答非常好,非常接近我所发布的内容。祝你好运,当你完成后会是一个很酷的用户体验/嘿,Chad,Rasclatts的例子对我不起作用。阅读另一条评论Sok,我给你发了电子邮件。他的剧本和我看到的非常接近。兄弟,非常感谢,我还没有试过你的剧本,因为拉斯克莱特的剧本很好用,我想!!!!您好,Xcoder,您是否可以发布这个文件Transip/DomainService.php,否则我可能会制作一些较小的php片段来返回响应,并在Main.php中使用html。使用jquery并检索结果。当然可以,您可以发邮件到x@xcoder.eu然后我会回复你我的投票@Rasclatt伟大的快速例子。。。我要添加的一点是prevent.default jquery处理程序,这样页面就不会提交/重新加载$(“#notifyMe”).submit(函数(事件){..//Stop form from submitting normaly etc.event.preventDefault();嗯,
    返回false;
    将具有与
    阻止.default
    相同的效果。我通常在使用Ajax时返回false,但我知道
    阻止。默认
    可能是首选方法。你确定吗?在过去处理提交按钮和表单时,这在我的每个浏览器中都不起作用。这是我的习惯经历40%的时间,返回错误;每次都有效。{主持人开玩笑说这么多}可能不会太好。:)嗯,我必须检查一下。在我做过的任何一个网站上,我从来没有人抱怨过返回false,而且我做了很多,所以这是一个有趣的点,我必须做进一步的研究!!可能有一个原因
    阻止。默认值
    是首选方法!这可能是一个已经不存在的遗留问题。。1.以后我得再试一次1.我的意思是。。