[PHP] Text Appearance

Re: php help

You'll need to give more details on this one. Do you mean, if the user's input is incorrect, you make some text appear?

If the person enters the correct password, some text appears. If the person otherwise enters a wrong password an error message pops up?

No idea on this one :/ thanks for help though
 
Re: php help

I just wrote this up for you...

PHP:
<?php
$password = "PASSWORD"; //Set the required password here.
if($_SERVER['REQUEST_METHOD'] == "POST") { //If the form was submitted, 
	if ($_POST['passcode'] != $password) { //Fail
		echo '<b>WRONG PASSWORD!</b>';
	} else { //Success
		echo '<i>The secret to life is: <b>42</b>.</i>';
	}
	echo '<br><a href="#" onclick="history.go(-1);">Back</a>'; //Display back button below the fail/success text.
} else { //No password entered yet, let's display the form.
	echo '<form name="myform" action="index.php" method="post">';
	echo 'Password:';
	echo '<br><input type="password" id="passcode" name="passcode">';
	echo '<br><input type="submit" id="button" value="Go">';
	echo '</form>';
}
?>

The code works fine and is commented. Enjoy.
 
Re: php help

I just wrote this up for you...

PHP:
<?php
$password = "PASSWORD"; //Set the required password here.
if($_SERVER['REQUEST_METHOD'] == "POST") { //If the form was submitted, 
	if ($_POST['passcode'] != $password) { //Fail
		echo '<b>WRONG PASSWORD!</b>';
	} else { //Success
		echo '<i>The secret to life is: <b>42</b>.</i>';
	}
	echo '<br><a href="#" onclick="history.go(-1);">Back</a>'; //Display back button below the fail/success text.
} else { //No password entered yet, let's display the form.
	echo '<form name="myform" action="index.php" method="post">';
	echo 'Password:';
	echo '<br><input type="password" id="passcode" name="passcode">';
	echo '<br><input type="submit" id="button" value="Go">';
	echo '</form>';
}
?>

The code works fine and is commented. Enjoy.

much appreciated for the comments mate!
 
Nothing wrong with the script above, but consider escaping out of PHP when writing non-dynamic HTML to make it easier to later read the code.

PHP:
<?php

// Set up your password / other variables.
$password = "mypass";
$goback = "<a href='javascript:history.go(-1)'>go back</a>";

// Check if form was submitted.
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    // Form was submitted, welcome the user.
    echo "<p>Hello, " . $_POST['user'] . ".";
    // Check if password matches $password.
    if ($_POST['password'] == $password) {
        // Password was correct, access granted.
        echo "<p>Your password was correct.</p>";
        echo "<p>Welcome to the system!";
        echo "<p>$goback</p>";
    } else {
        // Password was incorrect, return to log-in page.
        echo "<p>Your password was incorrect.</p>";
        echo "<p>Please $goback and try again.";
    }
} else {
    // Form has not been submitted yet, display log-in form.
    // Escape out of PHP to display the form for easier code readability.
    ?>
    <p>Please enter your username and password.</p>
    <form name="checkpass" action="./index.php" method="post">
        <p>Username: <input type="text" name="user"/></p>
        <p>Password: <input type="password" name="password"/>
        <input type="submit" name="submit" value="Go"/></p>
    </form>
    <?php
    // Return to PHP to close the IF statement, end of script.
}
?>
 
Back