anonymousmofo Posted March 28, 2012 Share Posted March 28, 2012 I am currently building a website which uses postbacks. How can I clear the postback so that it can only be posted once because at the minute when i refresh the page it keeps adding the same entry. Any help would be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/ Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 Do a header redirect after you have processed everything. Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331884 Share on other sites More sharing options...
anonymousmofo Posted March 28, 2012 Author Share Posted March 28, 2012 I have used the header and have redirected it to the same page however it is still inserting it twice into my table. Any Further ideas? Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331890 Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 Post code please. Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331893 Share on other sites More sharing options...
anonymousmofo Posted March 28, 2012 Author Share Posted March 28, 2012 <form name="input" action="NewProduct.php" method="post"> ID: <input type="text" name="ID" /> Product: <input type="text" name="Product" /> Price: <input type="text" name="Price" /> <input type="submit" value="Submit" /> </form> <?php $server = '********'; $connectionInfo = array( "Database"=>"********"); $conn = sqlsrv_connect($server,$connectionInfo); $insert_query = "INSERT INTO Car (Make,Model,Price) VALUES (?,?,?)"; $Make = $_POST['Make']; $Model = $_POST['Model']; $Price = $_POST['Price']; $params = array("$Make","$Model", "$Price" ); $result = sqlsrv_query($conn,$insert_query,$params); $describeQuery="select Make,Model,Price from Cars"; $results = sqlsrv_query($conn, $describeQuery); echo '<table border="1" BORDERCOLOR=Black>'; echo '<tr><th bgcolor = "LightBlue">Make</th><th bgcolor = "LightBlue">Model</th><th bgcolor = "LightBlue">Price</th></tr>'; while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) {   echo '<tr>'; echo '<td >' .$row['Make'].'</td>'; echo '<td >' .$row['Model'].'</td>'; echo '<td >£' .$row['Price'].'</td>'; echo '</tr>'; } echo '</table>'; sqlsrv_close($conn);  This is suppose to let me enter new cars into a table however when i refresh this page it keeps re entering the previously entered car.  Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331897 Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 You are not checking for a POST request, so you are just going to be inserting empty rows every time you load the page. The $_POST superglobal is only going to be populated when you send a POST request. Â So do something like; if (!empty($_POST)) { $insert_query = "INSERT INTO Car (Make,Model,Price) VALUES (?,?,?)"; $Make = $_POST['Make']; $Model = $_POST['Model']; $Price = $_POST['Price']; $params = array("$Make","$Model", "$Price" ); $result = sqlsrv_query($conn,$insert_query,$params); $describeQuery="select Make,Model,Price from Cars"; $results = sqlsrv_query($conn, $describeQuery); } Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331900 Share on other sites More sharing options...
anonymousmofo Posted March 28, 2012 Author Share Posted March 28, 2012 Yes this will solve the problem of entering empty rows however there is still the problem of entering a car and that being entered twice into the table. Eg. when i enter 'ferrari' into the table 'ferrari' is added again when the page is refreshed  Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331904 Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 Are you talking about when you refresh the page and you are asked to send the POST data again by your browser? Â To get around that you'll need to header redirect to the same page. if (!empty($_POST)) { $insert_query = "INSERT INTO Car (Make,Model,Price) VALUES (?,?,?)"; $Make = $_POST['Make']; $Model = $_POST['Model']; $Price = $_POST['Price']; $params = array("$Make","$Model", "$Price" ); $result = sqlsrv_query($conn,$insert_query,$params); $describeQuery="select Make,Model,Price from Cars"; $results = sqlsrv_query($conn, $describeQuery); header('location: yourpage.php'); } Â However this is going to give you a header's already sent error because you have output above it. You'll either need to move your form below this code or use output buffering. Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331909 Share on other sites More sharing options...
anonymousmofo Posted March 28, 2012 Author Share Posted March 28, 2012 Right that seems now to be working however it will not now print out the table on the web page  would i have to change anything where the table is drawn?  Here is the code:  if (!empty($_POST)) { $insert_query = "INSERT INTO Cars (Make,Model,Price) VALUES (?,?,?)"; $Make = $_POST['Make']; $Model = $_POST['Model']; $Price = $_POST['Price']; $params = array("$Make","$Model", "$Price" ); $result = sqlsrv_query($conn,$insert_query,$params); $describeQuery="select Make, Model, Price from Cars"; $results = sqlsrv_query($conn, $describeQuery); header('location: NewCar.php'); } echo '<table border="1" BORDERCOLOR=Black>'; echo '<tr><th bgcolor = "LightBlue">Make</th><th bgcolor = "LightBlue">Model</th><th bgcolor = "LightBlue">Price</th></tr>'; while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC)) {   echo '<tr>'; echo '<td >' .$row['Make'].'</td>'; echo '<td >' .$row['Model'].'</td>'; echo '<td >£' .$row['Price'].'</td>'; echo '</tr>'; } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331919 Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 Oops, went too far with the conditional. if (!empty($_POST)) { $insert_query = "INSERT INTO Car (Make,Model,Price) VALUES (?,?,?)"; $Make = $_POST['Make']; $Model = $_POST['Model']; $Price = $_POST['Price']; $params = array("$Make","$Model", "$Price" ); $result = sqlsrv_query($conn,$insert_query,$params); } Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331924 Share on other sites More sharing options...
anonymousmofo Posted March 28, 2012 Author Share Posted March 28, 2012 I can see what you mean however i am back to square one, The table is now shown however there are duplicate records. When i could not see the table before and i had this code: $describeQuery="select name, Id, Price from Products"; $results = sqlsrv_query($conn, $describeQuery); in the if statement as you originally suggested it seemed to be working fine. When i checked the table it only had one of each entry which is what i want it to have. Â Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331936 Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 I forgot to stick the header redirect back in. Does that help? if (!empty($_POST)) { $insert_query = "INSERT INTO Car (Make,Model,Price) VALUES (?,?,?)"; $Make = $_POST['Make']; $Model = $_POST['Model']; $Price = $_POST['Price']; $params = array("$Make","$Model", "$Price" ); $result = sqlsrv_query($conn,$insert_query,$params); header('location: yourpage.php'); } Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331946 Share on other sites More sharing options...
anonymousmofo Posted March 28, 2012 Author Share Posted March 28, 2012 YES! Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/259871-php-clearing-post/#findComment-1331949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.