A
A
Andrey_872021-08-11 15:48:28
AJAX
Andrey_87, 2021-08-11 15:48:28

Why is the form not reloading after submitting a post request via ajax?

There is a form (loaded into a tab using the load jquery method)

<button type="submit" form="form-filtergroup" data-toggle="tooltip" title="" class="btn btn-primary"><i class="fa fa-save"></i></button>

<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-filtergroup" class="form-horizontal">
  <div class="form-group required">
  <label class="col-sm-2 control-label" for="input-name"><?php echo $entry_name; ?></label>
    <div class="col-sm-10">
      <input type="text" name="<?php echo $name ?>" value="" placeholder="<?php echo $name ?>" id="input-name" class="form-control" />
    </div>
  </div>
  <div class="form-group required">
  <label class="col-sm-2 control-label" for="input-keyword"><?php echo $entry_keyword; ?></label>
    <div class="col-sm-10">
      <input type="text" name="<?php echo $keyword ?>" value="" placeholder="<?php echo $keyword ?>" id="input-keyword" class="form-control" />
    </div>
  </div>
</form>


Ajax request to save button

<script>
$("#form-filtergroup").submit(function(e) {
    e.preventDefault();
  $.ajax({
    url:"index.php?route=catalog/SMfilter_opt/add&token=<?php echo $token; ?>",
    type:"post",
    data:{filter_group_id: 0},
    dataType:"json",
    success:function(a){
      console.log('111');
    },
    error: function (e) {
    }
  })
  });
</script>


After clicking "save" the request is submitted, but the form is not reloaded.
I need the form to reload and show errors on required fields, the logic of which is written in php. But that doesn't happen. Although if you load the same form without ajax, then everything works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
notlive, 2021-08-12
@notlive

All right. First you need to at least read what Ajax is and how it works.
Wikipedia ...
AJAX, Ajax (ˈeɪdʒæks, from English Asynchronous Javascript and XML - “asynchronous JavaScript and XML”) is an approach to building interactive user interfaces for web applications, which consists in the “background” exchange of data between the browser and the web server. As a result, when data is updated, the web page does not completely reload, and web applications become faster and more convenient.
There are 3 ways out of your situation, either you use only php, or you use ajax + php, but you implement the check for filling in the fields on the user side using javascripts, or you implement the check for sending data in php, but return an error code, javascripts in your the queue outputs a message about filling in the fields based on the error code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question