【问题标题】:Search data from array in php在php中从数组中搜索数据
【发布时间】:2013-08-09 19:21:50
【问题描述】:

我想从 php 数组中获取数据并将其显示在同一页面上。如何 使用搜索框从 php 数组中导入数据。此代码无法正常工作。 这段代码的错误是什么?

foodstore.js

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
    var xmlHttp;

    if(window.ActiveXObject){
        try{
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e){
              xmlHttp = false;
        }
    } else{
        try{
            xmlHttp = new XMLHttpRequest();
        }catch(e){
              xmlHttp = false;
        }
    }
    if(!xmlHttp)
        alert("cant create that object hoss!");
    else
        return xmlHttp;

}

function process(){
      if(xmlHttp.readyState==0|| xmlHttp.readyState==4){
            food = encodeURIComponent(document.getElementById("userInput").value);
            xmlHttp.open("GET","foodstore.php?food="+food,true);
            xmlHttp.onreadystatechange = handleServerResponse;
            xmlHttp.send(null);
      }else{
         setTimeout('process()',1000);

      }
}

function handleServerResponse(){
    if(xmlHttp.readyState==4){
         if(xmlHttp.status==200){
            xmlResponse = xmlHttp.responseXML;
            xmlDocumentElement = xmlResponse.documentElement;
            message = xmlDocumentElement.firstChild.data;
            document.getElementById("underInput").innerHTML ='<span style="color:blue">'+message+'</span>';
            setTimeout('process',1000);
         }else{
            alert('Something went wrong!');
         }
    }

 }

foodstore.php

<?php
   header('Content-Type: text/xml'); 
   echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';

echo '<response>';
   $food = $_GET['food'];
   $foodArray = array('tuna','bacon','beef','loaf','ham');
   if(in_array($food,$foodArray))
      echo 'We do have '.$food'!';
    elseif($food =='')
      echo 'Enter a food you want to buy';
    else
       echo 'Sorry we don't sell it '.$food'!';       

echo '</response>';

?>

索引.html

<html><head>
<script type="text/javascript" src="foodstore.js"></script>
</head>
<body onload="process()">
<h3>The foods  </h3>
Order your foods:
<input type="text" id="Userinput"></input>
<div id="underInput"></div>
</body>
</html>

如何通过搜索框搜索显示数组数据

【问题讨论】:

  • 在页面加载时调用 ajax 调用,而不是在用户实际输入任何内容时调用。
  • &lt;input type="text" id="Userinput" onchange="process()"&gt;&lt;/input&gt;
  • 嗨克里斯托弗,它不工作。
  • 您需要提供一些错误或更详细的信息。
  • 当我添加“培根”并输入时,什么也没发生。未显示数据。

标签: php arrays search


【解决方案1】:

我已经使用 jquery 更改了代码,它很简单。你可以试试看。

index.html

    <html>
    <head>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script>
            $(function()
            {
                $("#Userinput").keyup(function()
                {
                    process();
                });
                $("#Userinput").keydown(function()
                {
                    process();
                });

                $("#Userinput").focus(function()
                {
                    process();
                });
                $("#Userinput").change(function()
                {
                    process();
                });
            });

            function process() {
                var input_food = $("#Userinput").val();
                $.ajax({
                    type: "GET",
                    url: "foodstore.php",
                    data: {food: input_food},
                    success: function(message)
                    {
                        $("#underInput").html('<span style="color:blue">' + message + '</span>');
                    },
                    error: function()
                    {
                        $("#underInput").html('<span style="color:red">Some error occured</span>');
                    }
                });
            }
        </script>
    </head>
    <body >
        <h3>The foods  </h3>
        Order your foods:
        <input type="text" id="Userinput" ></input>
        <div id="underInput"></div>
    </body>
</html>

foodstore.php

    <?php

    if (!empty($_GET['food']))
    {
        $food = $_GET['food'];
        $foodArray = array('tuna', 'bacon', 'beef', 'loaf', 'ham');
        if (in_array($food, $foodArray))
            echo "We do have " . $food . "!";
        elseif ($food == '')
            echo "Enter a food you want to buy";
        else
            echo "Sorry we don't sell it " . $food . "!";
    }
    else
    {
        echo "Enter a food you want to buy";
    }
?>

如果你知道 jquery,我认为它很简单。而且 php 中有一个简单的错误,你没有转义(不要)中的额外单引号,所以我使用双引号来表示 echo 语句。复制粘贴,告诉你这是不是你想要的。有任何疑问可以问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多