Dora
Dora

Reputation: 6970

php, form using the same page after submittion

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";
        }
    }
    ?>

Upvotes: 9

Views: 73518

Answers (9)

Apostolos
Apostolos

Reputation: 3445

I think that all have missed the actual question, i.e. if there is a way to stay in the same page after submitting a form. The answer is NO. Once you submit a form -- whether you use the POST or GET method, and of course assuming that you are using $_SERVER['PHP_SELF'] or empty action, etc. -- a new page is opened automatically. This holds at least for PHP. (I have tried a lot of different ways using XAMPP.) I don't now about other scripting languages.

Upvotes: 0

Ehsan Ilahi
Ehsan Ilahi

Reputation: 298

this code will help you

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

or you could just do this:

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

Thank you ....

Upvotes: 1

Boris
Boris

Reputation: 11

This is a code that I created to control learning the required input fields satisfy the requirements. when this is so, the data will be sent to the database. if it does not meet the requirements, there will be a message may be shown at the top of the page

<?php
if (!isset($_POST['submitform'])) {   
}
else
{
$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['lastname'] = $_POST['lastname'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['mobile'] = $_POST['mobile'];
$_SESSION['telephone'] = $_POST['telephone'];
$_SESSION['place'] = $_POST['place'];
$_SESSION['street'] = $_POST['street'];
$_SESSION['housenumber'] = $_POST['housenumber'];
$_SESSION['gender'] = $_POST['gender'];

if (empty($_POST['firstname']) or empty($_POST['lastname']) or empty($_POST['email']) or empty($_POST['mobile'])or empty($_POST['telephone']) or empty($_POST['place'])
or empty($_POST['street']) or empty($_POST['housenumber']) or !isset($_POST['gender']))
{
    echo "Sending denied";
}
else
{       
    require 'database.php';

    header('Location: succes.php');
}  ?>

I hope this is helpful information for you

Upvotes: 1

Dyann
Dyann

Reputation: 317

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

This is exactly how you work with your form to reload the page when click the submit button inside this form.

ADDITIONAL: Add required to all input you wanted to be required or check if it's empty.

Upvotes: 1

forresthopkinsa
forresthopkinsa

Reputation: 1467

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']).

Upvotes: 6

Axel Capaert
Axel Capaert

Reputation: 11

<?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"]))

Upvotes: 1

Techie
Techie

Reputation: 45124

Just use below syntax

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

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

Upvotes: 3

yourdeveloperfriend
yourdeveloperfriend

Reputation: 1590

You can define in the form submission:

<form method=get action=index.php >

You can also leave action out altogether, and it will automatically post/get to that same file.

Upvotes: 0

codefreak
codefreak

Reputation: 7131

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

Upvotes: 4

Related Questions