您好我想在按钮点击时生成6位数的唯一随机数,我想控制使用选择列表生成数字的方式。我制作控制器,我制作了几个生成数字的方法,但我不知道如何在点击按钮上运行方法。
这是我的控制者:
public function index(){
$numbers = Number::all();
return view('authUser.generatedNumbers', compact('numbers'));
}
public function store(Request $request){
$number = new Number;
$number->number = $this->getGenaratedNumber();
$number->save();
}
public function getGeneratedNumber(){
do{
$rand = $this->generateRandomNumber(6);
}while(!empty(Number::where('number',$rand)->first()));
return $rand;
}
public function generateRandomNumber($length) {
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomNumber = '';
for ($i = 0; $i < $length; $i++) {
$randomNumber .= $characters[rand(0, $charactersLength - 1)];
}
return $randomNumber;
}
这是我的表格:
<form class="form-horizontal" method="POST" action="">
{{ csrf_field() }}
<div class="form-group">
<div class="col-md-6">
<label for="prefix">Prefix</label>
<select name="prefix" class="form-control">
<option value="0888">0888</option>
<option value="0877">0877</option>
<option value="0889">0889</option>
</select>
</div>
<div class="col-md-6">
<label for="count">Count</label>
<select name="count" class="form-control">
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="400">400</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-primary btn-block">
Generate Numbers
</button>
</div>
</div>
</form>