Dfret82 Posted May 15, 2007 Share Posted May 15, 2007 Hi there, I am new to this PHP fun stuff and I am muddling my way through a PHP book in an attempt at getting my site more dynamic. The code below represents a mysql query which displays the data to the screen. That seems to work fine. Then I have tried to get a form with a submit button so data can be entered into the field i.e comments etc. I am having a few problems with the code and any help is always welcome. 1. When user hits refresh button on browser the variables are passed into the database again and again. 2. After the submit button is hit the page does not display the data in the list which has just been entered. This only displays when the page is refreshed via the browswer. I assume there is a way to combine a refresh with the submit button. P.S I am a beginner to please try and explain in simple terms! ----------------------------------------------PHP Code Section Below --------------------------------------- <?php echo date ('l, dS F Y.'); $result = @mysql_query('SELECT EMail FROM Critics'); while ($row = mysql_fetch_array($result)) { echo '<p>' . $row ['EMail'] . '</p>'; } ?> <form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <label>Type your comment here: <br /> <textarea name="EMail" rows="5" cols="40"> </textarea></label><br /> <input type="submit" value="SUBMIT" /> </form> <?php if (isset($_POST['EMail'])) { $house = $_POST['EMail']; $sql = "INSERT INTO Critics SET EMail ='$house' "; if (@mysql_query($sql)) { echo '<p>Your House has been added. </p>'; header('location: ' . $SERVER['PHP_SELF']); } else { echo '<p>Error adding submitted joke: ' . mysql_error() . '</p>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51465-php-form-issue-refresh-multiple-entry/ Share on other sites More sharing options...
jitesh Posted May 15, 2007 Share Posted May 15, 2007 a.php ------------------ <?php echo date ('l, dS F Y.'); $result = @mysql_query('SELECT EMail FROM Critics'); while ($row = mysql_fetch_array($result)) { echo '<p>' . $row ['EMail'] . '</p>'; } ?> <form action = "b.php" method="post"> <label>Type your comment here: <textarea name="EMail" rows="5" cols="40"> </textarea></label> <input type="submit" value="SUBMIT" /> </form> ------------------------------- b.php <?php if (isset($_POST['EMail'])) { $house = $_POST['EMail']; $sql = "INSERT INTO Critics SET EMail ='$house' "; if (@mysql_query($sql)) { echo '<p>Your House has been added. </p>'; header('location: ' . $SERVER['PHP_SELF']); } else { echo '<p>Error adding submitted joke: ' . mysql_error() . '</p>'; } } header('location:a.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/51465-php-form-issue-refresh-multiple-entry/#findComment-253413 Share on other sites More sharing options...
Dfret82 Posted May 15, 2007 Author Share Posted May 15, 2007 Thanks for the reply. I have tried it. When entering the text into the form and pressing "Submit" then the page b.php is displayed with the message. I have now adapted it so both a.php and b.php have the same code. But when pressing the submit on a.php and going over to b.php the new entry is still not displayed and the refresh issue still remains. Is there a way to include a refresh of the data? I have the SQL Query at the start of the script not sure if this makes a difference. I would ideally like to have the function working on one page as it will make life easier to manage as this functionality will form a large part of the site. Thanks one and all. Quote Link to comment https://forums.phpfreaks.com/topic/51465-php-form-issue-refresh-multiple-entry/#findComment-253466 Share on other sites More sharing options...
jitesh Posted May 15, 2007 Share Posted May 15, 2007 You need to create a seperate action file in which you have to make some processing like add/edit/delete. and after process you sholuld redirect elsewhere from processing file. By the way post the code you have make for a.php and b.php. Quote Link to comment https://forums.phpfreaks.com/topic/51465-php-form-issue-refresh-multiple-entry/#findComment-253468 Share on other sites More sharing options...
Dfret82 Posted May 15, 2007 Author Share Posted May 15, 2007 As per requested code for both a.php and b.php code. --------------------------------------a.php---------------------------------- <p>Date: <?php echo date ('l, dS F Y.'); $result = @mysql_query('SELECT EMail FROM Critics'); while ($row = mysql_fetch_array($result)) { echo '<p>' . $row ['EMail'] . '</p>'; } ?> <form action = "b.php" method="post"> <label>Type your comment here: <textarea name="EMail" rows="5" cols="40"> </textarea></label><br> <input type="submit" value="SUBMIT" /> </br></form> <?php if (isset($_POST['EMail'])) { $house = $_POST['EMail']; $sql = "INSERT INTO Critics SET EMail ='$house' "; if (@mysql_query($sql)) { echo '<p>Your House has been added. </p>'; header('location: ' . $SERVER['PHP_SELF']); } else { echo '<p>Error adding submitted joke: ' . mysql_error() . '</p>'; } } header('location:b.php'); ?> </p> ---------------------------b.php---------------------------------------- <p>Date: <?php echo date ('l, dS F Y.'); $result = @mysql_query('SELECT EMail FROM Critics'); while ($row = mysql_fetch_array($result)) { echo '<p>' . $row ['EMail'] . '</p>'; } ?> <form action = "a.php" method="post"> <label>Type your comment here: <textarea name="EMail" rows="5" cols="40"> </textarea></label><br> <input type="submit" value="SUBMIT" /> </br></form> <?php if (isset($_POST['EMail'])) { $house = $_POST['EMail']; $sql = "INSERT INTO Critics SET EMail ='$house' "; if (@mysql_query($sql)) { echo '<p>Your House has been added. </p>'; header('location: ' . $SERVER['PHP_SELF']); } else { echo '<p>Error adding submitted joke: ' . mysql_error() . '</p>'; } } header('location:a.php'); ?> </p> Quote Link to comment https://forums.phpfreaks.com/topic/51465-php-form-issue-refresh-multiple-entry/#findComment-253540 Share on other sites More sharing options...
jitesh Posted May 15, 2007 Share Posted May 15, 2007 review attached images [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/51465-php-form-issue-refresh-multiple-entry/#findComment-253542 Share on other sites More sharing options...
Dfret82 Posted May 15, 2007 Author Share Posted May 15, 2007 Ok I have removed the sections as per your images so cheers for those. But it still loads a different pages is there not an option which allows me to do this within one page as this code will form a part of a larger page where people can comment on a movie or concert etc. I would like people to be able to see the comment history if possible. The refresh issue still enters multiple data entries. Is there a function within php to clear the variables to stop them from being passed into the MySQL db upon the refresh of a screen. Quote Link to comment https://forums.phpfreaks.com/topic/51465-php-form-issue-refresh-multiple-entry/#findComment-253562 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.