Im trying to show an alert if the form submited is valid or not.
In the view, I have a hidden input which change the value if form is valid or not.
I have this JavaScript:
$( document ).ready(function() {
if (document.registroForm.alert.value==1){
swal("¡Bien hecho!", "El registro fue exitoso.", "success")
}
if (document.registroForm.alert.value==0){
swal("¡Oops!", "Algo salió mal.", "error")
}
});
The problem is that I get the alert every time I refresh the page.
And I just wanna do it when the user submit the form and the page is refreshed
Thanks.
What I did, is to set the variable "alert" to None on page load. which changes after form submit: (I modified this on my views.py)
views.py
def registroUsuario(request):
if request.method == "POST":
form = registroForm(request.POST or None)
if form.is_valid():
alert = 1
instance = form.save(commit=False)
instance.is_active = False
instance.save()
else:
alert = 0
else:
alert = None
form = registroForm()
context = {
"titulo": "Registrarse",
"form": form,
"alert": alert,
}
template = "micuenta/registro.html"
return render(request, template, context)
and my .html:
<form method="POST" action=".">
[... some labels and inputs ...]
<input type="hidden" name="alert" value="{{alert}}" readonly>
</form>
<script>
$( document ).ready(function() {
if (document.registroForm.alert.value==1){
swal("¡Bien hecho!", "El registro fue exitoso.", "success")
}
if (document.registroForm.alert.value==0){
swal("¡Oops!", "Algo salió mal.", "error")
}
});
</script>
Thanks by the way.
I've used something like this in the past.
$( document ).ready(function() {
$('#your-form').on('submit', function(e) {
e.preventDefault();
if (document.registroForm.alert.value==1){
swal("¡Bien hecho!", "El registro fue exitoso.", "success");
return true;
} else if (document.registroForm.alert.value==0){
swal("¡Oops!", "Algo salió mal.", "error")
}
});
});
However you accomplish it, I believe you'll need an event listener for the form submission and disable the post back by default. You can then perform your check for a valid submission.
You can use a flag variable to control it. You pass the variabel by the URL.
In your button:
<button type="button" onclick="myFunction(1)">Submit</button>
In the script of this same page:
<script type="text/javascript">
function myFunction(value){
window.location = "page.html?myVariabel="+value;
}
</script>
In the page you wanna check and display the mensseger you can use this:
<script type="text/javascript">
var query = location.search.slice(1);
var part = query.split('=');
if(part[1] == 1){
//Your code
}
</script>
What I did, is to set the variable "alert" to None on page load. which changes after form submit: (I modified this on my views.py)
views.py
def registroUsuario(request):
if request.method == "POST":
form = registroForm(request.POST or None)
if form.is_valid():
alert = 1
instance = form.save(commit=False)
instance.is_active = False
instance.save()
else:
alert = 0
else:
alert = None
form = registroForm()
context = {
"titulo": "Registrarse",
"form": form,
"alert": alert,
}
template = "micuenta/registro.html"
return render(request, template, context)
and my .html:
<form method="POST" action=".">
[... some labels and inputs ...]
<input type="hidden" name="alert" value="{{alert}}" readonly>
</form>
<script>
$( document ).ready(function() {
if (document.registroForm.alert.value==1){
swal("¡Bien hecho!", "El registro fue exitoso.", "success")
}
if (document.registroForm.alert.value==0){
swal("¡Oops!", "Algo salió mal.", "error")
}
});
</script>
Thanks by the way.