0
<?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

Edited by mike_2000_17: Fixed formatting

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

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

0

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.

0

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

0

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.

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.