weixin_33738578 2015-11-27 17:26 采纳率: 0%
浏览 47

通过ajax显示PHP输出

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 :)

  • 写回答

1条回答 默认 最新

  • ?yb? 2015-11-27 17:42
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮