Manage Session as an array

Asked

Viewed 1,062 times

0

I wanted to know if there is any way for me to save two values in the same variable Session as if it were an array, because I want to save both the value of input text and the wysiwyg element by clicking the "save chapter" button on the image, I tried to save it like this:

<?php
    session_start();
    $titulo  = isset($_POST['titulo']) ? $_POST['titulo'] : '';
    $paragrafo  = isset($_POST['valor_paragrafo']) ? $_POST['valor_paragrafo'] : '';
    $p = $_GET['p'];
    $_SESSION[$p][0] = $titulo;  //aqui salvaria os valores na session
    $_SESSION[$p][1] = $paragrafo;
?>

Only when I go to display on the screen, if I save this way, on the screen at the time I will display, appears only the letter related to the Dice that I put in Sesssion instead of all the text. code to display the values on the screen:

$(document).ready(function() {
  $("#paragrafo").Editor();
  $('#paragrafo').Editor('setText', '<?=addslashes($_SESSION["$p"][1])?>');
  $('#titulo').val('<?=($_SESSION["$p"][0])?>');
});

the first php code posts come from this html and javascript:

function Envia_Form(valor, arquivo, chooser, target){  //valor do chooser: 0 = faz submit com refresh na pagina |  1 = sem refresh na pagina
  $("#valor_paragrafo").val(valor);
  if(chooser == 0){
    $("#form").attr("action",arquivo);
    if(target != ''){
      $("#form").prop("target", target);
    }
    document.form.submit();
  } else if(chooser == 1){
    $.post(
      arquivo,  
      $('#form').serialize(),
      function( data ){
      }
    );
  }
}

<form id="form" name="form" method="post" action="proc.php">
    <div class="titulo">TÍTULO
        <input type="text" class="titulo-input" name="titulo" id="titulo" placeholder="Digite o texto">
    </div>
    <div class="container-fluid">
        <div class="row">
          <div id="paragrafo" name="paragrafo"></div>
          <input type="hidden" id="valor_paragrafo" name="valor_paragrafo" />
        </div>
    <p>
        <br>
        <input type="button" value="Salvar Capítulo" onclick="Envia_Form($('#paragrafo').Editor('getText'), 'salva_paragrafo.php?p=<?=$p?>', 1);" />
       <input type="button" value="Gerar PDF" onclick="Envia_Form($('#paragrafo').Editor('getText'), 'proc.php', 0, '_blank');" />
   </p>

the system: inserir a descrição da imagem aqui

1 answer

0


Yes, the $_SESSION is a variable SUPERGLOBAL and allows you to add multimensional arrays.

<?php
#inicia a sessão
session_start();

#atribui as sessões
$_SESSION['aula']['video']  = $video;
$_SESSION['aula']['resumo']  = $resumo;
$_SESSION['aula']['descricao']   = $descricao;

#destroi todas as sessões
session_destroy();

?>

Analyzing your code, the part of PHP and of JQUERY ta ok. I noticed that you’re getting the get and not treating. SESSION has no index numbered if your $_GET['p'], for number will give error (see documentation).

Guy just for kicks, changes:

$('#paragrafo').Editor('setText', '<?=addslashes($_SESSION["$p"][10])?>');

for

$('#paragrafo').Editor('setText', '<?=addslashes($_SESSION["$p"][1])?>');

and test if rotate is what I noticed wrong.

  • but what would I need to do about the get? this 10-1 input I had switched on purpose to see if it changed the value that appeared in the html fields, and really changed, only I keep appearing a single letter :/

  • session_destroy is obsolete, prefers to do something like: $_SESSION['aula'] = NULL;

  • @Guilhermenascimento why session_destroy is obsolete? wouldn’t session_unset be obsolete??

  • @Alexandremartinsmontebelo pardon confused me, the problem is that session_destroy, his problem is that it will destroy variables beyond $_SESSION['aula'], what can affect other system functionalities, the best way is the $_SESSION['aula'] = NULL;, prefer to use the session_destroy if you’re really sure what you’re doing.

  • ah yes, in the system I’m only using session_destroy when the user clicks "exit", yes, there is a login system also in that system

  • @Alexandremartinsmontebelo but that’s what I’m saying, session_destroy will delete everything from that session, if you have other data from other applications that do not need to be logged in to work they will also be removed, summarizing can use, but only when really necessary. The recommended is to use $_SESSION['aula'] = NULL; and then check with if (empty($_SESSION['aula']) === false) { ... } if it is logged in.

  • And then it worked?

Show 2 more comments

Browser other questions tagged

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