简体   繁体   中英

Passing PHP form data into a HTML page

The following is the PHP code to my login script:

   <?php

session_start();
if(isset($_POST['submitted']))
{

$username = $_POST['username'];
$password = $_POST['password'];



            $connect = mysqli_connect("localhost", "root", "root", "test");
            if (mysqli_connect_errno()){
                echo mysqli_connect_error();
            } 

            $query = "SELECT * FROM members where username = '$username'";

            $result = mysqli_query($connect, $query);

        $log = mysqli_fetch_array($result);
        $dbusername=$log['username'];
        $dbpassword=$log['password'];
        //echo "<h1> ". $log['username']." ".$log['password']."</h1>";
        if($username == $dbusername && $password == $dbpassword)
        {
            $_SESSION['username'] = $dbusername;
            $_SESSION['password'] = $dbpassword;
            header("Location: Correct.html {$_POST["redirect"]}"); 
        }else
        {
            header("Location: Incorrect.html {$_POST["redirect"]}"); 
        }

} else {
    echo "boo";
}

    ?>

As you can see, the user is redirected to the 'Correct.html' page should they enter an existing username and password combination. The problem is passing information onto the 'Correct.html' page. I would like the ' username ' variable to be passed-on so that the page will show a " Welcome 'username' " message. Now from all my attempts I am aware that I need the following piece of code on the 'Correct.html' page for this variable to be successfully passed through:

    <?php
session_start();
if(isset($_SESSION['username'])){
    echo "Your session is running " . $_SESSION['username'];
}
?>

I have also found out that for this to work, I need to change the extension of my page from .HTML to .PHP. Now the problem is that if I change the extension, my 'Correct.php' page will no longer load, and I am shown a blank screen. I have tried online HTML to PHP converters (which seem to echo every line of my code), but this still does not work.

Would anyone be able to help me on one of two things:

  1. Provide me a solution to passing through a PHP variable in a HTML script (easier, but not sure if possible?)
  2. Correctly convert my HTML code to PHP (longer, but would ensure that variables can easily be passed through)

Please bear in mind that I already have a MySQL database and table setup (you probably already figured that from the code).

The extension of the file names don't matter if you set your web server correctly. If it's Apache HTTP: AddType application/x-httpd-php .php .htm .html , or something like it.

header() expects a full URI. So pass in the domain name and path along with the file name. And get rid of the {$_POST["redirect"]} stuff in the header calls (not sure what you were trying to do with that).

I'll just provide it as an answer with an explanation...

Here is the example answer I cited. What it does is looks at the URL that was sent to the browser, searches for a "?", which would imply there is a parameter being passed. It then grabs everything after the "?" and splits it at the "&"s to provide for all the parameters that were passed. It then loops through what it finds, and creates a list of what was passed in. Like, "username=bob", "thissite=spiffy".

You can use this in any HTML file to handle parameters passed in. There is no need for the server side scripting like PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM