0

I have an HTML container like this:

<div class="col-6">
  <div class="card ">
    <div class="card-header py-2">Container Left</div>
    <div id="leftContainer" class="list-group" style="height:225px; overflow-y: scroll">
        {% for item in item_history %}
           <a href="#" data-set="5000" id = "{{forloop.counter}}" class="list-group-item py-0 list-group-item-action">{{ item }}</a>
        {% endfor %}                      
    </div>
  </div>
</div>

<p id = "output"></p>    <!-- For DEBUG output -->

And I have a JS like this of which half is mine. I am able to print the value in the var item_selected back to the HTML page where the <p id="output>" is at.

<script>
    $("#leftContainer > a").click(function(event){
        event.preventDefault();
        $("#leftContainer > a").removeClass("active");
        $(this).addClass("active");

        var leftDataSet = parseInt($(this).attr("data-set"));        
        var item_selected = $(this).text();
        document.getElementById("output").innerHTML = item_selected

        // Found this while googling, supposed to send data back to Python.
        var xmlhttp;
        if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        // xmlhttp.onreadystatechange=function(){ // really not sure what this block does?!
        //     if (xmlhttp.readyState==4 && xmlhttp.status==200){
        //         document.getElementById("output").innerHTML=xmlhttp.responseText;
        //     }
        // }
        xmlhttp.open("GET",item_selected,true);  // just send item_selected
        xmlhttp.send();
    });
</script>

I want to send the selection from the list box/container upon selection, which is currently stored in the JavaScript in the variable item_selected. I want to send this info back to Python which renders this page/template.

Can you help; I know Python 3, suck at JS/JQuery. My Framework used is Django. I render this page from the backend using render method like this:

# myapp/views.py
def render_view_results(self, request, projectid):
    return render(request, 'myapp/index.html',{'item_history':items})

Also, I do not want to send info back from HTML/JS to Python using the GET method suggested here

8
  • did you request successfully hitting server (python). If it then your item_selected value will be in request object. From Javascript you have to send item_selected in request query url. like this htpp:localhost/getItem?item=12 Commented Nov 28, 2018 at 5:35
  • Is there a way to send to the Python server without sending it through a url? Commented Nov 28, 2018 at 6:16
  • You can use post method also to send data using ajax request. Commented Nov 28, 2018 at 6:26
  • How do I do that using ajax request? Commented Nov 28, 2018 at 6:30
  • Read this anwser stackoverflow.com/questions/13465711/… Commented Nov 28, 2018 at 6:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.