I'm using a function from this site:
The function works perfectly, when I select an option in a dropdown, the 2nd DropDown already brings me what I want, the only problem is after clicking the submit button that does not bring me the selected fields of these dropdowns, I believe I'm not sure how to use the POST method in javascript. I do not have much knowledge in javascript / ajax. Here's a small part of an example of what I'm trying to do:
Model
class Regiao(models.Model):
marca=models.IntegerField('Marca', null=True, blank=True,)
cor=models.ForeignKey('cor.Cor', verbose_name=u'Cores', null=True, blank=True,)
Form
class RegiaoForm(forms.ModelForm):
class Meta:
model=Regiao
fields=['marca', 'cor']
Views
form = RegiaoForm(request.POST or None)
if form.is_valid():
saveMarca = request.POST['marca']
return redirect('apps.past.views.marcas')
marcas=Marca.objects.all()
return render_to_response('marca.html', {'form':form, 'marcas':marcas}, context_instance=RequestContext(request))
def log_getdetails(request):
selected_marca = ""
marca_name = request.GET['cnt']
result_set = []
all_cores = []
marca_name = marca_name.split()
marca_name = str(marca_name[2])
selected_marca = Marca.objects.get(name=marca_name)
all_cores = selected_marca.cor_set.all()
for cor in all_cores:
result_set.append({'name': cor.name})
return HttpResponse(json.dumps(result_set), mimetype='application/json', content_type='application/json')
Template
JAVASCRIPT
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script><scripttype="text/javascript" src="http://yourjavascript.com/7174319415/script.js"></script><script>$(document).ready(function(){$('select#selectmarcas').change(function(){varoptionSelected=$(this).find("option:selected");
//var valueSelected = $(this.result).find("option:selected");
var marca_name = optionSelected.text();
data = {'cnt' : marca_name};
ajax('/getdetails',data,function(result){
console.log(result);
$("#selectcores option").remove();
$("#selectcores").append('<option>'+'TODOS'+'</option>');
for (var i = result.length - 1; i >= 0; i--) {
$("#selectcores").append('<option>'+ result[i].name +'</option>');
};
});
});
});
</script>
HTML
{% block content %}
<form class="form-horizontal" method="post">
{% csrf_token %}
{% load add_attr %}
<select class="form-control" name="selectmarcas" id="selectmarcas" >
<option value="">TODAS</option>
{% for marca in marcas %}
<option value="{{marca.id }}">{{ marca.name }}</option>
{% endfor %}
</select>
</div>
</div>
<br/>
<div class="row {% if form.cor.errors %}has-error{% endif %}">
<label class="col-md-2 control-label" for="{{form.cor.auto_id }}">
{{form.cor.label }}
</label>
<div>
<select class="form-control" name ="selectcores" id="selectcores">
<option value="">TODAS</option>
</select>
<button type="submit" class="btn btn-primary">Salvar</button>
</form>
{% endblock %}
What I need is that in addition to making a dropdown dependent on the other is to be able to save the information that I selected after giving the submit. I'm sorry if I put a lot of information, but I'm trying to clarify my question.