[ad_1]
I have 3 .txt files (name.txt, cardnumber.txt, expirydate.txt) and 1 html form with one input (name).
I want to read the user input, check if the input matches any line in name.txt, and then display string in all .txt files with the same line where the input found + an image.
EXAMPLE
name.txt:
John Doe
Jane Doe
cardnumber.txt:
1111 2222 3333 4444
4444 3333 2222 1111
expirydate.txt:
2025-12-31
1999-09-25
user input:
Jane Doe
desired output:
Jane Doe 4444 3333 2222 1111 1999-09-25
Here’s my code:
<html>
<body>
<form method="POST" class="form">
<h2>name: </h2><br>
<input type="text" class="name" name="name" id="name"><br>
<input type="submit" name="submit" id="submit" value="search">
</form>
<?php
//convert files to arrays
$file1 = 'name.txt';
$file2 = 'cardnumber.txt';
$file3 = 'expirydate.txt';
$namefile = file($file1);
$cardnumberfile = file($file2);
$expirydatefile = file($file3);
//put user input to variable
$search = $_POST["name"];
//display element with the same index in array namefile, cardnumberfile, and expirydatefile
//index = index where user input found in namefile
while ($point = current($namefile)){
if($point == $search){
?>
<div class="card">
<img src="https://stackoverflow.com/questions/72441638/images/img.png">
<h2 class="cardname"> <?php echo $namefile[key($namefile)]; ?></h2><br>
<h2 class="cardnumber"><?php echo $cardnumberfile[key($namefile)]; ?> </h2><br>
<h2 class="expirydate"> <?php echo $expirydatefile[key($namefile)]; ?> </h2>
</div>
<?php ;}
else{
echo "No match\n";
} next($namefile);}
?>
</body>
</html>
Even though the user input matches with a line in name.txt, the program still outputs “No match”.
I must work with files, but using database is prohibited. It’s okay to use array.
I don’t know what is wrong with the code. Please help.
Thank you so much.
[ad_2]