Scope Issue: Access array in if statement (PHP) : I have an admin panel for an application. For this admin panel, the admin has the option to select a user from a dropdown list. When the user is selected, I use an AJAX call to grab the users data and store it in a variable called **$result**. Now when I try to access **$result** outside of the if statement in my HTML file, it is not recognized. What are the possible ways I can access this variable?



HTML file (AJAX call)

    $(document).ready(function(){

    $('#user').change(function() {
     
        var user_id = $(this).val();
      
        $.ajax({

                url: 'adminNew.php',
                method:'POST',
                data: {user_id : user_id},
                success: function(data) {

                    alert(user_id);
                   
                   
                }
        });
      });
    });



Php file (if statement)

    if (isset($_POST["user_id"])) {
    
    if ($_POST["user_id"] != '') {
    $sql = "SELECT * FROM user_data WHERE user_id ='".$_POST["user_id"]."'";
    }

    $result = mysqli_query($connect, $sql);
   
    } 

HTML file (table)

    <tr>
    <tbody id="expBody">
           <?php
            if ($result != ''){
            while($row1 = mysqli_fetch_array($result)) {
            echo '
                     <tr id = '.$row1["id"].'>
                     <td> '.$row1['user_id'].'</td>
                     <td> '.$row1['name'].'</td>
                     <td> '.$row1['email'].'</td>
                     <td> '.$row1['city'].'</td>    
                     <td> '.$row1['state'].'</td>
                     <td> '.$row1['zip'].'</td>
                     

            ';
            }
          }
           
        ?>
    </tbody>
    </tr>
