0

I am making a calendar web app and when I click on a date a form pops up. If I use $_SERVER["REQUEST_METHOD"] == "POST" or isset($_POST['submit']) to check whether the form is submitted or not, the echo code executes even when I refresh the page and don't click on submit. How can I make sure that the form data is retrieved only when I submit the form?

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"> Event Title:<br> <input type="text" name="eventTitle" id="eventTitle" maxlength="15" size="20" placeholder="Code.fun.do" required><br><br> Event Description:<br> <textarea name="eventDescription" rows="5" cols="50"></textarea><br><br> From:<br> <input type="time" name="eventTimeFrom"><br><br> To:<br> <input type="time" name="eventTimeTo"><br><br> <input id="eventSave" type="submit" name="submit" value="Save"> </form> <?php $eventTitle = $eventDescription = $eventTimeFrom = $eventTimeTo = ""; //if ($_SERVER["REQUEST_METHOD"] == "POST") { if(isset($_POST['submit'])) { echo "<h2>something</h2>"; $eventTitle = test_input($_POST["eventTitle"]); $eventDescription = test_input($_POST["eventDescription"]); $eventTimeFrom = test_input($_POST["eventTimeFrom"]); $eventTimeTo = test_input($_POST["eventTimeTo"]); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> 
12
  • You want to keep the values after submit? Commented Mar 17, 2017 at 10:12
  • @MasivuyeCokile I am using javascript to get form data and display it in the calendar. Commented Mar 17, 2017 at 10:24
  • Your question title says something else, and your body something else, can u explain clearly what u want? Commented Mar 17, 2017 at 10:25
  • @MasivuyeCokile Removed that part of the question Commented Mar 17, 2017 at 10:27
  • When you are testing the form, are you refreshing the page? This could be re-sending the POST request. Commented Mar 17, 2017 at 10:31

1 Answer 1

2

When you submit the form. A POST request is sent to the server.

You can avoid this by redirecting the page once you have completed the work that your script is doing with this:

header("Location: http://mypage.php"); die(); 

Now, the problem here is that you lose echoing data data, so you could add something to give a success message:

header("Location: http://mypage.php?success=true"); die(); 

Now, in your script you could have this where the output would go:

<?php if ( isset( $_GET['success'] && $_GET['success'] == 'true' ) ) { echo 'Your form has been submitted!'; } 

This should avoid the trouble that you're having. There are other techniques though and you should use one that's right for you.

On a side note, POST requests are also resubmitted when you use forward and back buttons in your browser - you should be mindful of this, too.

Sign up to request clarification or add additional context in comments.

1 Comment

Other techniques include using sessions and / or unique URLs - but I don't want to get into that in this specific answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.