R
R
Rassul Nassyrov2020-11-23 08:37:21
Django
Rassul Nassyrov, 2020-11-23 08:37:21

Why is the variable not updated on the front?

<form method = "POST" id = "search"> {% csrf_token %}
        <input name = "sdf" value = "" onkeyup="search(this.value);" onkeypress="search(this.value);" type = "search" style = "width: 100%; height: 35px; background: url(/static/simg/search-icon.png) no-repeat scroll 7px 7px; background-size: 20px 20px; padding-left:30px; color: white; font-size: 18px;" placeholder="Поиск пользавателей">
         <input id = "gosearch" type = "submit" style = "">    
</form>
<p id = "text">{{sn}}</p>


$("#search").submit(function (e) {
            e.preventDefault()
            var form_data = $(this).serialize(); 
            $.ajax({
                type: "POST", 
                url: "", 
                data: form_data,
                success: function () {
                    $.ajax({
                        url: document.URL,
                        cache: false,
                        success: function(html){
                        }
                    });
                }
            });
        });


views.js:

if "sdf" in request.POST:
      sn = request.POST['sdf']

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Fr, 2020-11-23
@nssr02

for example, make changes to the js script

$("#search").submit(function (e) {
            e.preventDefault()
            var form_data = $(this).serialize(); 
            $.ajax({
                type: "POST", 
                url: "", 
                data: form_data,
                success: function () {
                    $.ajax({
                        url: document.URL,
                        cache: false,
//внесите изменения
                        success: dataFromServer(html){
                        }
                    });
                }
            });
        });

//добавьте простую функцию
function dataFromServer(html):
   let html = JSON.parse(html)
   document.querySelector('#text').innerHTML = html

And don't forget to properly implement the backend logic (don't forget request.is_ajax() - but it's considered deprecated)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question