Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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";
        }
    }
    ?>
like image 645
Dora Avatar asked Feb 28 '13 08:02

Dora


People also ask

How do I display form data on the same page?

If you want to add content to a page you need to work with the DOM. Google "create div javascript" or "create span javascript" for examples, you basically need to create an element that has your text in it and add that element to the part of the page you want the text to display.

How do you get the success message in the same page after submitting the contact form?

If you want the same form page displayed again after submission with results, you will make the action the same page and include the processing script at the beginning of the file. Instead of echoing the result message within the processor as you have, store it as a variable which can be output in the html form page.

How can I submit a form without refreshing the page?

Use jQuery's submit event to handle the form submit, add return false; at the end of the submit handle function to prevent the page to reload. return false ; });

How can submit form to itself in PHP?

The proper way would be to use $_SERVER["PHP_SELF"] (in conjunction with htmlspecialchars to avoid possible exploits). You can also just skip the action= part empty, which is not W3C valid, but currently works in most (all?) browsers - the default is to submit to self if it's empty.


2 Answers

Just use below syntax

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

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

like image 115
Techie Avatar answered Oct 05 '22 17:10

Techie


this code will help you

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

or you could just do this:

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

Thank you ....

like image 31
Ehsan Ilahi Avatar answered Oct 05 '22 19:10

Ehsan Ilahi