donglankui1263 2017-09-18 11:09
浏览 115
已采纳

提交按钮将值传递给数据库并显示值的出现(laravel)

I'm new at programming, especially at laravel. I've tried a lot and now I try to make laravel project but get stuck at some problems.

I want to make the submit button to pass data to the database and find out the occurences of the value in the radio button. I managed to pass data to the database but I still can't find a solution to find occurences. So, how can I know the occurences when the submit button was clicked?

Here is the blade (question.blade.php)

<table class="table" style="width: 40%">

<form action="storequestion" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

<tr>
    <th>M</th>
    <th>Question 1</th>
    <th>L</th>
</tr>
<tr>
    <td><input type="radio" name="q1" value="S" data-col="1"></td>
    <td>1.Statement</td>
    <td><input type="radio" name="q2" value="S" data-col="1"></td>
</tr>
<tr>
    <td><input type="radio" name="q1" value="I" data-col="2"></td>
    <td>2.Statement</td>
    <td><input type="radio" name="q2" value="I" data-col="2"></td>
</tr>
<tr>
    <td><input type="radio" name="q1" value="B" data-col="3"></td>
    <td>3.Statement</td>
    <td><input type="radio" name="q2" value="D" data-col="3"></td>
</tr>
<tr>
    <td><input type="radio" name="q1" value="C" data-col="4"></td>
    <td>4.Statement</td>
    <td><input type="radio" name="q2" value="C" data-col="4"></td>
</tr>

        <tr>
            <th>M</th>
            <th>Question 2</th>
            <th>L</th>
        </tr>
        <tr>
            <td><input type="radio" name="q3" value="D" data-col="5"></td>
            <td>5.Statement</td>
            <td><input type="radio" name="q4" value="D" data-col="5"></td>
        </tr>
        <tr>
            <td><input type="radio" name="q3" value="C" data-col="6"></td>
            <td>6.Statement</td>
            <td><input type="radio" name="q4" value="C" data-col="6"></td>
        </tr>
        <tr>
            <td><input type="radio" name="q3" value="B" data-col="7"></td>
            <td>7.Statement</td>
            <td><input type="radio" name="q4" value="I" data-col="7"></td>
        </tr>
        <tr>
            <td><input type="radio" name="q3" value="B" data-col="8"></td>
            <td>8.Statement</td>
            <td><input type="radio" name="q4" value="S" data-col="8"></td>
        </tr>

<tr> <td> <br><br> <input type="submit" name="submit" value="submit"> </td></tr>

</form>
</table>

Here is the controller (tanswercontroller.php)

public function storequestion(Request $request)
{
    $tanswer = new tanswer;
    $tanswer->q1 = $request->q1;
    $tanswer->q2 = $request->q2;
    $tanswer->q3 = $request->q3;
    $tanswer->q4 = $request->q4;
    $tanswer->save();
    return 'data saved';
 }

Here is the route (web.php)

route::post('/storequestion', 'tanswercontroller@storequestion');

So, when the submit button clicked it shows the occurence of the value of radio button checked which is a string so then it will give output something like this

//suppose we choose the radio button who has:
//in Question 1
//name="q1" value="S" data-col="1"
//name="q2" value="I" data-col="2"

//In Question 2
//name="q3" value="B" data-col="7"
//name="q4" value="S" data-col="8"

//The result will show
// S = 2 , I = 1 , B = 1

Please help me to solve this problem. If you think using javascript is easier, please let me know. Thanks

  • 写回答

2条回答 默认 最新

  • douzhen1234 2017-09-19 10:38
    关注

    as i understood you can do this like this way.

    first you have to create a route

    route::get('/result', 'tanswercontroller@result'); 
    

    then in tanswercontroller.php

    modify your storequestion() method and add result() method


    tanswercontroller.php

    class tanswercontroller extends Controller
    {
    
        /*
        * for storing data then redirected to result 
        * page with id you can make token for this 
        * if you prefer 
        */ 
        public function storequestion(Request $request)
        {
            // you must validate data like ---
            // $this->validate($request, ['q1' => 'required', ...]);
    
            $tanswer = new tanswer;
            $tanswer->q1 = $request->q1;
            $tanswer->q2 = $request->q2;
            $tanswer->q3 = $request->q3;
            $tanswer->q4 = $request->q4;
            $tanswer->save();
    
            // redirect to result page with id
            return redirect()->url('result/'.$tanswer->id); // added this line
        }
    
    
        public function result($id)
        {
            // pulling data of answer for calulation 
            $tanswer = tanswer::find($id);
    
            // do some calulation here the return data like 
            $result = //yourCalculation;
    
            /*
            * now calculation is stored in result variable then 
            * passing result in view so show result as you wish
            */
            return view('result', ['result' => $result]);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的