-1

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.

Axmed nuur
  • 117
  • 2
  • 16

3 Answers3

1

If it's form, then you can use $_GET or $_POST to get data from the form (depending on the method).

So I guess your form is a POST form, so you can do something like :

<?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";
    ?>
<input name="getUser" id="getUser" value=''/>

And if you want to execute your php code which get the user info using JS without reloading the page :

You can't run PHP with javascript. JavaScript is a client side technology (runs in the users browser) and PHP is a server side technology

So AJAX is what you are looking for.

Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
1

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>
Arun P
  • 541
  • 4
  • 11
0
if(isset($_POST['getUser'])){
        $userId = $_POST['getUser'];
    }
    <form action="$_SERVER['PHP_SELF']" method="POST">
           <input name="getUser" id="getUser" value=''/>
          <input type="submit" value="Submit">
        </form>