Manage session as array

0

I want to know if there is any way I can save two values in the same session variable as if it were an array, since I want to save both the input text value and the wysiwyg element by clicking the "save chapter" button in the image. , I tried to do 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 display on the screen, if I save that way, on the screen at the time I'm going to display, only the index letter appears that I put in the session instead of the whole 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 arrive 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:

    
asked by anonymous 21.02.2016 / 03:49

1 answer

0

$ _ SESSION is a SUPERGLOBAL variable and allows you to add multi-threading 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();

?>

By analyzing your code, the PHP part of JQUERY is ok. I noticed that you're taking the get and not treating. SESSION does not have a numbered index if your $ _GET ['p'] , for number will give error ( see documentation ).

Face only to disengage, change:

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

for

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

and test if running is what I noticed wrong.

    
21.02.2016 / 05:01