0

i just combine some code to generate some random string, searching from stackoverflow and googling.

How do i get result instantly without page going refresh.

when i press button generate.

here is my code

<?php
function randomString($length = 5) {
    $str = "";
    $characters = array_merge(range('A','Z'), range('0','9'));
    $max = count($characters) - 1;
    for ($i = 0; $i < $length; $i++) {
        $rand = mt_rand(0, $max);
        $str .= $characters[$rand];
    }
    return $str;
}
?>

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>



<?php
$randomprivate = randomString();

if (isset($_POST['submit']))
{
$result = $_POST['firstname']."-".$_POST['lockercode']."-".$randomprivate;
}
?>

<form action="#" method="post">
  First name:<br>
  <input type="text" name="firstname">
  <br>
  Locker ID:<br>
  <input type="text" name="lockercode">
  <br><br>
  <input type="submit" name="submit" value="Generate Secret">
  <br>
  Your Secret Identifier:<br>
  <input type="text" value="<?php if (isset($result)) echo $result ?>" readonly>
  <br><br>
  <button onclick="goBack()">Go Back</button>
</form> 
artasena
  • 51
  • 4

1 Answers1

0
<script>
    function makeid() {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        for (var i = 0; i < 5; i++) 
            text += possible.charAt(Math.floor(Math.random() * possible.length));
        document.getElementById("mytext").value = text;

    }
</script>

<form action="#" method="post">
    First name:<br>
    <input type="text" name="firstname">
    <br>
    Locker ID:<br>
    <input type="text" name="lockercode">
    <br><br>
    <input type="submit" name="submit" value="Generate Secret" onclick="makeid()">
    <br>
    Your Secret Identifier:<br>
    <input id="mytext" type="text" readonly>
    <br><br>
    <button onclick="goBack()">Go Back</button>
</form>
TsV
  • 629
  • 4
  • 7