我只是组合一些代码来生成一些随机字符串,从stackoverflow和googling中进行搜索。
如何在不刷新页面的情况下立即获得结果。
当我按下生成按钮时。
以下是我的代码
<?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>
发布于 2018-08-02 23:06:17
<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>
https://stackoverflow.com/questions/51656647
复制相似问题