I'm a student and I'm making a quiz using php and mysql, my problem is I'm trying to echo a name on the results page but it doesn't work.
My first page is an index page where I create a form which gets the users name which I send to my quiz.php page.
<form method="post" action="quiz.php">
<img src="pictures/indeximage.jpg" alt="horrormovies" width="1024" height="640">
<p>
Please Enter Your Name
<br>
<input type="text" name="name">
</p>
<input type="submit" name="submit" value="Start">
</form>
on my quiz.php page i put make a variable and put it in a session
<?php
//start session
session_start();
$var_name=$_REQUEST['name'];
$_SESSION['ses_name']=$var_name;
?>
On my results page I have this
<?php
session_start();
$var_name=$_SESSION['ses_name'];
?>
<p>
Thank you for taking the quiz <?php echo $var_name; ?>.
</p>
Use isset for assign value in session variable. for good practice.
if(isset($_POST['submit']))
{
//start session
session_start();
$var_name=$_REQUEST['name'];
$_SESSION['ses_name']=$var_name;
}
quiz.php
session_start();
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$_SESSION['ses_name']=$_REQUEST['name'];
}
Try this code :-
results page
<?php
//start session
session_start();
if(!empty($_SESSION['ses_name']))
{
?>
<p>Thank you for taking the quiz <?php echo $_SESSION['ses_name']; ?>.</p>
<?php
}
else{
echo 'session not set ';die;
}
?>
Related
Helloo, I have a mutistep form functionality where
in "Step1" ( page 1 ) i have a form with three radioButton
And in step 2 (page2 ) I have another form.
So, My problem is that I want to restrict access to page2 via URL like if I type in Browser Addressbar : localhost/page1.php then it should load page1.php but when I write localhost/page2.php then it should restrict user to have access for page2 and I would like to be redirected to localhost/page1.php
so i tried this :
page1 :
<form class="type" action="page2.php" method="post">
<label style="text-align:center;"> Type de compte : </label>
</br> </br>
<input type="radio" name="typeCompte" value="enseig" required>
Enseignant </br>
<input type="radio" name="typeCompte" value="etud" required>
Etudiant </br>
<input type="radio" name="typeCompte" value="admin" required>
Adminstrateur </br>
</br></br>
<input type="submit" class="make" name="submit" value="Valider">
</form>
<?php
if(isset($_POST['submit']))
{
$_SESSION['log']=1;
}
?>
page 2 :
<?php
session_start();
if($_SESSION['log']!=1)
header("location: page1.php");//redirecting to first page
?>
Try putting session variable as bool like...
In Page1.php
<?php
session_start();
if($_POST['submit']){
$_SESSION['log'] = "true";
header("Location: page2.php");
}
?>
Then In Page2.php
if(!isset($_SESSION['log']) && $_SESSION['log'] == "false"){
//send them back
header("Location: page1.php");
}
else{
//your logic if any.
}
I think in Php session might be working on true false basis only..
to check for session you can use isset command
<?php session_start();
//if ! means not isset check if the session is active
if(!isset($_SESSION['log']))
header("location:page1.php");
?>
in your page where you assign the session for log you can use it to assign any value for the user that you might need later. however do some reading about .htaccess and put your limited access file in a different dir
on page 1
$t='1';
$_SESSION['log']=$t;
and very very important on the first line after <?php on page 1 you must have
session_start();
edit try this that i wrote only spend 5 min so it is not much but i tested it and it works make 2 file test1.php and test2.php
test1.php
<?php
session_start();
if(isset($_POST['set'])){
$log='1';
$_session['log']=$log;
include_once("test2.php");
exit;
}
if(isset($_POST['dontset'])){
include_once("test2.php");
}
?>
<html>
<body>
<form action="" method="post">
<input type="submit" name="set" value="set">
<input type="submit" name="dontset" value="dontset">
</form>
</body>
</html>
test2.php
<?php
session_start();
if (!isset($_session['log'])){
echo 'you are not authorised';
header("location:test1.php");
}
if (isset($_session['log'])){
echo 'you are authorised';
}
?>
<html>
<body>
<form action="" method="post">
<input type="submit" name="destroy" value="destroy">
</form>
</body>
</html>
<?php
if(isset($_POST['destroy'])){
session_destroy();
include_once("test2.php");
exit;
}
?>
it also shows you how to logout the user using sessions
I want to write a code which would be remember name given by user and on next visit will welcome him with this given name. I don't really understand cookies and session yet so I would be thankful for any help. I wrote something like this:
File: 1.php
<?php
session_start();
?>
<html>
<form action="2.php" method="post">
Name:<input type="text" name="name"/></br>
<input type="submit" value="send"/>
</form>
</html>
<?php
$name=$_POST['name'];
setcookie('name',$name,time()+3600*24);
$_SESSION['name']=$name;
?>
File: 2.php
//2.php
<?php
session_start();
if(isset($_COOKIE['name']))
echo "Hello".$_SESSION['name'];
else
echo "Cookie doesnt exist";
?>
In the example below, 1.php is just used for submitting to 2.php so no PHP code is being used.
1.php
<html>
<form action="2.php" method="post">
Name:<input type="text" name="name"/><br>
<input type="submit" name="submit" value="send"/>
</form>
</html>
I've shown both instances of this below on how to set a cookie and session. You can refresh just 2.php and the cookie output should still show the value of $_COOKIE['name'].
2.php
<?php
session_start();
if (isset($_POST['name'])) {
$_SESSION['username'] = $_POST['name'];
setcookie('name',$_SESSION['username'],time()+3600*24);
}
//Session Value will show in first instance
echo "Session Name: " . $_SESSION['username'] . "<br>";
//Cookie Value will not how until you refresh page
echo "Cookie Name: " . $_COOKIE['name'] . "<br>";
?>
Edit: Variables will not be overwritten when the page is refreshed.
I am aware that there are several topics about this but after hours of reading I still can't figure out what's going wrong.
I'm building a survey with a login screen at index.php. The user is required to insert their name and submit the form. The username should be saved and passed onto setup1.php.
This is part of my index.php:
<?php
session_start();
print session_id();
?>
<form id="login" method="POST" action="setup1.php">
<input id="participant" name="participant" type="text" size="20"/>
<input type="submit" name="start" value="Start"/>
</form>
<?php
$name = $_POST['participant'];
$_SESSION['username'] = $name;
?>
Start of setup1.php:
<?php
session_start();
print session_id();
print_r($_SESSION);
echo $_SESSION['username'];
?>
My $_SESSION variable is empty, I have nothing printed on the following page setup.php. I would appreciate if you could help.
Your $_POST code is in the wrong file. Your form is going to setup1.php, but you're trying to set the $_SESSION in your index.php.
You need to take it out of there and put it in setup1.php:
<?php
session_start();
if (!isset($_POST['participant'])) {
die('No $_POST data');
}
$_SESSION['username'] = $_POST['participant'];
print session_id();
print_r($_SESSION);
echo $_SESSION['username'];
?>
Also, make sure that you're using $_SESSION and not %_SESSION. I hope it was just a typo.
Your form hasn't been submitted when you set the $_SESSION['username'], i.e., $_POST['participant'] has no value.
You should move the piece of code below from index.php to setup1.php
<?php
$name = $_POST['participant'];
$_SESSION['username'] = $name;
?>
index.php
<?php
session_start();
?>
<form id="login" method="POST" action="setup1.php">
<input id="participant" name="participant" type="text" size="20"/>
<input type="submit" name="start" value="Start"/>
</form>
setup1.php
<?php
session_start();
if(isset($_POST['participant']) && ! empty($_POST['participant']))
{
$_SESSION['username'] = $_POST['participant'];
echo $_SESSION['username'];
}
?>`
I have numerous pages that I need to access a variable on. This variable is assigned a value when a user enters an ID into a form on accounts.php:
account.php
<form action="afterlog.php" class="form" method="post">
<input type="text" name="amid" id = "amid" class="input" />
<input class="btn" type="submit" value="Go" />
</form>
which posts the value 'amid' to afterlog.php
afterlog.php
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
header('Location: customer_view.php');
}
?>
which checks the POST, assigns the session variable, and redirects rhe user to customer_view.php
customer_view.php
I need to use '$account_manager_id' in this page and all pages after. Here is how I am assigning it the value of the _SESSION variable:
<?php
session_start();
$_SESSION['account_manager_id']=$account_manager_id;
?>
Bu the value isn't being held on any of the pages, including customer_view.php. I know its passing to afterload.php because it prints out on that page, but its gone after that page.
What I am doing wrong?
Thanks for your help!
You are trying to assign a value to $_SESSION['account_manager_id'] before $account_manager_id has any value in it. You just need to switch the order:
$_SESSION['account_manager_id']=$account_manager_id;
$account_manager_id = $_POST['amid'];
or simply:
$_SESSION['account_manager_id'] = $_POST['amid'];
in afterlog.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$account_manager_id = $_POST['amid'];
$_SESSION['account_manager_id']=$account_manager_id;
header('Location: customer_view.php');
}
?>
or
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$_SESSION['account_manager_id']=$_POST['amid'];
header('Location: customer_view.php');
}
?>
I have 2 php files in my folder. In page1.php, there's a textarea, user should enter some values in it. In page2.php, it will grab what is in the textarea and work with its program. But I can't find a command that grabs the value in textarea. Can someone help me?
page1.php:
<?
$hello = "hello";
?>
<html>
<input type = "text" name = "user_input">
</input>
</html>
page2.php
<?
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
?>
So, is there anyone that can solve this? =/
Use $_GET or $_POST in page2.php
page1.php
<?
$hello = "hello";
?>
<html>
<form method="get" action="page2.php" enctype="multipart/form-data">
<input type = "text" name = "user_input">
<input type="submit">
</form>
</html>
page2.php
<?
$text=$_GET['user_input'];
ob_start();
include 'page1.php';
ob_end_clean();
echo $hello;
echo $text;
?>
You may use either $_GET['user_input'] or $_POST['user_input'].
The difference is, you can see the data in the url (visible to everyone) when using GET method and not in the other method.
Also, always use <input> elements (which you want to pass to another file) inside a <form> and specify action="file.php", to where you want to pass data, and the method, either method="get" or method="post", like;
<form method="get" action="page2.php">
also specify the method to grab data in the target file also, like;
$text=$_GET['user_input']; or $text=$_POST['user_input'];
And in your case, you may use;
Method 1
<?php
$hello = "hello";
?>
<html>
<form method="get" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_GET['user_input'];
echo $text;
?>
Method 2
<?php
$hello = "hello";
?>
<html>
<form method="post" action="page2.php">
<input type="text" name="user_input">
<input type="submit">
</form>
</html>
page2.php
<?php
$text=$_POST['user_input'];
echo $text;
?>
If you want to share the data over a number of pages, you may do this using PHP Session or saving the data in a cookie.
1. Using Sessions
<?php
session_start();
$_SESSION['data'] = 1; // store session data
echo "Pageviews = ". $_SESSION['data']; //retrieve data
?>
Make sure you add session_start(); on every page you want to handle session
You can read more about php sessions here www.tizag.com/phpT/phpsessions.php/
2. Using Cookie
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
and retreive it using
echo $_COOKIE["user"];
You can read more about php sessions here http://www.w3schools.com/php/php_cookies.asp
hope this helps...:)
basically your page1.php is a page with some form in it with a text area. Now user will have to fill it and submit the form to page2.php. You can't echo it's content like that, because that will be on browser subject to user actions. Use a form and submit the data to page2.php. Like this:
page1.php
<html>
<head>
</head>
<body>
<form action="page2.php" method="post">
<textarea name="t1">
</textarea>
</form>
</body>
</html>
page2.php
<?php
$textAreaContents = isset($_POST['t1'])?$_POST['t1']:'';
echo "You submitted: ".$textAreaContents;
?>
if i were you i should use sessions for this. that is where they were made for..
example:when user clicks on submit.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$_SESSION['post'] = $_POST;
}
that is where every post variable will be put in a session.
and your inputbox will be something like this..
<textarea name="message" type="text" value="" rows="0" cols="0" placeholder="" ><?php if(isset($_SESSION['post'])){echo $_SESSION['post']['message'];} ?></textarea>
?>
note that you now can use every post variable that you used in your form by echo (example)
echo $_SESSION['post']['message']
where message is the name of the inputbox. in this case of the textarea
don't forget that at the end when you don't want to use the session anymore use session_destroy(); otherwise you will keep having it in your form. and don't forget session_start(); above every page where you are planning to use sessions ( it must be at 1st line of your document at all times)