-2

There is a controller - MainController, it has a save Changes function, to which you need to pass an array from a Javascript file. Whatever I tried, everything does not fit, an empty request is output.

JS:

'use strict';

$(function () {

    $("#saveTableChange").click(function(e){
        var arrayMiss = [];
        $("#Table td").each(function(key){
            var arr= [];
            if($(this).attr('id').includes('TableName')===false) {
                if($(this).text()!==""){
                    arr.push($(this).attr('id'),$(this).text());
                    arrayMiss.push(arr);
                }
            }
        });


        $.ajax({
            type: "POST",
            traditional: true,
            url: "../savechanges",
            data: JSON.stringify(arrayMiss),
        });
    });
} )(jQuery);

Controller:

  /**
     * @Route("/savechanges", name="save_changes")
     * @param Request $request
     */
    public function saveChanges(Request $request)
    {

        if ($request->isXmlHttpRequest()) {
            dump($request->getContent());
        }
        echo json_encode($request);
        die();    

    }

2 Answers2

0

Two files are created with the same names, but with different types. The first one that is created from jquery has an array, but the second one that is created from <form action="{{ path('save_changes') }}" method="POST" id="formTableChange"> does not have this array. After redirection, the file with the array disappears, only the one that was created by redirection remains

twig

`{% extends 'main/base.html.twig' %} {% block body %}

<div class="container-fluid">
    <div class="row no-gutters justify-content-center mt-5">
        <div class="col-12 col-lg-6 col-md-6">
            <h1 class="display-4 text-center">
                {{ thisUser_group }}
            </h1>
        </div>
    </div>
</div>

<div class="container-fluid">
    <div class="row no-gutters justify-content-center mt-5">
            <div id="app">

            <form action="{{ path('save_changes') }}" method="POST" id="formTableChange">
            <table id="Table">
                 <th id="TableNAME" rowspan="2"> NAME</th>
                 <th id="TableMonth" colspan="{{ daysInMonth }}"><a href=""><</a>{{ monthList[monthNow-1] }}<a href="">></a>
                  <tr>

                 {% for i in range(1,daysInMonth) %}
                 <th>{{ i }}</th>
                 {% endfor %}
                                      </tr>
                                             </th>
                 {% for user_group in users_group %}

                   <tr>
                         <td id="TableName_{{ user_group.id }}">
                        {{ user_group.username }}
                        </td>
                         {% for i in range(1,daysInMonth) %}
                         <td contentEditable="true" id="{{ yearAndMonthNow }}-{{ i }}_{{ user_group.id }}"> </td>
                         {% endfor %}
                    </tr>
                {% endfor %}
                </table>
                <button id="saveTableChange">Save</button>
            </form>
    </div>
</div>

{% endblock %} {% block addjs %} {% endblock %}`

0

It's hard to know without seeing you html, but looks like your button in inside a form, and the click is submitting it, as that's the default behavior of buttons. To prevent it just disable the default functionality with .preventDefault().

$("#saveTableChange").click(function(e){
    e.preventDefault();
    var arrayMiss = [];
    // Rest of the code
});
msg
  • 7,863
  • 3
  • 14
  • 33
  • In this case, the redirect does not occur and the selection via `if($(this).attr('id').includes('TableName')===false) { if($(this).text()!==""){ arr.push($(this).attr('id'),$(this).text()); arrayMiss.push(arr); } }` is not triggered – Евгений Иванов Oct 21 '20 at 13:51
  • @ЕвгенийИванов That's... quite strange. Are there any syntax errors in the browser's js console? – msg Oct 21 '20 at 21:37
  • Only this warning `Resource interpreted as Document but transferred with MIME type application/json: "http://journal.loc/savechanges".` – Евгений Иванов Oct 22 '20 at 13:56