I am using HTML to make a form with a php processor, like so:
<?php require_once("./scripts/process.php"); ?>
<form action="" method="post">
<input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
I have, in my PHP form, a script that echo
s a message that says "You didn't fill in some of the required fields. Go back and fill them in." But, when I load the page for the first time, it says "You didn't fill in some of the required fields. Go back and fill them in." How can I make it so it says this only when the form is submitted? I can't use <form action="./scripts/process.php
it gives me a 500 Server Error, so I want to do it this way. Is there another PHP method to do this? If so, how? Thanks!
You can check if form have been submitted by checking $_POST value
if(!empty($_POST)) {
// some code
}
But this can fail when there is another form on the same page. In this case, you have to do it like following
if(isset($_POST["name"])) {
// some code
}
just make sure only post request, require process.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once('./scripts/process.php');
}
?>