How to pass variable Session in a Hidden input in php

Asked

Viewed 1,589 times

0

I have a form and need to pass the value of the session via post method. I’m using the code below, but it’s not working.

<input type="hidden"  name="cod_usuario" value="<?php session_start(); $_SESSION['login']; ?>" class="input-medium" id="txtFuncionario" title="Nome do Funcionário"/>
  • 1

    Obs: session_start(); has to be at the beginning of the file by default... put the code every pf friend

  • Thank you. The code is too big. I put it on http://jsfiddle.net/2L7zLmoc/

  • The text is on line 100.

  • Doesn’t work like? Returns an error? It’s empty? What’s the point of sending the session id in a POST? Explain better, the way you’re alone you understand.

  • I need to pass the user who is logged in. It is getting blank.

  • Why don’t you do it in "Formcados.php" (It’s in your form action)? Note: whenever you refer to a user in the comments, use the @ (arroba), a list will appear with the names of who commented, choose the recipient. Only then will the user receive an alert and realize that you have made reference to what he commented.

  • @Philip. It may be that way, but I tested it and it wasn’t working. I perform the test by passing 1 straight to variable and the system entered into the database normally. Then I put the below and did not insert anything into the bank. '<?php&#xA;session_start();&#xA;&#xA; require '../php/funcao_mysql.php';&#xA; &#xA;$cod_os = $_POST['N_OS']; //1 - Número da OS&#xA;$cod_usuario = $_SESSION['login'];'

Show 2 more comments

1 answer

1


First place at the beginning of your file.

<?php
session_start();
?>

Second, as much as it is a session variable, for it to be populated in a form field, it is necessary to give a ECHO in it. In your case:

<input type="hidden" id="txtFuncionario" 
name="cod_usuario" value=" <?php echo $_SESSION['login']; ?>" >
  • @Filipe. I performed the tests with the above guidance and it did not work.

  • Fabricio, check whether Voce set the session value correctly, on this form page, tries to do the following: from a 'echo var_dump($_SESSION);' after session_start(); and post here the result... REMEMBERING... AND NO LESS IMPORTANT session_start(); it has to be on every page, or php files that you need to check or transport the session variables

  • I did what you asked and returned the result below: 'array(2) { ["login"]=> string(6) "djalma" ["senha"]=> string(3) "123" }' Strange that does not record in the bank but if I put '$cod_usuario = 1;' will record.

  • Well, re-capitulating, Voce da um echo var_dump($_SESSION); in the form wherever the login appears in an Hidden field, and this appears: array(2) { ["login"]=> string(6) "djalma" ["senha"]=> string(3) "123" } correct? And when Voce gives an echo in the variable $_SESSION['login']; nothing appears?

  • Sorry @Marcelo. Printed the previous result and printed the name of the logged-in only not saved in the database. I am using mysqli. Do you have any idea what it might be? ;'echo var_dump($_SESSION); &#xA;echo "<br />";&#xA;echo $_SESSION['login'];'

  • Is that another question within the scope of that question? if not I recommend creating a new topic exposing what the problem is happening now, in more detail and more openly... it is probably related to the way that Voce ta trying to save...

  • Which question? Do you have any idea what it might be? I’m sorry, but I really don’t know and I think people are looking for the forum, not for someone to do it for you, but because they’ve run out of attempts and he can’t. If I have a form and I open a post because of a problem in the form and it does not save in the bank, I do not see the need for a new post.

  • 1

    Old, the initial subject was "How to pass variableSession in an input of type Hidden in php", apparently this problem has already been solved... Oce has another problem in the form, nothing fairer than to create another doubt that has a subject but what it seeks.... This here ends up becoming off-topic subject

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.