Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2013
    Location
    Toronto
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Matching user input from form fields to array from data in text files

    I am new to php and I am trying to match user input from fields in a form to an array generated from data stored in text files. I have been stumped for several hours on this and would appreciate any assistance available. Thanks

    Here is the php file (user.php):

    PHP Code:

    $user 
    rtrim(htmlentities($_POST['user']));
    $id rtrim(htmlentities($_POST['id']));
    $userFound_Array = array($user$id);
       
    // code to check to test if form fields have been filled out

        
    else{
            
            
    // Opens the "users.txt" file and assigns the contents to $userArray.
            
    $userArray file("users.txt");
            foreach (
    $usertArray as $value){
                
    $userArray explode ("**"$value);
               
    // print_r($userArray);

                // This does not appear to be working 
                // I also tried if($userArray[0] == $user && userArray[1] == $id)

                
    if (array_key_exists('$userFound_Array'$userArray)){
                    
    $userFound 1;
                    
    print_r($userFound_Array);
                    echo 
    "<div style='color:red;font-size:large'>Your user id and password combination was found</div>";            }
               else{
                    echo 
    "<div style='color:red;font-size:large'>One or more fields you entered is incorrect</div>";
                } 
    Here is what the form element looks like:

    Code:
    <form action="index.php" method="post">
    <?php
    include "user.php";
    ?>
    <br><br>
    Please enter your user name:<br><br><input type= 'Text' value=''  name='user'><br><br>
    Please enter your user ID:<br><br><input type= 'password' value=''  name='id'><br><br>
    <input type='submit' name='submit'  value='submit'>
    </form>
    This is what the $userArray looks like:

    Code:
    (
        [0] => person1
        [1] => userid1
    
    )
    (
        [0] => person2
        [1] => userid2
    
    )
    (
        [0] => person3
        [1] => userid3
    
    )

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    You've got $usertArray in the foreach, shouldn't it be $userArray? And then you overwrite it in the loop.

    Is this correct for users.txt?
    Code:
    person1**userid1
    person2**userid2
    person3**userid3
    Try
    PHP Code:
            $userArray file("users.txt");
            foreach (
    $userArray as $value){

                
    $user_string $user '**' $id;
                if (
    trim($value) == $user_string){
                    
    $userFound 1

  • Users who have thanked schleppel for this post:

    unwiredcoder (05-14-2014)

  • #3
    New Coder
    Join Date
    Dec 2013
    Location
    Toronto
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks schleppel ... This worked perfectly ... I have struggle with this concept for a while now .... this makes it all very clear ... thanks for pointing out the "typo".


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •