Search code examples
phpwordpressgetgetusermedia

How to change the value of a variable using input field?


I have created a wordpress website. I want to extract each user's data using php through his/her id.

I have created this code;

<?php
$userid = /* help me get the below input field's value */
$user_info = get_userdata($userId);
      echo 'Username: ' . $user_info->user_login . "\n";
      echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
      echo 'User ID: ' . $user_info->ID . "\n";
    ?>
<input name="getUser" id="getUser" value=''/>

The user will write the id of the user he wants to extract his data. and then the data will echo back.

The code is working fine but i can't set the value of "getUser" input field to "$userId" variable. Also i want that php to re-execute on input field value change.


Solution

  • The best option is to use an HTML form and update "action_page.php" with your page url

    <?php
    if(isset($_POST['getUser'])){
        $userId = $_POST['getUser'];
    }else{
        $userId = null;
    }
    
    $user_info = get_userdata($userId);
          echo 'Username: ' . $user_info->user_login . "\n";
          echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
          echo 'User ID: ' . $user_info->ID . "\n";
        ?>
    <form action="/action_page.php" method="POST">
       <input name="getUser" id="getUser" value=''/>
      <input type="submit" value="Submit">
    </form>