0

when I click on "submit", I always get an error saying that "Object not Found!" However, if I change $action to $_SERVER['PHP_SELF']: i.e.

<form action='<?php echo $action; ?>' method='post'>

to

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>

it will work. However, in this situation, we cannot submit the form to "sighup.php" if there is no error.

<!DOCTYPE html>
<html>
<head>
<title>Testpage</title>
</head>

<body>
<?php
    $uid_err="";
    if(isset($_POST['submit'])) {
            if(empty($_POST['uid'])) {
                    $uid_err="This field cannot be empty!";
                    $action=$_SERVER['PHP_SELF'];
            }   else {
                    $action="signup.php";
            }
    }
?>

<form action='<?php echo $action; ?>' method='post'>
    <input type="txt" name="uid"><?php echo $uid_err; ?><br>
    <button type="submit" name="submit">Submit</button>
</form>     

</body>

</html> 
3
  • Of course can't. Please debug the page after load.. The action surely empty since you do checking before providing any data just like you have a cup with nothing inside but you think it got coffee ready Commented Feb 27, 2018 at 2:54
  • And you can inline (span) source code, so it will be shown in the same style as the block you are referencing. Commented Feb 27, 2018 at 2:58
  • Thank you guys for the quick response. I really appreciate it! Now I realize where the problem is. Commented Feb 27, 2018 at 4:44

1 Answer 1

2

Your $action variable is only set inside of your double-layer if conditional. While you are indeed setting it to $_SERVER['PHP_SELF'] if uid is empty, you're not actually accounting for submit not being set.

Because you need the form action attribute to send the form in the first place, this condition cannot be triggered on page load, and as such, your if condition is never entered, and $action is never set.

To resolve this, you need to set $action in the else condition:

if(isset($_POST['submit'])) {
    if(empty($_POST['uid'])) {
        $uid_err="This field cannot be empty!";
        $action=$_SERVER['PHP_SELF'];
    } else {
        $action="signup.php";
    }
} else {
    $action=$_SERVER['PHP_SELF'];
}

Having said that, it is kind of a moot point to even have the if(isset($_POST['submit'])) ... $action= condition at all, as it makes little sense to modify where the form is sent after it has already been submitted; you're unlikely to want to submit the same form twice to two different locations.

Rather than validating the form, preventing submission, and then attempting to show the form again, validation should be handled on where the form gets submitted to. If you want to prevent the user from even submitting the form, you should use JavaScript for that. However, note that you will also need to validate server-side if you choose to do so.

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

5 Comments

perhaps he should do the validation on the destination signup.php, not on this page
Correct. I've added a note on that.
i guess he could parse uid to signup.php in the url or a session
Thank you Obsidian, and it is very clear to me. I really really appreciate it. And I also thank rtfm for your comments! Actually you are right, I am supposed to move the validation to "signup.php", however, if I do so, how can I show the user the error message: "This field cannot be empty!" in this index.php page? i.e. come back to the index.php from the signup.php, but show the error message beside the box?
Ideally you would use JavaScript to achieve that, hooking your submission into a function. Then you would prevent the submission with e.preventDefault(), and run conditionals that check your validation. If the form is invalid, show the relevant error. If it's valid, trigger the real submission with .submit(). Of course, a user could bypass this JavaScript validation (as it's on the front end), so that's why it's important to check in your PHP as well. For that, you could POST to the same page, and simply check what's sent in $_POST inside your existing if(isset($_POST['submit']) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.