Accessing PHP Session Variables in Multiple Files - php

I have a school project where I'm trying to use session variables to save a log in ID, however I can't seem to access the session variables across files. I'm relatively new to sessions, but have gotten them to work in simpler applications on the same server that I am using for this project. The program works as follows:
getData.php is the log in form where the user enters their ID and password.
front.php collects data from this form and forwards it to another server (of another group member) using php curl. Since this is not my part, I do not have this code.
After accessing a database, information from the database is forwarded back to me, also using curl and I format it in a table, in tList.php.
The problem is that the session variables do not seem to be accessible between the files on MY SERVER. The exact goal I am trying to accomplish is to access the session variables that I create in front.php on tList.php, however the variables are not set even though I assign them in front.php.
Any help would be greatly appreciated. Thank you.
getData.php:
<?php
session_start();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/style.css">
<link rel="stylesheet" type="text/css" href="../css/b.css">
<link href="../css/test.css" rel="stylesheet" type="text/css">
</head>
<body>
<script type="text/javascript">
function status_toggle() {
if(document.getElementById('stud').checked) {
document.getElementById('sl').style.display='block';
document.getElementById('tl').style.display='none';
} else if(document.getElementById('teach').checked) {
document.getElementById('sl').style.display='none';
document.getElementById('tl').style.display='block';
}
}
</script>
<!-- Status Selection -->
<form name="radio" class="nav selection_form" action="" align="center">
<div align="center" class="">
Are you a <br>
Student <input id="stud" type="radio" name="status" value="student" onclick="javascript:status_toggle();" checked>
&nbsp&nbsp&nbsp or &nbsp&nbsp&nbsp
Teacher <input id="teach" type="radio" name="status" value="teacher" onclick="javascript:status_toggle();">
</div>
</form>
<!-- Student Form -->
<form id="sl" name="student_login" action="front.php" class="input-block my_form" autocomplete="on" method="post">
<div align="center">
Username: <input type="text" name="txtUCID" size="15" /><br />
Password: <input type="password" name="txtPasswd" size="15" /><br />
<p><input class="btn-red" id="submit" type="submit" value="Login" /></p>
</div>
</form>
<form style="display:none" id="tl" name="teacher_login" action="front.php" class="input-block my_form" autocomplete="on" method="post">
<div align="center">
Username: <input type="text" name="teacherTxtUCID" size="15" /><br />
Password: <input type="password" name="teacherTxtPasswd" size="15" /><br />
<p><input class="btn-grn" id="submit" type="submit" value="Login" /></p>
</div>
</form>
<div align="center" class="alert_box input-block my_form">
<h2 align="center" type="text" id="local_db"> change me!</h2>
<h2 align="center" type="text" id="njit_db"> change me!</h2>
</div>
</body>
</html>
front.php:
<?php
session_start();
$un = $_POST["txtUCID"];
$pw = $_POST["txtPasswd"];
$_SESSION['id'] = $un;
$curl = curl_init('web.njit.edu/~mc332/cs_490/loginSTD.php');
$postData= array(
'txtUCID' => $un,
'txtPasswd' => $pw
);
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 0,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 5
));
echo $_SESSION['id'];
$result = curl_exec ($curl);
curl_close ($curl);
tList.php
<?php
session_start();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/style.css">
<link rel="stylesheet" type="text/css" href="list.css">
</head>
<body>
<div class="header">
Your Tests
</div>
<div class="list-view">
<?php
echo "SV: ".$_SESSION['id'];
$result = $_POST['tests'];
echo "<table>";
echo "<tr><th>Test ID</th><th>Test Name</th><th>Attempted</th><th>Score</th><th> Attempt Test </th></tr>";
$json = json_decode($result);
$i = 0;
while($i < count($json)) {
$id = $json[$i][0];
$name = $json[$i][1];
$tId = $id;
echo "<tr> <td> $id </td> <td> $name </td> <td> ? </td> <td> ?/100 </td> <td> <form method=\"post\" action=\"goToTest.php\" id=\"form$tId\"><button type=\"submit\">Take Test</button></input><input type=\"text\" style=\"display: none;\" name=\"poop\" value=\"$tId\"></input></form></td> </tr>";
$i++;
}
echo "</table>";
echo "end";
?>
</div>
</body>
</html>
Here is the working application:
file1:
<?php
session_start();
$_SESSION['test'] = "test";
echo "Click";
?>
file2:
<?php
session_start();
echo $_SESSION['test'];
?>

The session_start() function must appear BEFORE the tag
<?php session_start(); ?>
<html>
<body>
</body>
</html>
http://www.w3schools.com/php/php_sessions.asp
Try this to make sure you are passing valid values into the SESSION variable.
<?php
session_start();
if (!empty($_POST["txtUCID"])){
$_SESSION['id'] = $_POST["txtUCID"];
} else {
$_SESSION['id'] = 'no value set';
}
echo $_SESSION['id'];

Related

How to keep the user logged on the entire web

I have a problem logging users. When logs, session work, but when you click to another page, the session will disappear and does not work and when I put session_start to the main page and to login.php, I got error that session is already running. Some ideas? Thanks
Main Page.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type="text/css" href="css.css">
<link rel="icon" href="favicon.ico">
<title>----</title>
</head>
<body>
<!-- HEADER _____________________________________________________________________________ -- >
<div id="whiteblock">
<div id="grayblock">
<div id="logo">
</div>
<h1>----</h1>
<h2>----</h2>
Registration
<?php
include "Login.php";
echo $_SESSION["LogiNick"];
?>
<div id="inputposunuti">
<form name="Log" id="Log" method="post">
<label for="Nick" class="inputtext">Nick:</label>
<input type="text" name="LogiNick" id="LogiNick" class="input"> <?php echo $EmptyNick; echo $Else; ?>
<label for="Pass" class="inputtext">Password:</label>
<input type="password" name="LogiPass" id="LogiPass" class="input"> <?php echo $EmptyPass; echo $Else; ?>
<input type="submit" name="LogIn" value="⇒">
</form>
</div>
</div>
<!-- HEADER _____________________________________________________________________________ -->
<!-- MENU _______________________________________________________________________________ -->
<div id="pruh">
<div id="search">
<form name="search" method="post">
<input type="text" name="hledat" id="sirka">
<input type="submit" name="subhledat" value="Search!" id="button">
</form>
</div>
<div id="menutext">
<p>Home</p>
<p>----</p>
<p>----</p>
<p>----</p>
<p>----</p>
<p>----</p>
</div>
</div>
<!-- MENU _______________________________________________________________________________ -->
<!-- CONTENT ____________________________________________________________________________ -->
<!-- CONTENT ____________________________________________________________________________ -->
<div id="teams"></div>
</div>
<p id="creator">Created by</p>
Login.php
<?php
session_start();
include_once "db.php";
global $db;
$Else = $EmptyNick = $EmptyPass = $EmptyNick = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$LogiNick = $_POST["LogiNick"];
$LogiPass = $_POST["LogiPass"];
if (empty($_POST["LogiNick"])) {
$EmptyNick = " - Nick";
}
if (empty($_POST["LogiPass"])) {
$EmptyPass = " - Password";
}
}
if(!empty($_POST["LogiNick"]) AND !empty($_POST["LogiPass"])){
$LogiPass = $_POST["LogiPass"];
$SHA = sha1($LogiPass);
$sql = "SELECT * FROM WEB_REGISTER WHERE Nick = :nick AND Heslo = :heslo";
$query = $db->prepare($sql);
$query->execute(array('nick' => $LogiNick, 'heslo' => $SHA));
$count = $query->rowCount();
if ($count > 0) {
$_SESSION["LogiNick"];
}
}
?>
You just set a session if you put a value into it.
$_SESSION["name"]=$value;

Updating post with PHP & MySQL

I'm building a sort of blog system, but I'm having trouble with editing an existing 'post' in the system. It is collecting the data from the database, and it displays it. But when I click the update button, I get this error. I've looked for it, checked my code couple of times, but I just couldn't find anything. This is the error.
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
And this is my code.
<?php //include config
require_once('../../includes/config.php');
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); }
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bewerk</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<link rel="stylesheet" href="../style/normalize.css">
<link rel="stylesheet" href="../style/main.css">
<link rel="stylesheet" href="../style/login.css">
</head>
<body>
<?php
//check for any errors
if(isset($error)){
foreach($error as $error){
echo $error.'<br />';
}
}
try {
$stmt = $db->prepare('SELECT patient_id, voornaam, achternaam, leeftijd, lengte, gewicht, foto_url FROM patienten WHERE patient_id = :patient_id') ;
$stmt->execute(array(':patient_id' => $_GET['id']));
$row = $stmt->fetch();
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
<?php
//if form has been submitted process it
if(isset($_POST['submit'])){
$_POST = array_map( 'stripslashes', $_POST );
//collect form data
extract($_POST);
if(!isset($error)){
try {
//insert into database
$stmt = $db->prepare('UPDATE patiënten SET patient_id, voornaam = :voornaam, achternaam = :achternaam, leeftijd = :leeftijd, lengte = :lengte, gewicht = :gewicht, foto_url = :foto_url WHERE patient_id = :patient_id') ;
$stmt->execute(array(
':patient_id' => $patient_id,
':voornaam' => $voornaam,
':achternaam' => $achternaam,
':leeftijd' => $leeftijd,
':lengte' => $lengte,
':gewicht' => $gewicht,
':foto_url' => $foto_url
));
//redirect to index page
header('Location: index.php?action=updated');
exit;
} catch(PDOException $e) {
echo $e->getMessage();
}
}
}
?>
<div class="row">
<div class="col-md-2 col-md-offset-5">
<img src="../style/images/logo.png">
</div>
</div>
<div class="keuze_link row">
<p><b><?php echo $row['voornaam'];?> <?php echo $row['achternaam'];?></b> bewerken</p>
</div>
<div class="container">
<form class="toevoegen" action="" method="post">
<input type="text" name="voornaam" value="<?php echo $row['voornaam'];?>">
<br>
<input type="text" name="achternaam" value="<?php echo $row['achternaam'];?>">
<br>
<input type="text" name="leeftijd" value="<?php echo $row['leeftijd'];?>">
<br>
<input type="text" name="lengte" value="<?php echo $row['lengte'];? >">
<br>
<input type="text" name="gewicht" value="<?php echo $row['gewicht'];?>">
<br>
<input type="text" name="foto_url" value="<?php echo $row['foto_url'];?>">
<br>
<input class="button" type="submit" name="submit" value="Updaten!">
</form>
</div>
</body>
</html>
If you guys wanna know more, or see more code, I'd like to hear it. Thanks in advance.
EDIT. for update.
//insert into database
$stmt = $db->prepare('UPDATE patienten SET patient_id = :patient_id, voornaam = :voornaam, achternaam = :achternaam, leeftijd = :leeftijd, lengte = :lengte, gewicht = :gewicht, foto_url = :foto_url WHERE patient_id = :patient_id') ;
$stmt->execute(array(
'patient_id' => $patient_id,
':voornaam' => $voornaam,
':achternaam' => $achternaam,
':leeftijd' => $leeftijd,
':lengte' => $lengte,
':gewicht' => $gewicht,
':foto_url' => $foto_url
));
Latest form for update
<form class="toevoegen" action="" method="post">
<input type="hidden" name="voornaam" value="<?php echo $row['patient_id'];?>">
<input type="text" name="voornaam" value="<?php echo $row['voornaam'];?>">
<br>
<input type="text" name="achternaam" value="<?php echo $row['achternaam'];?>">
<br>
<input type="text" name="leeftijd" value="<?php echo $row['leeftijd'];?>">
<br>
<input type="text" name="lengte" value="<?php echo $row['lengte'];?>">
<br>
<input type="text" name="gewicht" value="<?php echo $row['gewicht'];?>">
<br>
<input type="text" name="foto_url" value="<?php echo $row['foto_url'];?>">
<br>
<input class="button" type="submit" name="submit" value="Updaten!">
</form>
:patient_id is missing from the $stmt->execute() function. Please add it and try again.
In your update query you forgot about :patient_id to bind
And yes, remove $ before column names.

How to retrieve multiple check box value using explode?

I have several check boxes and when the multiple check box is clicked & form is submitted it goes in the db search for the corresponding result but it is giving me notice :
Warning: explode() expects parameter 2 to be string, array given in C:\xampp\htdocs\enoticeboard\home.php on line 68
this error is 3 times.
how I successfully retrieve it?
my code is:
<?php
include_once 'dbconfig.php';
if(!$user->is_loggedin())
{
$user->redirect('index.php');
}
$user_id = $_SESSION['user_session'];
$stmt = $DB_con->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
?>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
<title>welcome -
<?php print($userRow[ 'user_email']); ?>
</title>
<style>
.left {
margin-left: 20px;
width: 100px;
}
</style>
</head>
<body>
<div class="header">
<div class="left">
<label><a>Welcome : <?php print($userRow['user_name']); ?></a> </label>
</div>
<div class="right">
<label><i class="glyphicon glyphicon-log-out"></i> logout
</label>
</div>
</div>
<div class="content">
<h1>Notice Board</h1>
<div class="left">
<h4>Select Hostel,Branch,Year</h4>
<form action="home.php" method="POST">
<label>Hostel</label>
<br/>
<input type="checkbox" name="hostel[]" value="K.P-1">K.P-1</input>
<br/>
<input type="checkbox" name="hostel[]" value="K.P-2">K.P-2</input>
<br/>
<input type="checkbox" name="hostel[]" value="K.P-3">K.P-3</input>
<br/>
<label>Branch</label>
<br/>
<input type="checkbox" name="branch[]" value="CSE">CSE</input>
<br/>
<input type="checkbox" name="branch[]" value="I.T">I.T</input>
<br/>
<input type="checkbox" name="branch[]" value="CIVIL">CIVIL</input>
<br/>
<label>YEAR</label>
<br/>
<input type="checkbox" name="year[]" value="2011">2011</input>
<br/>
<input type="checkbox" name="year[]" value="2012">2012</input>
<br/>
<input type="checkbox" name="year[]" value="2013">2013</input>
<br/>
<input type="submit" value="See Notices" />
</form>
</div>
<?php
if(isset($_POST['hostel']) && isset($_POST['branch']) && isset($_POST['year']))
{
if(!empty($_POST['hostel']) && !empty($_POST['branch']) && !empty($_POST['year']))
{
$arrayName = $_POST['hostel'];
$arrayName2 = $_POST['branch'];
$arrayName3 = $_POST['year'];
$hostel=explode(",",$arrayName);
$branch=explode(",",$arrayName2);
$year=explode(",",$arrayName3);
$stmt2=$DB_con->prepare("SELECT * FROM doc WHERE hostel=:hostel AND branch=:branch AND year=:year");
$stmt2->execute(array(':hostel'=>$hostel,':branch'=>$branch,':year'=>$year));
$userRow2=$stmt2->fetch(PDO::FETCH_ASSOC);
?>
<div class="middle">
<?php
if($userRow2 > 0){
foreach($userRow2['doc'] as $value2){
print_r($value2['doc']);
echo 'hi';
}
}
else {
echo "No records.";
}
?>
</div>
<?php
}
else {
echo "No Notice Found.";
}
}
else {
echo "Please fill in all fields.";
}
?>
</body>
</html>
explode() function splits string into array.
implode() function converts array into string.
So, it requires string as second parameter.
Your checkbox branch[] is already getting posted as array.
$_POST['branch'] is an array.
You do not need to explode() the checkbox array.
I think you need to combine array into a string.
So, use implode()
Just use:
$arrayName2 = $_POST['branch'];
$branch=implode(",",$arrayName2);
Instead of
$arrayName2 = $_POST['branch'];
$branch=explode(",",$arrayName2);
The warning is clear explode() requires parameter 2 be string, but you are passing array there. If you want to join array element with comma then you should use implode() as below :
$hostel=implode(",",$arrayName);
$branch=implode(",",$arrayName2);
$year=implode(",",$arrayName3);
Try this
if(isset($_POST['Submit'])) {
echo "<pre>";
print_r($_POST);
$checked = implode(',', $_POST['checkbox']);
echo $checked;
}

Populate a certificate with information from a form on another page in php

I am creating e-learning that users will take a quiz. Upon submit they will be sent to the results page where it will show them their scores. If the user passes a link will pop up to a certificate they can print out. I cannot get the certificate to populate variables(first name, last name, etc).
Here is what I have:
index.php (home page where they take the quiz and fill out a form)
<form id="form1" name="form1" method="post" action="results.php" onsubmit="return validateForm() ;">
<label>Last Name:
<input type="text" name="lname" id="lname" tabindex="1" />
</label>
<label>First Name:
<input type="text" name="fname" id="fname" tabindex="2" />
</label>
<label>Title:
<input type="text" name="title" id="title" tabindex="3" /><br/><br/>
</label>
<?php
$quiz = new Quiz( $questions );
echo $quiz->renderQuiz();
?>
<input name="Submit" type="submit" value="Submit" />
</form>
On results.php (page that shows results and link to certificate)
<?php
$ip = $_SERVER['REMOTE_ADDR']; // employee's Ip address
$time = date("d/m/y : H:i:s", time()); // current timestamp
$email_string = create_email_string( $force_email, $_POST['email_to'] );
$questions_correct = array();
$info_to_save = array( $_POST['lname'], $_POST['fname'], $_POST['title'];
// Grades the quiz
$score = (string)round( ( count($questions_correct) / count($questions)*100), 2 );
$variables = array();
$variables['fname'] = $_POST['fname'];
$variables['lname'] = $_POST['lname'];
$variables['test_name'] = $test_name;
$variables['score'] = $score;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Results</title>
<link rel="stylesheet" type="text/css" href="./stylesheets/quiz_stylesheet.css" />
</head>
<body>
<div id="white">
<div id="header">
<img src="./images/ucdheader.jpg" width="1000" id="header" /img>
</div>
<div id="mainContent">
<h1><span>You Scored <?php echo $score ?>%</span>
<br><?php if ($score >= 80)
echo "<a href=certificate.php >Print Certificate</a>
"?></h1>
<p>Below are your results for <?php echo $test_name; ?>. In order to pass this quiz, you must score 80% or better. If you would like to try again, click the following link.</p>
<a href='<?php echo $test_page_name ?>'>Try again</a>
<p>These results were sent to the following addresses: </p>
<ul>
<?php
$all_emailed_to = explode( "; " , $email_string);
foreach ($all_emailed_to as $email) {
echo "<li>" . $email . "</li>";
}
?>
</ul>
</div>
</div>
</body>
</html>
On certificate.php
<title>Certificate</title>
</head>
<body>
<div id="white">
<div class="btn-group btn-group-lg">
<a role="button" class="btn btn-default" href="#" data- function="print">Print Certificate</a>
</div>
<div class="certificate">
<img src="images/cert.png" width="820" height="527" id="cert" />
<div class="name">
<?php $variables['fname'] . " " . $variables['lname'] . ?>
</div>
<div class="course">
<p></p>
</div>
<div class="completion_date">
<?php $variables['time']; ?>
</div>
</div>
</div>
</body>
</html>
Thank you!
You are not passing any variables to the certificate.php page. There are several options of passing data between php pages such as:
1) Sessions
2) $_POST
3) $_GET
The easiest option in your code is using the $_GET variable. Simply use this line of code in results.php:
echo "<a href=certificate.php?fname={$variables['fname']}&lname={$variables['lname']} >Print Certificate</a>"?></h1>
Than in certificate.php retrieve the data by using:
<?php echo $_GET['fname'] . " " . $_GET['lname'] . ?>
Note 1: you will need to urlendode() the variables in the link in case someone puts special chairs in their name/last name
Note 2: It's a hackable approach. An employee could print a certificate with just any name/last name by calling a prepared url.
The best approach would be to rewrite all three pages to use Sessions: http://php.net/manual/en/features.sessions.php
Either you go for Kamran's approach and build a hidden form which will send by POST the information needed in the certificate page, either you go for a database oriented model(i.e. save users and their results and then retrieve those information to generate the certificate).
The first way is pretty unsafe since you can change the values of the fields using Firebug and send dirty unexpected input if you don't test the values.
In results.php store necessary values in SESSION:
$info_to_save = array( $_POST['lname'], $_POST['fname'], $_POST['title'], time);
//Start session if not started
if (!isset($_SESSION)) {
session_start();
}
$_SESSION['certInfo'] = $info_to_save;
Then use it in certificate.php like this:
//Start session if not started
if (!isset($_SESSION)) {
session_start();
}
$lname = $_SESSION['certInfo'][0];
$fname = $_SESSION['certInfo'][1];
$title = $_SESSION['certInfo'][2];
$time = $_SESSION['certInfo'][3];

How to make selected value to appear in other page

I have following code:
<html>
<head>
<meta charset="UTF-8"/>
</head>
<body>
<!--See siin all tekstiväli-->
<H3>Minu küsitlused </H3>
<hr>
<br>
<br>
<br>
<ol>
<?php
include_once 'init/init.funcs.php';
$result = mysql_query('SELECT * from katse_kysimustik_pealkiri');
while($row = mysql_fetch_assoc($result)) {
$titles[] = $row['pealkiri'];
}
foreach($titles as $title) {?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php' method="post">
<input type="submit" name = "saada" value="saada"/>
<input type="button" value="tulemused"/>
<input type="button" value="lõpeta ennetähtaegselt"/>
<input type="button" value="X"/>
</li>
</form>
<?php
}
?>
</ol>
</body>
</html>
<?php
if(isset($_POST['saada'])){
header("Location:http://localhost/Praks/saada.html");
}
?>
Right now there is button "saada" for each title in database. When I click "saada", page saada.html will appear.
This is saada.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Saada</title>
</head>
<form action="test1.php" method="POST">
<body>
<header>Küsitluse pealkiri</header>
<br>
Lisa sõnumile pealkiri:
<br>
<textarea rows='1' name='pealkiri'>Küsitluse algne pealkiri</textarea>
<br>
Lisa Adressaadid: <br>
<textarea rows='1' name='email'></textarea>
<br>
Lisa sõnum:
<br>
<textarea rows='4' name='tekst'></textarea>
<br><footer>
<INPUT TYPE="submit" VALUE="Saada" NAME="Saada">
</form>
<FORM METHOD="LINK" ACTION="Minu_kysitlused.html">
<INPUT TYPE="submit" VALUE="Loobu">
</footer>
</body>
</html>
My question is how could I make it work so value of $title appears in saada.html. The value of this $title which is next to 'saada' button I clicked. I need this value to be in this line instead of Küsitluse algne pealkiri:
<textarea rows='1' name='pealkiri'>Küsitluse algne pealkiri</textarea>
You can not have a value through PHP in HTML type page....change page extension to .php
then pass your $title in input hidden field in your form
like this
foreach($titles as $title) {?>
<li>
<?php echo $title ?>
<form action='Minu_kysitlused_1.php' method="post">
<input type="submit" name = "saada" value="saada"/>
<input type="button" value="tulemused"/>
<input type="button" value="lõpeta ennetähtaegselt"/>
<input type="button" value="X"/>
<input type="hidden" name="title" value="<?php echo $title;?>"/>
</li>
</form>
<?php
}
?>
Now set the cookie
<?php
if(isset($_POST['saada']))
{
if(isset($_REQUEST['title']))
{
$title = $_REQUEST['title'];
setcookie("page_title", $title, time()+3600);
}
header("Location:http://localhost/Praks/saada.html");
}
?>
Now on Your saada.html you can use js to retrieve cookie like this
JS Function Reference
var title = getCookie("page_title"); // retrieve cookie
document.getElementById('page_title').value = title; // put title into textaread
function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2) return parts.pop().split(";").shift();
}
<textarea rows='1' name='pealkiri' id="page_title" >Küsitluse algne pealkiri</textarea>

Categories