Dany 7elo
Dany 7elo

Reputation: 73

php code to read from text file and compare the user input with the content

i have a php code that read from text file an compare the user input with its content.

the problem is that the system read from the text file but doesn't compare

what is the error and is their another method?

testfile.txt

test admin people bob321 danyjd ajb

code:

<?php

if(isset($_POST["Search"]))
{
    $data= file('testfile.txt');
    $accessData = array();
    foreach($data as $line){
        list($dataFile1) = explode(',', $line);

    }

    $dataInput = isset($_POST['name'])? $_POST['name']:'';

    if(array_key_exists($dataInput, $accessData)){
        echo "text exist";
    }
    else{
        echo "text doesn't exist";
    }
}
?>
<html>
    <head>

    </head>
    <body>
     <form action="test2.php" method="post">
          <p>enter your string <input type ="text"  id = "idName"  name="name" /></p>
          <p><input type ="Submit" name="Search" /></p>
    </form>
    </body>
</html>

Upvotes: 0

Views: 776

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46610

You can do it like this, using array_search(), reducing the check to 1 line :/

<?php
if (isset($_POST["Search"]) && !empty($_POST['name']))
{
    if (array_search($_POST['name'], file('testfile.txt')) !== false){
        echo "text exist";
    }
    else{
        echo "text doesn't exist";
    }
}

Upvotes: 2

Related Questions