-1

I would like to record the data of a form in a session

I find in the documentation that I have to use: Session::put('key', 'value'); but I do not know where to place this piece of code.

knowing that my form is located in the form_commun_1.htm page.

this is all the page :

title = "formulaire_commun_1"
url = "/formulaire_commun_1"
layout = "sidebar_layout"
is_hidden = 0
==





 <ul class="progressbar">
 <li class="active">transaction</li>
 <li>details</li>
 <li>confirmation</li>
 </ul>

 <br><br><br><br>




 <form method="POST" action=""  name="formu" accept-charset="UTF8"  enctype="multipart/form-data">
        <input type="hidden" name="_handler" value="onUpdate">
                {{form_token()}}
                {{form_sessionkey()}}
<div class="form-group">
  <label for="TYPE_transaction" class="col-sm-3">type de transaction:</label>
   <div class="col-sm-9">
     <select class="form-control" id="TYPE_transaction">
    <option>vendre</option>
    <option>louer</option>

  </select>
    </div>
    <br><br><br>


 <p>choisissez un type de bien:</p>
 <br><br>

<div class="col-sm-offset-2 col-sm-2">
 <button class="btn btn-default"   onclick="type_bien(0)"><img src="{{ 'assets/img/icones/icone_maison.png'|theme }}"></button>
  </div>
 <div class="col-sm-offset-2 col-sm-2">

 <button class="btn btn-default"   onclick="type_bien(1)"><img src="{{ 'assets/img/icones/icone_villa.jpg'|theme }}"></button>

 </div>
 <div class="col-sm-offset-2 col-sm-2">
 <button class="btn btn-default"   onclick="type_bien(2)"><img src="{{ 'assets/img/icones/icone_riad.jpg'|theme }}"></button>

 </div>


<br><br><br><br><br>

<div class="col-sm-offset-2 col-sm-2">
 <button class="btn btn-default"   onclick="type_bien(3)"><img src="{{ 'assets/img/icones/icone_appartement.png'|theme }}"></button>

  </div>
 <div class="col-sm-offset-2 col-sm-2">

 <button class="btn btn-default"   onclick="type_bien(4)"><img src="{{ 'assets/img/icones/icone_ferme.png'|theme }}"></button>

 </div>
 <div class="col-sm-offset-2 col-sm-2">
 <button class="btn btn-default"   onclick="type_bien(5)"><img src="{{ 'assets/img/icones/icone_magasin.jpg'|theme }}"></button>


 </div>

 <br><br><br><br>



 <div class="radio"   style="display: none;" >
<label><input type="radio" name="radio_type_bien" id=0  value="maison" ></label>

 </div>
  <div class="radio" >
 <label><input type="radio" name="radio_type_bien" id=1  value="villa"></label>

 </div>
  <div class="radio" >
<label><input type="radio" name="radio_type_bien" id=2 value="riad" ></label>

 </div>
  <div class="radio" >
<label><input type="radio" name="radio_type_bien" id=3  value="appartement" ></label>

 </div>
  <div class="radio" >
<label><input type="radio" name="radio_type_bien" id=4  value="formulaire_appartement" ></label>

 </div>
  <div class="radio" >
<label><input type="radio" name="radio_type_bien" id=5 value="magasin" ></label>

 </div>


<br><br><br><br><br><br



<div class=" col-sm-offset-3 col-sm-9">

 <button type="submit" class="btn btn-default">Register</button>


</div></div>


</form> 


      <script type = "text/javascript">


function type_bien(x){

    switch( x) {
    case 0:


        document.getElementById(0).checked="true";
        document.forms["formu"].action="formulaire_villa";


        break;
    case 1:

        document.getElementById(1).checked="true";
        document.forms["formu"].action="formulaire_villa";


        break;
    case 2:

        document.getElementById(2).checked="true";
        document.forms["formu"].action="formulaie_riad";

        break;

    case 3:


        document.getElementById(3).checked="true";
        document.forms["formu"].action="formulaire_appartement";


        break;
    case 4:

        document.getElementById(4).checked="true";

        break;
    case 5:

        document.getElementById(5).checked="true";

        break;
    default:
         alert('local_commerce est selected');
}

}
      </script>

do I have to go through creating a plugin or component ? and where to put the session and how to use it?

please help?thank you

Fati Alami
  • 51
  • 11

1 Answers1

1

I would recommend creating a plugin and component, but this isn't 100% necessary in your case.

Here would be a basic self-contained page that would do what you are requesting:

title = "test"
url = "/test"
layout = "default"
is_hidden = 0
==
<?php
function onStart()
{
    $this['example'] = Session::get('example-key');
}

function onTest()
{
    Session::put('example-key', input('my-input'));
}

?>
==

<form data-request="onTest" data-request-success="location.reload();">
    <input type="text" name="my-input">
    <button type="submit">Submit</button>
</form>

{% if example %}
    <strong>Example: {{ example }}</strong>
{% endif %}

I would recommend reading through the OctoberCMS documentation, it is excellent.

Joseph Oppegaard
  • 611
  • 5
  • 12
  • i have a question please: why did you chose to put **session::put** in the **ontest()** function ,can you please explain the difference between the too functions? – Fati Alami Nov 15 '18 at 10:54
  • Because of this `data-request="onTest"` . It will create session after clicking on submit button and when the next time page reloads, it will show your entered value. You can follow this article for more information - https://octobercms.com/docs/cms/pages – Mittul At TechnoBrave Nov 15 '18 at 13:05
  • another problem please :is that i don't khow what to do with the form's action where will the form submit redirect;i don't want to refresh the page i want it to redirect to another page can help please – Fati Alami Nov 15 '18 at 14:04
  • Take a look at the Redirects section of the documentation: https://octobercms.com/docs/services/response-view#redirects – Joseph Oppegaard Nov 15 '18 at 17:41
  • yes I found that but I do not know where to put the code exactly ?? – Fati Alami Nov 16 '18 at 10:37