-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.

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, i forgot about the get request.
can you tell me how i can use ajax when requesting the above data. Because it reloads when extracting data which is inconvenient.
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.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.