多个表单并提交提交错误表单

时间:2015-07-12 17:01:16

标签: php forms symfony twig

我正在使用带有两个foreach的Twig,每个都为每个居民创建一个表单。一个将驻留设置为活动,另一个将其设置为非活动状态。一个应该发布到urlFor intake.post和另一个urlFor discharge.post。我遇到的问题是,当我提交卸货岗位时,它会张贴到进气口。如果有人能解释为什么这将是伟大的。

{% extends 'Templates/default.php' %}

{% block title %}All Residents{% endblock %}

{% block content %}
<h2>All active Residents</h2>
<div>
{% if residents is empty %}
    <p>No active residents</p> 
{% else %}
    {% for resident in residents if resident.active %}
        <div class="row">
            <form action="{{ urlFor('discharge.post') }}" method="post" autocomplete="off">
                <div class="col-sm-4">
                    {{ resident.resident_id }}
                    {% if resident.getResFullName %}
                        <a href="{{ urlFor('Resident.profile', {resident: resident.id}) }}">({{ resident.getResFullName }})</a>
                    {% endif %}
                </div>

                <div class="col-sm-1">
                    <input type="hidden" name="id" value="{{ resident.id }}">
                    <input type="hidden" name="resident_id" value="{{ resident.resident_id }}">
                    <p><div>
                        <button type="submit" class="btn btn-default" value="discharge">
                            Discharge
                        </button>
                    </div></p>
                </div>
            </form>
        </div>              
    {% endfor %}            
{% endif %} 
</div>
<h2>Residents for intake.</h2>
<div>
{% for resident in residents if not resident.active %}
    <div class="row">
        <form action="{{ urlFor('intake.post') }}" method="post" autocomplete="off">
            <div class="col-sm-4">
                {{ resident.resident_id }}
                {% if resident.getResFullName %}
                    <a href="#">({{ resident.getResFullName }})</a>
                {% endif %}
            </div>

            <div class="col-sm-1">
                <input type="hidden" name="id" value="{{ resident.id }}">
                <p><div>
                    <button type="submit" class="btn btn-default" value="intake">
                        Intake
                    </button>
                </div></p>
            </div>
        </form>
    </div>

{% endfor %}    
</div>
{% endblock %}

如果需要,这是default.php页面

<!DOCTYPE html>
<html lang="en">
<head>
      <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

    <title>Rocmnd | {% block title %}{% endblock %}</title>

<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="css/simple-sidebar.css" rel="stylesheet">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

</head>

<body>


{% include 'templates/partials/navigation.php' %}
<div id="wrapper">

    <!-- Page Content -->
    <div id="page-content-wrapper">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-12">
                    {% include 'templates/partials/messages.php' %}

                    {% block content %}{% endblock %}

                </div>
            </div>
        </div>
    </div>
    <!-- /#page-content-wrapper -->

</div>
<!-- /#wrapper -->

<!-- jQuery -->
<script src="js/jquery.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>

         <!-- Menu Toggle Script -->

<script>
$("#menu-toggle").click(function(e) {
    e.preventDefault();
    $("#wrapper").toggleClass("toggled");
});
</script>


</body>
</html>

两个php文件是摄入量

<?php

$app->get('/Resident/all', function() use($app) {

$app->render('Resident/all.php');

})->name('intake.resident');

$app->post('/Resident/all', function() use($app) {

$request = $app->request;

$ResidentId = $request->post('id');

$resident = $app->resident
    ->where('id', $ResidentId)
    ->where('active', false)
    ->first();

    if (!$resident){
        $app->flash('global', 'There was a problem activating this resident. ');
        $app->response->redirect($app->urlFor('home'));

    } else {
        $resident->activateAccount();

        $app->flash('global', 'Resident is now active.');
        $app->response->redirect($app->urlFor('home'));
    }


 $app->render('Resident/all.php');


})->name('intake.post');

和放电php

<?php

$app->get('/Resident/all', function() use($app) {

$app->render('Resident/all.php');

})->name('discharge.resident');

$app->post('/Resident/all', function() use($app) {

$request = $app->request;

$ResidentId = $request->post('id');
$residentNumber = $request->post('resident_id');

$app->resident->where('id', $ResidentId)->update([          
    "active" => false,
    "discharge_date" => NOW
]);

$app->numbers->where('number', $residentNumber)->update(array(
    "active" => false
));

$app->flash('global', 'Resident has been discharged.');
$app->response->redirect($app->urlFor('home'));

$app->render('Resident/all.php', [

]);

})->name('discharge.post');

1 个答案:

答案 0 :(得分:0)

当同一页面上的两个表单有不同的操作时,他们的提交应该落在相应的操作上。您的Twig特定代码很可能实际上将变量{{urlFor('discharge.post')}}证实为进入URL。我建议您在表单页面呈现后查看它的源代码;并确保这两个动作是正确的。