<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/session.php"); ?>
<?php find_selected_item(); ?>
<?php include("includes/header.php"); ?>

<?php
if (isset($_POST['submit'])) {
$top_up_cash = mysql_prep($_POST['top_up_cash']);
$user_id = $_SESSION['user_id'];
            $result = mysql_query("SELECT * FROM users WHERE id='$user_id'");

            while($row = mysql_fetch_array($result))
            {

            $bidot_cash = $row['bidot_cash'];

             }
$new_bidot_cash = $top_up_cash + $bidot_cash;
$query= "UPDATE users SET bidot_cash = '{$new_bidot_cash}' WHERE id='$user_id'";
mysql_query($query);
$message = "your top-up is complete, " . "<a href=\"userinfo.php\">" . " click here" . "</a> to check\n";

}
?>

<table id="structure">
    <tr>
        <div class="menu">
            <ul>

                <?php echo public_navigation($sel_cat, $sel_item); ?>

                <br />
            </ul>
        </div>


        </div>
        <td id="item">
            <?php if ($sel_item) { ?>
                <h2><?php echo htmlentities($sel_item['item_name']); ?></h2>
                <div class="page-content">


                </div>
            <?php } else { ?>
                <h2>Top-up Bidot Cash</h2>
                                <?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
                                key in the amount you want to top-up:<br />
                <form method="post" action="topup.php">
                    RM :<input type="text" name="top_up_cash" /><br />
                    <input type="submit" name="submit" value="Confirm" />
                    </form>

            <?php } ?>
        </td>
    </tr>
</table>
<?php include("includes/footer.php"); ?>

hi all,
as the code above,
i obtain a value let call it A from user then add the value A with another value from database let call it value B then store it as value C and update the C into database.
im now facing a problem which is everytime when i refresh my browser, the code run again and auto submit the value A and update into database again.
For example, C=input value+database value, then update database value =C,
when i refresh the browser, it run again the code and become C=A+ (C in database)
may i know how can i clear the content of A after a complete $_POST is done?
let me know if my question not clear enough,thanks for help

Member Avatar for diafol

Never post to the same page. ALways use a formhandler page which then redirects to the form or not, depending on the data.

Agreed.
This can simply be achieved by a header redirect. For example:

<?php //line 1
if (isset($_POST) && !empty($_POST)) {
header('Location: http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
}

Then you can use the exit function after you have processed the $_POST code.

Ardav, can you elaborate on why they should use a formhandler page? I have seen you say this twice now, any reason why?

Member Avatar for diafol

The reload/refresh problem - submit twice - it's passive as opposed to deliberate.

Ok, makes sense. Won't work well on large forms, but I see your point.

Member Avatar for diafol

Yeah, I agree, a pain for large forms, however, you can put all the $_POST vars into a $_SESSION or something if you need to return and replace the values on unsuccessful validation.

I find it useful when doing ajax calls (post vars server-validated in ajax php file) and when using template engines, where html and php need to be kept separate.

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.