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.

Member Avatar for diafol

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']);
}

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.

Member Avatar for diafol

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

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>

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 :)

Member Avatar for diafol

I think he found a way after 3 years :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.