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

标签 javascript php laravel submit-button find-occurrences

我是编程新手,尤其是 Laravel。我已经尝试了很多,现在我尝试制作 laravel 项目,但遇到了一些问题。

我想让提交按钮将数据传递到数据库并找出单选按钮中出现的值。我设法将数据传递到数据库,但我仍然找不到找到事件的解决方案。那么,我如何才能知道单击提交按钮时发生了什么?

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

这是 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';
 }

这是路由(web.php)

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

所以,当提交按钮被点击时,它显示单选按钮的值被选中,它是一个字符串,所以它会给出类似这样的输出

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

请帮我解决这个问题。如果您认为使用 JavaScript 更容易,请告诉我。谢谢

最佳答案

据我了解,您可以这样做。

首先你必须创建一个路由

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

然后在 tanswercontroller.php

修改您的storequestion() 方法并添加result() 方法


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

关于javascript - 提交按钮将值传递给数据库并显示值的出现(laravel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46277881/

相关文章:

使用 api token 身份验证的 laravel phpunit 测试

javascript - kineticjs自定义命中区域不起作用

javascript - 使用 angularjs : Input textbox gives TypeError: Cannot set property '0' of undefined 在 restful API 上通过 POST 提交 webform

php - 捕获最大执行时间php

php - 未找到类 'App\Providers\AppServiceProvider'

php - Laravel 更新多对多外键记录

javascript - 单击时设置div的背景色

javascript - 水平滚动自动对焦

php - 试图在表格中获得彩色条

php - 如何将json从php传递到javascript以进行jqPlot?