简体   繁体   中英

Multiple Forms and submits submitting to wrong form

I'm using Twig with two foreach each one creates a form for each resident. One to set the resident to active and the other to set them to inactive. One should post to urlFor intake.post and the other to urlFor discharge.post. The problem i am having is that when I submit for the discharge post it posts to the intake post. If anyone could explain why this is that would be great.

{% 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 %}

this is the default.php page if needed

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

the two php files are the intake

<?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');

and the discharge 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');

When two forms on the same page have different actions, their submissions should land on their corresponding actions. It is most likely that your Twig specific code is actually substantiating the variable {{urlFor('discharge.post')}} to the intake URL. I would suggest that you have a look at the source code of your form page after it is rendered; and make sure that the two actions are correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM