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="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>
My Answer:
This code looks mostly correct, but there are a few things that need to be adjusted.
1. The condition in the if statement should be checking if the user input matches the current element in the array, not the key. So it should be `$point` instead of `$namefile[key($namefile)]`.
2. The `next($namefile)` function should be outside of the if-else statement, so that it moves to the next element in the array regardless of whether there is a match or not.
3. You should also add a check to see if the form has been submitted before trying to access `$_POST["name"]`.
Here is the updated code:
php
if(isset($_POST["submit"])) {
//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(trim($point) == $search){
?>
}
next($namefile);
}
}
?>
This code should now correctly check if the user input matches any line in the `name.txt` file and display the corresponding information from the other files.
© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog | Privacy Policy | Terms & Conditions | Contact Us
Create blog | Privacy Policy | Terms & Conditions | Contact Us
Rate this post
4 of 5 based on 9440 votesComments