PHP Form echoing Post variable not working - php

<?php
ob_start();
// First we execute our common code to connection to the database and start the session
define('MyConst', TRUE);
include('../database.class.php');
include('../table.class.php');
include('../user.class.php');
include('../loginattempts.class.php');
include('../timer.class.php');
include('../characters.class.php');
include('../weapontype.class.php');
include('../objects/weapons/weaponobject.class.php');
include('../objects/weapons/bowieknife.class.php');
include('../npc/enemy.class.php');
include('../npc/skinhead.class.php');
include('../npc.class.php');
include('../npctype.class.php');
include('../functions.php');
include('../loginf.php');
include('locationf.php');
$dbo = database::getInstance();
$dbo -> connect("***************", "********", "********", "***************", array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
secSessionStart();
// At the top of the page we check to see whether the user is logged in or not
if(empty($_SESSION['user']))
{
// If they are not, we redirect them to the login page.
header("Location: login.php");
// Remember that this die statement is absolutely critical. Without it,
// people can view your members-only content without logging in.
die("Redirecting to login.php");
}
$_SESSION['currentlocation'] = "combat.php";
?>
<?php
if($_POST['formSubmit'] == "Submit")
{
$varMovie = $_POST['formMovie'];
echo $varMovie;
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
Which is your favorite movie?
<input type="text" name="formMovie" maxlength="50">
<input type="submit" name="formSubmit" value="Submit">
</form>
</body>
</html>
Ok...so its supposed to echo out some text. Instead it just reloads the form! I'm not sure what else to write and it won't allow me to post so i'm just going to repeat what i've wrote until i reach the limit.

Add an ELSE part in the HTML, that will either show the form OR the answer, but keeps the header etc intact.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
if($_POST['formSubmit'] == "Submit")
{
$varMovie = $_POST['formMovie'];
echo $varMovie;
}
else {
?>
<form action="index.php" method="post">
Which is your favorite movie?
<input type="text" name="formMovie" maxlength="50">
<input type="submit" name="formSubmit" value="Submit">
</form>
<?php } ?>
</body>
</html>

I would tend to use:
<?php
if (array_key_exists("formSubmit",$_POST) && !strcmp($_POST["formSubmit"],'Submit'))
{
$varMovie = $_POST['formMovie'];
echo "Movie=(${varMovie})<br>\n";
}
:
:
Also comment out all the includes etc. above this, check it's giving you the contents of formMovie then add the other stuff back in gradually until it fails (or not).
Cheers.

Related

Loading new page after php code has run?

I have a form, this form is on a page called question1.php, and I want it to load question2.php when the submit button is pressed.
<form action="question2.php" method="post">
<input type="radio" name="ans" value="cuboid">
<input type="radio" name="ans" value="cone">
<input type="radio" name="ans" value="cylinder">
<input type="radio" name="ans" value="sphere">
<input type="submit" value="submit" name="submit">
</form>
But I also have this php code
<?php
if(isset($_POST['submit'])) {
if(isset( $_POST['ans'])) {
$selected_answer = $_POST['ans'];
if($selected_answer == "cuboid") {
$_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;
}
}
}
?>
EDIT: I have made a simpler demo to try and explain myself better, i have got three pages.
page1.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="page2.php">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
?>
</body>
</html>
page2.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="page3.php" method="post">
<input type="radio" name="ans" value="color">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Echo session variables that were set on previous page
if(isset($_POST['submit'])) {
if(isset($_POST['ans'])) {
$selected_answer = $_POST['ans'];
if($selected_answer == "color") {
$_SESSION["favcolor"] = "red";
}
}
}
?>
</body>
</html>
And page3.php
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo "Favorite color is " . $_SESSION["favcolor"] . ".";
?>
</body>
</html>
So on the first page I declare the session variable "favcolor", then on the second page if the user selects the radio button I want to update the color to red, however it just won't chane for me, on the third page it is still printing green
You had issue on page2.php because you are submitting form on page3.php but you are handling submission request on page2.php. So i corrected below for you.
page1.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="page2.php" method="post">
<input type="submit" value="submit" name="submit">
</form>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
?>
</body>
</html>
page2.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="page3.php" method="post">
<input type="radio" name="ans" value="color">
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>
page3.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
if (isset($_POST['submit'])) {
if (isset($_POST['ans'])) {
$selected_answer = $_POST['ans'];
if ($selected_answer == "color") {
$_SESSION["favcolor"] = "red";
}
}
}
echo "Favorite color is " . $_SESSION["favcolor"] . ".";
?>
</body>
</html>
I hope this should work.
$_SESSION["cuboid"] = isset($_SESSION["cuboid"]) ? $_SESSION["cuboid"] : 0;
$_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;
From what I can tell, you are attempting to redirect to another webpage.
Try sending HTTP headers using the header() function.
header('Location: http://example.com/page3.php');
exit();
If the HTTP header doesn't work at first, turn on the output buffer using ob_start().
You are checking for $_POST['ans'] in the wrong page.
Currently you are checking for 'ans' when user enters page2.php, which is before the user submits the form that contains that input element.
When you submit the form on page2.php, the action sends the request over to page3.php, so you actually want to move your if statement that sets the color to green to the top (below session start) of page3.php

PHP Keep the variable scope even after the page reload

The website generates the random number from 1 to 100 when accessing the first page(page1.php). And the user will guess the number.
The first page contains
- a text box for accepting a
number from the user, and a submit button.
The second page(page2.php) will be returned to the user if the guess number is too high or too low. And the page shows a message telling the user "Too High" or "Too Low". The page also contains
a button(retry button) that allows the user to go back to the first page(page1.php) to re-enter a new number
a button that allows the user to quit the game.
The third page(page3.php) is returned to the user if the guess is correct. The page displays "Correct", the random number, and the count of tries.
And I have this index.php which is heart for all the pages. And here is the code.
index.php
<?php
$name = '';
$inputnumber = '';
$random = 33; //this is just an assumption to keep it simple
$message = '';
$guesscount = '';
if (isset($_POST['action'])) {
$action = $_POST['action'];
}
if ($action === 'guess') {
$guesscount = $_POST['$guesscount'];
$inputnumber = $_POST['$inputnumber'];
if ($inputnumber == $random) {
$message = "Correct!";
include 'page3.php';
}
if ($inputnumber > $random) {
$message = "Too High";
include 'page2.php';
}
if ($inputnumber < $random) {
$message = "Too Low";
include 'page2.php';
}
}
if ($action === 'retry') {
include 'page1.php';
}
page1.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Number Guess: <input type="text" name="$inputnumber" value="<?php if(isset($inputnumber)==1){
echo $inputnumber;}else echo ""; ?>" /><br>
<input type="submit" name="action" value="guess" />
<hr>
Guess Count: <?php echo $guesscount; ?>
</form>
</body>
</html>
page2.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Message: <?php echo $message; ?>
<input type="hidden" name="$guesscount" value="<?php echo $guesscount;?>"/><br>
<input type="submit" name="action" value="retry" />
<hr>
Guess Count: <?php echo $guesscount;?>
</form>
</body>
</html>
page3.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Number Guess</title>
</head>
<body>
<h1>Number Guess</h1>
<form name="myForm" action="index.php" method="post" >
Message: <?php echo $message; ?>
Number of Tries: <?php echo $guesscount; ?>
<input type="submit" name="action" value="ok" />
</form>
</body>
</html>
page1.php is the page to load first.
Challenge I have faced is, I couldn't keep the $guesscount stable always. It keeps resetting on me. I have tried session but couldn't resolve it.Please help resolving it.
Thanks in advance.
I don't know why but my gut feeling tells me that the reason why the session is not working for you on other pages is because you do not initiate it ??
So what you have to do is:
index.php
<?php
session_start();
$_SESSION['myVariable'] = 'myVariable';
?>
page1.php
<?php
session_start();
$mySessionVar = $_SESSION['myVariable'];
var_dump($mySessionVar); // <- this should print myVariable
?>
You may get an error saying that $_SESSION is null or not set and to prevent that you can just enclose $_SESSION inside and isset method
if(isset($_SESSION['myVariable']) && $_SESSION['myVariable'] != null) {
$mySessionVar = $_SESSION['myVariable'[;
}

PHP $_SESSION Not checking login status

I've looked through multiple web articles and stackoverflow answers, however I cannot find the bug in my code. Maybe I've been looking at it too long.
Basically I'm just setting up a simple login for a demonstration, yes I know its inject-able and outdated, this doesn't matter. Basically I'm using a login with sessions and then redirecting the user to secure content when they're logged in. I've also created a script that checks for the session variables, to see if the user is logged in or not. Basically, I'm beating a dead horse and I don't know why this isn't working, could someone please help?
index.php:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Welcome, please log in</title>
<link href="../css/admin.css" rel="stylesheet" type="text/css">
</head>
<body>
<?PHP require_once"scripts/mysql_connect.php"; // Establish a database connection ?>
<div id="admin_top">
<div id="admin_logo"></div>
</div>
<div id="admin_login_box">
<H1 style="margin-left: 20px;">Please log in</H1>
<hr><br>
<?PHP
echo "<form method='post' action='checklogin.php' name='loginform'>
<input type='email' name='aEmail' placeholder='Your Email Address' required><br>
<input type='password' name='aPassword' placeholder='Password' required><br><br>
<input type='submit' value='Log In'>
</form>"
?>
</div>
</body>
</html>
checklogin.php:
<!doctype html>
<html>
<head>
<title>Checking login...</title>
<link href="../css/admin.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="admin_top">
<div id="admin_logo"></div>
</div>
<div id="admin_login_box">
<?php
require_once"scripts/mysql_connect.php";
$aEmail = $_POST['aEmail'];
$aPassword = $_POST['aPassword'];
$md5Password = MD5($aPassword);
$sql = "SQL";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$active = $row['active'];
$count = mysql_num_rows($result);
// If result matched, table row must be 1 row.
if($count == 1) {
$_SESSION["login"] = "OK";
$_SESSION["aEmail"] = $aEmail;
echo "<h1>Log in successfull!</h1>
<hr><br />
Your details checked out! Redirecting you now...";
// Wait 1 seconds then redirect to the secure content.
header("Location: http://www.website.com/secure_content.php");
} else {
echo "<h1>Log in unsuccessfull!</h1>
<hr><br />
Sorry. It seems your log in detials were incorrect. Please go back and try again.";
// Wait 2 seconds then redirect back to the log in page.
header("Location: http://www.website.com/index.php");
}
exit;
?>
</div>
</body>
</html>
loginstatus.php:
<?php session_start();
if(!(isset($_SESSION["login"]) && $_SESSION["login"] == "OK")) {
header("Location: http://www.website.com/index.php");
exit;
}
?>
Thanks for any help!
In checklogin.php and index.php you need to start the session. Add the following code before <!doctype html>
Add this code:
<?php session_start(); ?>
You forgot to put that line in this file because you are creating a new session during the checks in the database.
Looks like you haven't started the session in the first place. On the top of your page please write the following code:
<?php session_start(); ?>
Now, secondly, I'd suggest you to write your HTML and PHP separately instead of writing your HTML for the form within the echo.
Also, it's better if you add a name to your submit button.
Let me show a sample below.
<div id="admin_login_box">
<H1 style="margin-left: 20px;">Please log in</H1>
<hr><br>
<form method='POST' action='checklogin.php' name='loginform'>
<input type='email' name='aEmail' placeholder='Your Email Address' required><br>
<input type='password' name='aPassword' placeholder='Password' required><br><br>
<input type='submit' name='submit' value='Log In'>
</form>
Now, in your checklogin.php. you should place an isset condition and see if you're getting any POST request.
Try this:
<?php
require_once"scripts/mysql_connect.php";
if (isset($_POST['submit']) { // Add this condition
$aEmail = $_POST['aEmail'];
$aPassword = $_POST['aPassword'];
$md5Password = MD5($aPassword);
/* Other code */
if($count == 1) {
/* Other code */
} else {
/* Other code */
}
}
Hope this helps.

Bad html output

Trying to write form of registration.
<?php
include_once 'core.php';
if (User::IsAuthorization()) {
header("Location: /");
}
if(!is_null($_POST["Registration"])){
User::Registration($_POST["login"], $_POST["password"]);
$user = new User();
$user->Authorization($_POST["login"], $_POST["password"]);
$user->SetSession();
header("Location: /");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Регистрация</title>
</head>
<body>
<form action="registration.html" method="POST">
<input type="text" name="login" placeholder="login">
<input type="password" name="password" placeholder="password">
<input type="submit" value="Регистрация" name="Registration">
</form>
</body>
</html>
This form works correctly on my friend's laptop. But in my case, the output contains come code of php. I use denwer as local server.
it contains this code(but it doesn't have to):
Authorization($_POST["login"], $_POST["password"]); $user->SetSession(); header("Location: /"); } ?>
Link to picture: http://s23.postimg.org/5vv204qor/Capture.png
Simply start your server and use localhost to show it on webpage.
It should shows the same page as the one being shown on your fd's laptop.
If it does not, it probably forgot to include or change html to php in the file name.
Thanks

Login error Page using PHP page

i have login page if my username and password is incorrect it will display the error message using different page. i am using separate page for that. how can i display error message in login page itself.
login
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title> PHP Login </title>
</head>
<body>
<center>
<form method=”post” action=”login.php”>
<table>
<tr><td>Username:</td><td><input type=”text” name=”usr”></td></tr>
<tr><td>Password:</td><td><input type=”password” name=”pswd”></td></tr>
<tr><td><input type=”submit” name=”login” value=”Login”></td>
<td><input type=”reset” name=”reset” value=”Reset”></td></tr>
</table>
</form>
</center>
</body>
</html>
Controller
<?php
session_start();
if($_REQUEST['usr']==”ABC” && $_REQUEST['pswd']==”123″){
$_SESSION['usr'] = “ABC”;
$_SESSION['pswd'] = “123″;
header(“Location: content.php”);
}
else{
header(“Location: niceform.php”);
}
?>
Your code contained invalid double-quotes.
I replaced all instances of “ and ” with " (standard double-quotes).
The cause is usually the result from copying/pasting code taken from the Web.
See reformatted code below.
To show an error message in login page, you can use any of the following:
Replace header("Location: niceform.php"); with echo "Sorry, no access;"
or with die("Sorry, no access"); or exit("Sorry, no access");
Sidenote:
You can use include 'content.php'; to include a file instead of redirecting with header()
This code has been tested and working.
HTML Form:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> PHP Login </title>
</head>
<body>
<center>
<form method="post" action="login.php">
<table>
<tr><td>Username:</td><td><input type="text" name="usr"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pswd"></td></tr>
<tr><td><input type="submit" name="login" value="Login"></td>
<td><input type="reset" name="reset" value="Reset"></td></tr>
</table>
</form>
</center>
</body>
</html>
login.php:
<?php
session_start();
if($_REQUEST['usr']=="ABC" && $_REQUEST['pswd']=="123"){
$_SESSION['usr'] = "ABC";
$_SESSION['pswd'] = "123";
header("Location: content.php");
// include 'content.php'; // optional, but delete the line above if using it.
}
else{
header("Location: niceform.php");
// include 'niceform.php'; // optional, but delete the line above if using it.
}
?>
Would you try this way
ob_start();
session_start();
if($_REQUEST['usr']==”ABC” && $_REQUEST['pswd']==”123″){
$_SESSION['usr'] = “ABC”;
$_SESSION['pswd'] = “123″;
header(“Location: content.php”);
}
else{
header(“Location: niceform.php”);
}

Categories