0

hi i would like to know how i can stop multiple form insertion. What i mean is after you submit a form if you refresh the browser sends the script again. I done this with the header() function to resend the user but i also need to show a message saying the data was inserted successfully. I used a session but i don't know how to unset it correctly so when the user refreshes the message goes away. If anyone knows another way or a way i can remove the session after the message has been shown please let me know, thanks.

4
Contributors
6
Replies
21
Views
7 Years
Discussion Span
Last Post by diafol
0

You *shouldn't* send data to the same page. We all (well I do) do it when prototyping, but your data should be sent to form handling page, where it will be processed. Depending on the result of said processing a redirect is then initiated taking you to a specific page and/or providing a message.

You have the choice of how to propagate your errors - $_GET or $_SESSION. I tend to use session variables which I then immediately unset once they are used (echoed).

An error array is useful or a list of error constants, which are linked to error messages or values.

e.g.

$custom_errors[0] = "This data could not be added to the database at this time. Try again later."; 
$custom_errors[1] = "The data was added to the database."; 
(etc).

if(isset($_SESSION['custom_err'])){
$err = $_SESSION['custom_err']);  
echo $custom_errors[$err];
unset($_SESSION['custom_err']);
}
0

I am having a problem with the unset function. The session i use to echo out a error gets unset before the echo runs. In other words the page does not echo out the session, it goes straight into unsetting it which leaves me with no messages showing.

0

place session_start() at the very start of every page.

0

You have to put unset() function after ending your if statement..like

if(isset($_SESSION['s'])){
    ..show message
    }
    unset($_SESSION['s']);

So in your example put your code like this

<code>
$custom_errors[0] = "This data could not be added to the database at this time. Try again later.";
    $custom_errors[1] = "The data was added to the database.";
    (etc).
    if(isset($_SESSION['custom_err'])){
    $err = $_SESSION['custom_err']);
    echo $custom_errors[$err];  
    }
    unset($_SESSION['custom_err']);

</code>

0

sorry David,
the OP wont return to look at the answers after three years.
The code samples will be useful to others who get referred to DaniWeb from Google or other code sites because the answers work,
its not a total loss

Oh, Hi, Welcome to DaniWeb :)

Edited by almostbob

0

I think he found a way after 3 years :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.