php, form using the same page after submittion

72,149

Solution 1

The other answers are right; you only need to send the user back around to your current page in the "action" property. You can test to see if they did so using "isset".

Something that I'm surprised hasn't been mentioned yet is that your input is not being sanitized, and you're setting yourself up for disaster. Huge injection vulnerability in your action attribute:

$_SERVER['PHP_SELF']

If you don't sanitize this, then someone can just modify the URL that they see and your poor PHP script won't know any better than to process that as your SELF constant.

In other words, you absolutely must use an htmlspecialchars() function to html-encode that parameter. With that, your action should look more like htmlspecialchars($_SERVER['PHP_SELF']).

Solution 2

if current page is index.php, use index.php in form tag as value of action.

like this:

<form action="index.php" method="post">
</form>

u can check for submitted form by putting:

if(isset($_POST)){
...
}

at top of page

Solution 3

Just use below syntax

<form method="post" action="">

You can check whether post is set using isset() method.

Solution 4

this code will help you

<form action="<?= $_SERVER['REQUEST_URI'] ?>" method="post">

or you could just do this:

<form action="" method="post">

Thank you ....

Solution 5

<?php function userPass() {
    echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . ">";
    echo "<input type='text' name='user' /><br/>";
    echo "<input type='text' name='pass' /><br/>";
    echo "<input type='submit' value='Login' />"; }
    if(empty($_POST["user"]))
    {
        userPass();
    }
   *if(!(empty($_POST["user"])))*
    {
        if($_POST["user"] == "comp")
        {
            echo "Welcome comp";
        }
        else
        {
            echo "Wrong user";
        }
    }
    ?>

This part of your code is wrong, you should type : if(!empty($_POST["user"]))

Share:
72,149
Dora
Author by

Dora

learning to be a web developer. Just started so much to learn. Anyone need volunteers I need some work experience :P

Updated on June 19, 2020

Comments

  • Dora
    Dora almost 4 years

    I'm wondering what's the easiest way to make let's say a form with user/pass and submit button with php but after you submit it goes back to the same page instead of going to another php page.

    I'm thinking of if/else statement but a bit confused how to set it after lots tries but still not getting the result wanted

    weird I did all those you guys said before I posted..but...>.<" let's say just something simple like this...

    but I also want to set if nothing is entered then sumbit is clicked there'll be an error....should be something easy but I don't know why I can't seem to figure it out

    <?php
    function userPass()
    {
        echo "<form method='post' action=" . $_SERVER['PHP_SELF'] . ">";
        echo "<input type='text' name='user' /><br/>";
        echo "<input type='text' name='pass' /><br/>";
        echo "<input type='submit' value='Login' />";
    }
        if(empty($_POST["user"]))
        {
            userPass();
        }
        if(!(empty($_POST["user"])))
        {
            if($_POST["user"] == "comp")
            {
                echo "Welcome comp";
            }
            else
            {
                echo "Wrong user";
            }
        }
        ?>
    
  • slfan
    slfan almost 8 years
    could you provide some more information why this is a valid answer?
  • Jei
    Jei almost 8 years
    That exactly how you make your form.
  • Pete
    Pete almost 8 years
    Not w3c conform! Bad value '' for attribute 'action' on element 'form': Must be non-empty.
  • Apostolos
    Apostolos almost 7 years
    This is quite irrelevant to the question, isn't it?
  • Apostolos
    Apostolos almost 7 years
    Setting action to PHP_filename (e.g. index.php) goes w/o saying of course, so it doesn't change anything.
  • forresthopkinsa
    forresthopkinsa almost 7 years
    @Apostolos It's quite relevant to his use case, isn't it?