Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

654
Views
Display JavaScript alert after form submit - Django

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.

The problem was solved.

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.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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>
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!