在php中搜索数组中的数据

时间:2013-08-09 19:21:50

标签: php arrays search

我想从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>';

?>

的index.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>

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

1 个答案:

答案 0 :(得分: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语句。复制粘贴并告诉你这是不是你想要的。不管怎样怀疑。