-1

While clicking text field in the UI I need to display a random password in the text field.

<div class="form-group col-md-2">
    <label>Username</label>
    <input type="text" class="form-control"  placeholder="Please Enter Details " name="username" id="username" value="<?php echo $value['id'] ?>">        
    <!--<input class="form-control" type="text" autocomplete="off" placeholder="Search here" onkeyup="drop_down(this.value);" name="val_text" id="val_text" onclick="drop_down_show('block');" onmouseout="drop_down_show('none');">-->
</div>

<div class="form-group col-md-2">
    <label>Password</label>    
    <input type="text"  class="form-control" onblur="randomPassword()" readonly="" name="password" id="password" value="<?php echo $value['password']?>">                        
</div>
Dessus
  • 2,147
  • 1
  • 14
  • 24
Thasneem
  • 17
  • 3
  • What have you tried so far? What format would you like the password to be in? – Jack Bashford May 02 '19 at 02:45
  • Possible duplicate of [Random password generator in PHP not returning password](https://stackoverflow.com/questions/37359858/random-password-generator-in-php-not-returning-password) –  May 02 '19 at 02:45
  • So I guess somewhere you have a javascript function `randomPassword()`, can you provide its code also? – Zeusarm May 02 '19 at 02:53
  • If all logic happens on the front-end there will be a much better way to generate a password from JavaScript. You can check this solution [Generate random string/characters in JavaScript](https://stackoverflow.com/a/1349426/2392957) – alexey-novikov May 02 '19 at 02:58
  • function randomPassword() { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?"; $length = rand(10, PHP_INT_MAX); alert($length ); $password = substr( str_shuffle(sha1(rand() . time()) . $chars ), 0, $length ); alert(); return $password; } – Thasneem May 02 '19 at 03:10
  • @Thasneem and what `alert()` does? – Zeusarm May 02 '19 at 03:25

2 Answers2

1

firstly generate password and then use in your input

function randomPassword ($length = 8) 
{ 
  $genpassword = ""; 
  $possible = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  $i = 0;  
  while ($i < $length) {  
    $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); 
    if (!strstr($genpassword, $char)) {  
      $genpassword .= $char; 
      $i++; 
    } 
  } 
  return $genpassword; 
} 

<input type="text" class="form-control"  value="<?php echo randomPassword(); ?>">
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
0

The issue is that you are missing ; after your PHP statements in the PHP inline blocks of code.

<?php echo $value['whatever'] ?>

...should become...

<?php echo $value['whatever']; ?>

However, if you want to display the PHP data only when a user selects the input, you want to be using the following code:

onfocus='this.value = <?php echo $value["whatever"]; ?>'

...or...

onclick='this.value = <?php echo $value["whatever"]; ?>'

...or if you only want it to be displayed when the input is empty or has no user input...

onfocus='this.placeholder = <?php echo $value["whatever"]; ?>'
onblur='this.placeholder = ""'
oldboy
  • 5,729
  • 6
  • 38
  • 86