lundi 23 mai 2016

Handling multiple inputs in the form

I am trying to build a project like betting system.The view looks like:

    <form method="POST" action="">
    {!! csrf_field() !!}<br>
    Your Name: <input type="name" name="name" id="name" ><br><BR>
    Your Contact No. <input type="contact" name="contact" id="contact" ><br>
        1.Who will be crowned champions this season?.<br>
        Manchester City <br>
        Chelsea <br>
        Manchester United <br>
        Arsenal <br><br><br>

        Bet($) <input type-"text" name="city"> on Manchester City.  <br><BR>
        Bet($) <input type-"text" name="manu" > on Manchester United.<br><BR>
        Bet($) <input type-"text" name="chelsea"> on Chelsea.<br><BR>
        Bet($) <input type-"text" name="arsenal"> on Arsenal.<br><BR>
<input type="submit" name="champions" value="Bet" class="btn btn-default"><br><BR>

While my controller looks not so elegant. It works fine if i input only one value. i.e if i bet on one team. how can i handle this with more than one inputs? i.e. if user bets on more than one team.

    public function now(Request $request)
      {
            $name=$request->get('name');
            $contact=$request->get('contact');
            $champions=$request->get('champions');
            $derby=$request->get('derby');

            if($champions)
            {
            $city=$request->get('city');
            $manu=$request->get('manu');
            $arsenal=$request->get('arsenal');
            $chelsea=$request->get('chelsea');
            if($city)
            {
                    $count=$city*3.5;
                 bet::insert(['name'=>$name,'contact'=>$contact,'bet_on'=>'city', bet_amount=>$city]);
                    return redirect('/bet')
                    ->with ('message'," Hello $name. You bet on Manchester city for Rs.$city  and you will get Rs.$count if Manchester City win.");
            }
            else if($manu)
            {
                $count=$manu*7.3;
  bet::insert(['name'=>$name,'contact'=>$contact,'bet_on'=>'manu', bet_amount=>$manu]);

                return redirect('/bet')
                    ->with ('message'," Hello $name. You bet on Manchester United for Rs.$manu  and you will get Rs.$count if Manchester united win. ");        
            }
            else if($chelsea)
            {
                    $count=$chelsea*5.2;
bet::insert(['name'=>$name,'contact'=>$contact,'bet_on'=>'chelsea', bet_amount=>$chelsea]);

                return redirect('/bet')
                    ->with ('message',"Hello $name. You bet on Chelsea for Rs.$chelsea  and you will get Rs.$count ");      
            }
                    else if($arsenal)
            {
                $count=$arsenal*10;
bet::insert(['name'=>$name,'contact'=>$contact,'bet_on'=>'arsenal', bet_amount=>$arsenal]);

                return redirect('/bet')
                    ->with ('message',"Hello $name. You bet on Arsenal for Rs.$arsenal  and you will get Rs.$count");       
            }
            else
            {
                    return redirect('/bet')
                    ->with ('message','Bet some money on! ');       

            }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1UaBsjj
via IFTTT

Aucun commentaire:

Enregistrer un commentaire