PHP sessions do not pass variables to the other page - php

I know that if i want to pass variables in php from a page to another one I can use sessions, but I don't know what is wrong with this code, it's just doesn't work,
I want to pass the $cout variable from page 1 to page 2
PAGE 1
<?php
session_start();
if(isset($_POST['date']))
{
$dat = $_POST['date'];
$matricule = $_POST['matricule'];
$kilometrage = $_POST['kilometrage'];
$num_fact = $_POST['num_fact'];
$Fournisseur = $_POST['Fournisseur'];
$num_bon = $_POST['num_bon'];
$Fonctionnaire = $_POST['Fonctionnaire'];
$cout = $_POST['cout'];
//here is all what I did
$_SESSION['cout'] = $_POST['cout'];
header("Location: PAGE2.php") ;
}
?>
and here is PAGE 2:
<?php
session_start();
$cout = $_SESSION['cout'];
echo $cout ;?>
and here is the form
<form method="post" action="page1.php" >
<input type="date" placeholder="La Date Ex: 2014-07-17" name="date"><br>
<input type="text" placeholder="Matricule" name="matricule"><br>
<input type="text" placeholder="Kilometrage" name="kilometrage"><br>
<input type="text" placeholder="Numero de facteur" name="num_fact"><br>
<input type="text" placeholder="Fournisseur" name="Fournisseur" ><br>
<input type="text" placeholder="N° de bon à delivrer au fornisseurs ou facture" name="num_bon"><br>
<input type="text" placeholder="Fonctionnaire ayant effectué la Réparation" name="Fonctionnaire" ><br>
<input type="text" placeholder="le coût de la reparation en DH" name="cout" ><br>
<input type="submit" value="Valider">
</form>`

Your code seems correct, but dependant on the existence of $_POST['date'] variable.
When you submit the form - please make sure that it is not empty.
EDIT2:
I have made two files on my own, your code works fine.
You'll need to look for error on part of server php config, or pathways.
Try following: (assuming page1.php and page2.php are in root folder of your site)
in html
<form action="/page1.php" ..
in page1
header("Location: /page2.php");

Try isset($_POST['submit']) in place of isset($_POST['date']). I think you are not filling date.

May be your temp folder is write protected. Check session_save_path() is writable or not.

Related

Pass value from form to alter file PHP without POST

I´m trying to pass ALTER a value from my database with a form.
Because of the POST action, I putted the n_processo (primarykey) on the form so the POST can pass it to the file and edit the table.
I don´t want this value to appear on the table (Form), because it´s readonly, so that the POST can see it and edit the values.
Does anyone know how to change that?
Form file:
<form action="alterar_aluno3.php" method="POST">
<table>
<tr>
<th width="15%">Nº de Aluno</th>
<td><input type="text" maxlength="5" class="input-field4" name="teste" readonly value="<?php echo $idaluno;?>"/></td>
</tr>
<tr>
<th width="20%">Pessoas com quem vive:</td>
<td><input type="text" class="input-field4" name="agregado_existente" value="<?php echo $agregado_existente;?>"/></td>
</tr>
</table>
<p align=right>
<!--alterar este botao -->
<button type="submit" value="Alterar">Alterar</button>
<button type="cancel" onclick="window.location='http://donutsrool.pt/ficha_aluno.php';return false;">Cancelar</button>
</p>
</form>
Insert file:
<?php
include "functions.php";
session_start();
//captar os dados recebidos do formulário com o método POST
$idaluno1=$_POST['teste'];
$agregado_existente = $_POST['agregado_existente'];
$altera="UPDATE aluno SET `agregado_existente`='$agregado_existente' WHERE `n_processo`=$idaluno1;";
$resultado =DBExecute($altera);
header("Location: ficha_aluno.php");
?>
You can put id in a hidden field instead of text field.
<input type="hidden" maxlength="5" class="input-field4" name="teste" value="<?php echo $idaluno;?>"/>
Hidden fields work the same as of text fields.
Their value can only be set with PHP on page load or through JavaScript events.
Only difference is that they are not visible on browser.
Though, they can be seen in View Source of the page.
Try this:
<?php
include "functions.php";
session_start();
//captar os dados recebidos do formulário com o método POST
$idaluno1=$_POST['teste'];
$agregado_existente = $_POST['agregado_existente'];
$altera="UPDATE aluno SET `agregado_existente`='$agregado_existente' WHERE `n_processo`='$idaluno1'";
$resultado =DBExecute($altera);
header("Location: ficha_aluno.php");
?>

How to pass variable from POST

I want to pass two variable from POST, one is the text I write and the other one is the result of a query with I already have.But for some reason I am not getting the variable values. Can you help me?
This is my first page:
<form method="post" action="EliminarGrupos.php">
<label for="nomegrupo"><b>Editar nome do grupo 1 :</label</b><br>
<?php
while ($row = mysqli_fetch_array($result66)){
$result = $row['titulogrupo'];
$_POST['nomegrupo'] = $result; //saving first variable
?>
<input type="text" placeholder="<?php echo $result?>" name="grupo1" id="velhas"></td> //saving second variable
<?php } ?>
<input type="submit" name="submit_x" data-inline="true" value="Submeter">
</form>
This is my second page where I want the variables to appear
$variable = $_POST['nomegrupo'];
$variable2 = $_POST['grupo1'];
The placeholder attribute is for display purposes only. You need to set the value attribute to have it sent to the server.
To send a second value, just use a second <input> element. If you don't want it visible, set type attribute to hidden.
In addition, you are expecting an associative array from mysqli_fetch_array() which is not going to happen. Your HTML had a number of errors in it, which I think I've fixed below. You always need to escape output with htmlspecialchars(). You should separate your HTML and your PHP as much as possible.
<?php
$row = mysqli_fetch_assoc($result66);
$titulogrupo = htmlspecialchars($row["titulogrupo"]);
?>
<form method="post" action="EliminarGrupos.php">
<label for="velhas"><b>Editar nome do grupo 1 :</b></label><br/>
<input type="text" placeholder="" name="grupo1" id="velhas"/>
<input type="hidden" name="nomegrupo" value="<?=$titulogrupo?>"/>
<button type="submit" name="submit_x" data-inline="true">Submeter</button>
</form>
You get the $_POST data from the form submission, specfically from the name attributes. This is what gives the $_POST its information, which it retrieves from value, not placeholder, as you have it now.
<input name="grupo1" value="one"> will make $_POST['grupo1'] equal to one.
You also shouldn't set the $_POST variable on page 1 as you are currently doing, and should make the unchanged variable from the database call a hidden field:
Page 1:
<form method="post" action="EliminarGrupos.php">
<label for="nomegrupo"><b>Editar nome do grupo 1 :</label>
<?php
while ($row = mysqli_fetch_array($result66)){
$result = $row['titulogrupo'];
?>
<input type="text" value="<?php echo $result; ?>" name="grupo1" id="grupo1">
<input type="hidden" value="<?php echo $result; ?>" name="titlogrupo" id="titlogrupo">
<?php } ?>
<input type="submit" name="submit_x" data-inline="true" value="Submeter">
</form>
Page 2:
$variable1 = $_POST['titulogrupo']; // $row['titulogrupo']
$variable2 = $_POST['grupo1']; // Form input
Hope this helps! :)

Php POST method to 2 page

I just sent data to a page called diak_o.php with post method but I need to use this data on an another page. How can I send it to two pages or send from the first page to the next?
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" />
</form>
You could store it in Sessions and access it on multiple pages like this:
Page 1:
<form action="page2.php" method="post">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
session_start();
$dataFromPage1 = $_SESSION['page1text'] = $_POST['page1text'];
echo $dataFromPage1;
?>
You can use $_SESSION or just but i think $_POST should still work in the next file too...
when you send that data from this page to second page like diak_o.php in this page you can access it by below code.
in diak_o.php write code like below.
<?PHP
session_start();
echo $_POST['name'];
$_SESSION["name"] = $_POST['name'];
?>
in the other page you can use $_SESSION["name"] by accessing it.
you can also use COOKIE OF PHP.
On this URL there are different methods for passing data from one page to another.
http://www.discussdesk.com/how-to-get-data-from-one-page-to-another-page-in-php.htm
Thanks.
You need to give your button a name attribute, then on diak_o.php you check if the button isset, after that you check if the text input is not empty, else assign a session to the text input
Your Form
<form action="diak_o.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Bejelentkezés" name="submit" />
</form>
diak_o.php
<?php
session_start();
if(isset($_POST['submit'])){
if(empty($_POST['name'])){
die("enter name");
}else{
$_SESSION['name']= $_POST['name'];
}
}
?>
anotherpage.php
<?php
session_start();
echo $_SESSION['name']; // will output the value from form.
?>
when ever your wanna use the value on your pages, just use $_SESSION['name'];

Form with random number as validation

The following PHP code is for generating a random number of four digits ($numero) and using it as a validation for a simple HTML form with three input boxes. The last input box is for entering the code (the random number). If the user doesn't write the right number in that last input box the program skips its purpose, which is adding some text to a database (agregar.txt). I think the code is fine, except for if ($_POST['password'] != $numero) {. Should I change it to a string or use another kind of variable? Each time I run the code it acts as if $numero was different from password, and I'm sure I'm writing the right number. Please some help.
<html>
<body>
<center>
<h2>Agregar entradas a diccionarioie</h2>
<?php
// RANDOM FOUR DIGITS NUMBER
$numero = rand(1000, 9999);
echo "<b>Código: </b><big>".$numero."</big><p>";
if ($_POST['password'] != $numero) {
?>
<form name="form" method="post" action="">
<input title=" LEMA " size="30" type="text" name="lema" autofocus><br>
<input title=" TRADUCCIÓN " size="30" type="text" name="trad"><br>
<input title=" CÓDIGO " size="30" type="text" name="password"><br>
Gracias por colaborar <input title=" ENVIAR " type="submit" value="•"></form>
<?php
} else {
$lema = $_POST['lema'];
$trad = $_POST['trad'];
// ADDING TEXT TO A DATABASE
$texto = $lema." __ ".$trad."\n";
$docu = fopen("agregar.txt", "a+");
fwrite($docu, $texto);
fclose($docu);
}
?>
</center>
</body>
</html>
As pointed out by #Fred -ii-, the problem in your code is the $numero get generated to different random number when you submit the form. The solution is to use session: PHP session example
The session can be used to store your $numero value after the form being submitted. Here's the updated code:
<?php
// Make sure to start the session before any output.
session_start();
if (isset($_POST['password']) && isset($_SESSION['numero']) && $_POST['password'] == $_SESSION['numero']) {
unset($_SESSION['numero']);
$lema = $_POST['lema'];
$trad = $_POST['trad'];
// ADDING TEXT TO A DATABASE
$texto = $lema." __ ".$trad."\n";
$docu = fopen("agregar.txt", "a+");
fwrite($docu, $texto);
fclose($docu);
} else {
// RANDOM FOUR DIGITS NUMBER & STORE IT IN SESSION.
$numero = $_SESSION['numero'] = rand(1000, 9999);
?>
<html>
<body>
<center>
<h2>Agregar entradas a diccionarioie</h2>
<b>Código: </b><big><?php echo $numero; ?></big><p>
<form name="form" method="post" action="">
<input title="LEMA " size="30" type="text" name="lema" autofocus><br>
<input title="TRADUCCIÓN " size="30" type="text" name="trad"><br>
<input title="CÓDIGO " size="30" type="text" name="password"><br>
Gracias por colaborar <input title=" ENVIAR " type="submit" value="•">
</form>
</center>
</body>
</html>
<?php }
Just make sure that you call session_start() before any output (in your case the HTML document).
Hope this help.

php transfer variables from page to another pages

i have 4 pages like below:
page1
session_start();
<input type="text" name="log1" value="".$_SESSION["log1"]."">
page2
session_start();
$_SESSION["log1"] = $_POST["log1"]
<input type="text" name="log2" value="".$_SESSION["log2"]."">
page3
session_start()
$_SESSION["log2"] = $_POST["log2"]
<input type="text" name="log2" value="".$_SESSION["log3"]."">
page4
session_start();
$_SESSION["log3"] = $_POST["log3"]
<input type="text" name="log2" value="".$_SESSION["log4"]."">
i transfer with method post in order page1 > page2 > page3 > page4 . if i return to page2, i not have $_SESSION["log2"] in value of input And displays it an empty field.
Where is the problem?
You're using bad syntax for using PHP with HTML. Instead of:
session_start();
$_SESSION["log1"] = $_POST["log1"]
<input type="text" name="log2" value="".$_SESSION["log2"]."">
Use:
<?php
session_start();
$_SESSION["log1"] = $_POST["log1"];
?>
<input type="text" name="log2" value="<?php echo $_SESSION["log2"]; ?>">
Also make sure to use form to post data to another page to set session variables.

Categories