在 api 文本中搜索

发布于 2024-10-28 13:24:07 字数 176 浏览 3 评论 0原文

我正在尝试使用 api,我只是在学习如何使用 php 实际实现 api,希望我能学会合并 jquery。我知道如何通过 mysql 及其数据与 php 创建一个简单的搜索功能,但是有没有办法在 api 中创建搜索?使用 API,有 json/xml 响应,而且它们都是字符串,所以我想知道用户是否能够搜索这些字符串?

谢谢

I'm trying to use an api and I'm just learning how to actually implement an api using php, hopefully I'll learn to incorporate jquery. I know how to create a simple search feature through mysql and its data with php, but is there a way to create search within the api? with API, there's json/xml responses, and they're all strings, so I was wondering if the user was able to search those strings?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2024-11-04 13:24:07

首先你必须通过AJAX将json数据发送到php。像这样:

  var request;
  function runAjax(JSONstring)
  {
    // function returns "AJAX" object, depending on web browser
    // this is not native JS function!
    request = getHTTPObject();
    request.onreadystatechange = sendData;
    request.open("GET", "parser.php?json="+JSONstring, true);
    request.send(null);
  }

  // function is executed when var request state changes
  function sendData()
  {
    // if request object received response
    if(request.readyState == 4)
    {
    // parser.php response
    var JSONtext = request.responseText;
    // convert received string to JavaScript object
    var JSONobject = JSON.parse(JSONtext);

    // notice how variables are used
    var msg = "Number of errors: "+JSONobject.errorsNum+
        "\n- "+JSONobject.error[0]+
        "\n- "+JSONobject.error[1];

    alert(msg);
    }
  }

然后您可以通过调用 $_GET['json'] 变量来调用 javascript 创建的变量。

strstr($_GET['json'] , $whatEverYourSearchingFor);

First you have to send the json data to php via AJAX. Something like this:

  var request;
  function runAjax(JSONstring)
  {
    // function returns "AJAX" object, depending on web browser
    // this is not native JS function!
    request = getHTTPObject();
    request.onreadystatechange = sendData;
    request.open("GET", "parser.php?json="+JSONstring, true);
    request.send(null);
  }

  // function is executed when var request state changes
  function sendData()
  {
    // if request object received response
    if(request.readyState == 4)
    {
    // parser.php response
    var JSONtext = request.responseText;
    // convert received string to JavaScript object
    var JSONobject = JSON.parse(JSONtext);

    // notice how variables are used
    var msg = "Number of errors: "+JSONobject.errorsNum+
        "\n- "+JSONobject.error[0]+
        "\n- "+JSONobject.error[1];

    alert(msg);
    }
  }

You then can call the variable that javascript creates by calling the $_GET['json'] variable.

strstr($_GET['json'] , $whatEverYourSearchingFor);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文