Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to send data to server with select tag without form
    primarykey
    data
    text
    <p>I have a select tag in my html file. i want when user change his/her selection, my page which have to work with ajax, sends the value of user selection to my views.py function and then i recreate the page with this value. my html code:</p> <pre><code>&lt;div id="response&gt; &lt;select name="page_length_name" id="page_length_id"&gt; &lt;option id="opt1"&gt;10&lt;/option&gt; &lt;option id="opt2"&gt;20&lt;/option&gt; &lt;option id="opt3"&gt;30&lt;/option&gt; &lt;option id="opt4"&gt;40&lt;/option&gt; &lt;/select&gt; //here is a table &lt;/div&gt; </code></pre> <p>(this select tag isn't in form). i want it to be fixed untill user change data. so i think i have to use cookie. i don't know can i use function like this, although select isn't in form? Should it be "POST" method? can post only be used in forms? if yes, so how can i send data(my select tag) to server?</p> <pre><code>$(function() { $('#page_length_id').change(function() { temp = ["page_length_id"].options[selectedIndex].value; $.ajax({ type: "POST", data: temp, url: 'backup/', success: function(data) { alert("success"); $("#response").html(data); } }); }) </code></pre> <p>and also how can i force this select value to be in cookie? is it enough to do it by only this line of code in views.py?:</p> <pre><code>table_length = request.COOKIES.get('table_length') </code></pre> <p>here is my viwes.py:</p> <pre><code>def backup(request): if request.is_ajax(): try: table_length = request.COOKIES.get('table_length') except Exception: table_length = 10 if not page_length: table_length = 10 //here i create the page with xsl and send it to a page to be shown result = style.applyStylesheet(doc) out = style.saveResultToString( result ) ok = mark_safe(out) style.freeStylesheet() doc.freeDoc() result.freeDoc() if request.method == 'POST': return HttpResponse(ok) else: return render_to_response("show.html", { 'str': ok, }, context_instance=RequestContext(request)) </code></pre> <p>thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload