Display an alert () after submitting a form

1

I have a page that we will call pagina.php with a form. When doing submit I send the form with <form method="post" action="validar_datos.php"> . Then validar_datos.php checks that the data entered in the form is correct and if it is sent to the database. Independently, whether the data is correct or not, at the end of the check, redirect the user to the page of the form using header('location:pagina.php?r=1') if the data is correct or header('location:pagina.php?r=0') if the data is not correct.

Within pagina.php I have the following script:

<?php
if(isset($_GET['r'] == 1)){
    echo '<div id="message"><p>Nuevo cliente añadido con éxito!</p></div>
    <script>$("#message").delay(1500).fadeOut(200);</script>'; 
}else if($_GET['r'] == 0)){
    echo '<div id="message"><p>Datos incorrectos</p></div>
    <script>$("#message").delay(1500).fadeOut(200);</script>'; 
}else{
}
?>

That is, I show a "success" message if the r index passed by the URL is 1 or an "error" message if the r index is 0.

When the user is, for example, on the pagina.php?r=1 page, if he reloads the page, the "success" message will appear again and so on as many times as the page refreshes. You could even change the value of r of the URL to 0 and you would get the message "error".

My question is: Since I currently use header('location:pagina.php?r=valor') to redirect the user, is there any way to do it by passing the index r and its value completely invisible so that the user could not modify it? And could you make it only show the message once regardless of how many times the page is refreshed?

I hope you explained to me

    
asked by gmarsi 16.06.2017 в 14:38
source

1 answer

2

In your form add

<form action="/pagina.php" method="post">

Inside your form you can have a hidden input

<input type="hidden" name="r" id="r" value="">

And when you click on the form to do submit

<script>
    var r = document.getElementById("r"); 
    //Tus validaciones para saber si los datos son correctos o no....
    r.value = 1 //o 0, dependiendo de tu validacion
    r.element.form.submit();
</script>

Also a very good option is to use ajax

$.post('pagina.php', {r : r});

And the value received as $_POST['r'] in your PHP file

EDITING

Regarding how well you explain how was your problem. In my opinion, it is the flow that has a certain problem. In one file you VALID the fields and in another you send an error or success message. It seems to me that in the validation file you should send the alert message saving you redirecting again.

    
answered by 16.06.2017 / 15:00
source