S
S
Sergey Beloventsev2017-06-09 10:30:16
JavaScript
Sergey Beloventsev, 2017-06-09 10:30:16

How to properly discard generated json?

I'm trying to create my extension. It turns out js script form json, and then throw in input type='hidden' Here is the form itself

<form id="formMenu" action="update" method="post">
    <div class="form-group field-menuget-key_setup required">
          <label class="control-label" for="menuget-key_setup">Введите название меню (на английском)</label>
          <input id="menuget-key_setup" class="name" name="MenuGet[key_setup]" value="forHer" aria-required="true" type="text">
</div><div class="form-group field-contentForJson required">
      <input id="contentForJson" class="form-control" name="MenuGet[vaelye]" value="" type="hidden">
</div>
       <a id="secures" class="btn btn-success col-lg-offset-8 col-md-offset-8 col-sm-offset-6" href="#">Сохранить меню</a>

here is the js script
$("#secures").click(function (e) {
        e.preventDefault();
        var menu = {};
        var extra = {};
        $("#menu-to-edit li").each(function (i) {
            if ($(this).data('menu')) {
                var id = $(this).data('id');
                var model = $(this).data('model');
                var alias = $(this).data('alias');
                var depth = parseInt($(this).attr('data-depth'));
                var path = $(this).data('path');
                title = $(this).data('title').toString();
                var key = 'menu' + $(this).attr('data-item');
                var addmenu = {
                    title: title,
                    id: id,
                    model: model,
                    alias: alias,
                    depth: depth,
                    path: path,
                    imgPath: imgPath,
                    imgName: imgName
                };
                menu[key] = addmenu;
            }
           $('#contentForJson').val(JSON.stringify(menu, "", 4));
           $('#formMenu').submit();
        });

but it seems to me that this code smells a bit like shit code (well, I don’t like passing json to value, input). Are there best practices on how to do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-06-09
@qonand

but it seems to me that this code smells a bit like shit code (well, I don’t like passing json to value, input). Are there best practices on how to do this?

frankly, it smells. It's better to intercept the form submission, add the necessary parameters to it and send POST at the JS level

T
tyzberd, 2017-06-09
@tyzberd

send via ajax

$.ajax({
  method: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
  .done(function( msg ) {
    alert( "Data Saved: " + msg );
  });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question