How to search elelments from the array?

<!DOCTYPE HTML>
<html>
    <body> 
    <?php
        $nameErr = "";
        $name = "";
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            if (isset($_POST["name"]) && empty($_POST["name"])) {
                $nameErr = "Name is required";
                $veggies = array("Potato", "Cucumber", "Carrot", "Orange", "Green Beans", "onion");
                $fruits  = array("Apple", "Banana", "orange", "Pineapple", "Grapes", "Watermelon");
                $salad   = array_merge ($veggies, $fruits);
                $Object = $_POST["name"];
                $search = array_filter($salad, function($list) use ($Object){
                    return ( stripos($list, $Object) !== FALSE );
                });
                print_r($search);
            } 
            else{
                $name = test_input($_POST["name"]);
                if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                    $nameErr = "Only letters and white space allowed"; 
                }
            }
        }
        function test_input($data) {
            $data = htmlspecialchars($data);
            return $data;
        }
    ?>
        <p><span>* required field</span></p>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            Name: <input type="text" name="name"><span>* <?php echo $nameErr;?></span><br><br>
            <input type="submit" name="submit" value="Submit">
        </form>
    </body>
</html>

When any letter is entered say "a", "ap" etc in the text box, it should display all the elements which are present in the salad list, and if I enter any special characters like @,/. etc it should show special characters should not be entered and which is working fine, but only issue is I'm unable to fix the issue when any letters like "a", "ap", are entered it should display all the elements which are present in the salad list.

Please can any one help me,

1 answer

  • answered 2018-04-17 04:54 GYaN

    Here is the answer

    <?php
        $nameErr = "";
        if(isset($_POST["name"])){ //Check form submission
            $name = test_input($_POST["name"]);
            if (!preg_match("/^[a-zA-Z ]*$/",$name)) {//Check Validations
                $nameErr = "Only letters and white space allowed"; 
            }
            elseif(empty($_POST["name"]))$nameErr = "Name is required";//Check not empty name
            else { 
                $veggies = array("Potato", "Cucumber", "Carrot", "Orange", "Green Beans", "onion");
                $fruits  = array("Apple", "Banana", "orange", "Pineapple", "Grapes", "Watermelon");
                $salad   = array_merge ($veggies, $fruits);
                //echo "<pre>";print_r($salad);die;
                $Object = $_POST["name"];
                $search = array_filter($salad, function($list) use ($Object){
                    return ( stripos($list, $Object) !== FALSE );
                });
                print_r($search); //displays the result
            } 
        }
        function test_input($data) {
            $data = htmlspecialchars($data);
            return $data;
        }
    ?>
    <!DOCTYPE HTML>
    <html>
        <body>
            <p><span>* required field</span></p>
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                Name: <input type="text" name="name"><span>* <?php echo $nameErr;?></span><br><br>
                <input type="submit" name="submit" value="Submit">
            </form>
        </body>
    </html>
    

  • issue with autoloading modal on page load Rails 5 Tailwindcss

    Hey all I am trying to build a modal that loads 10 seconds after a user lands on my homepage. I have a basic modal example Ive added into my app and have tied to add custom js (my weakness, I'm trying to learn it...). when I inspect the element in chrome I see this error:

    application.self-33e0fcb4f9616be09c26add85386e3d65847a6a251a90987c7cc14257e70ecf6.js?body=1:25 Uncaught TypeError: $(...).modal is not a function
        at application.self-33e0fcb4f9616be09c26add85386e3d65847a6a251a90987c7cc14257e70ecf6.js?body=1:25
    

    The modal is stored in a partial on my homepage. I'm new to modals, but any assistance would be appreciated here!

    My modal code:

    <div id="interestModal" class="animated fadeIn fixed z-50 pin overflow-auto bg-smoke-dark flex">
      <div class="animated fadeInUp fixed shadow-inner max-w-md md:relative pin-b pin-x align-top m-auto justify-end md:justify-center p-8 bg-white md:rounded w-full md:h-auto md:shadow flex flex-col">
        <h2 class="text-4xl text-center font-hairline md:leading-loose text-grey md:mt-8 mb-4">Question!</h2>
        <p class="text-xl leading-normal mb-8 text-center">
          Is this Working???
        </p>
        <div class="inline-flex justify-center">
          <button @click="toggleModal" class="bg-grey-lighter flex-1 border-b-2 md:flex-none border-green ml-2 hover:bg-green-lightest text-grey-darkest font-bold py-4 px-6 rounded">
            Absolutely
          </button>
          <button @click="toggleModal" class="bg-grey-lighter flex-1 md:flex-none border-b-2 border-red ml-2 hover:bg-red-lightest text-grey-darkest font-bold py-4 px-6 rounded">
            Not so much
        </button>
      </div>
        <span id="closeModal" class="absolute pin-t pin-r pt-4 px-4">
            <svg class="h-12 w-12 text-grey hover:text-grey-darkest" role="button" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"><title>Close</title><path d="M14.348 14.849a1.2 1.2 0 0 1-1.697 0L10 11.819l-2.651 3.029a1.2 1.2 0 1 1-1.697-1.697l2.758-3.15-2.759-3.152a1.2 1.2 0 1 1 1.697-1.697L10 8.183l2.651-3.031a1.2 1.2 0 1 1 1.697 1.697l-2.758 3.152 2.758 3.15a1.2 1.2 0 0 1 0 1.698z"/></svg>
        </span>
      </div>
    </div>
    

    My javascript:

    $(window).load(function(){
       setTimeout(function(){
           $('#interestModal').modal('show');
       }, 10000);
    });
    
  • Can someone give me a working code to show the latest tweets of a user in the website

    I am trying to get the latest tweets or feed(may be 2 tweets) from the twitter account of a user. Can anyone help me in that? I need a working code.

  • Input type="search" - not enable in Chrome

    My type = "search" does not enable the 'X' option to clear the imput in Chrome! Any solution?

    ` <div layout="row  text-left" style="float:left; width:100%;">
          <md-input-container flex-offset="15" flex="70">
             <label>Pesquisar...</label> 
               <input type="search" ng-model="$ctrl.pesquisa" ng-disabled="'@ViewBag.EditaConteudo'">
          </md-input-container>
      </div>`