php, form using the same page after submittion

2019-03-26 11:30发布

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

9条回答
冷血范
2楼-- · 2019-03-26 11:51

this code will help you

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

or you could just do this:

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

Thank you ....

查看更多
神经病院院长
3楼-- · 2019-03-26 11:54
 <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.

查看更多
迷人小祖宗
4楼-- · 2019-03-26 11:55

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.

查看更多
Explosion°爆炸
5楼-- · 2019-03-26 11:55
<?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"]))

查看更多
仙女界的扛把子
6楼-- · 2019-03-26 12:03

Just use below syntax

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

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

查看更多
【Aperson】
7楼-- · 2019-03-26 12:04

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

查看更多
登录 后发表回答