samedi 23 septembre 2017

Generate random number onclick and count generated numbers?

Hi i want go generate 6 digits unique random numbers on button click, and i want to controll how numbers was generate using select list. I make controller and i make few methods that generate the numbers but i don't know how to run methods on button click.

This is my controller:

 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;
}

and this is my form:

 <form class="form-horizontal" method="POST" action="">
                            

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire