Session passing value that was not expected

Asked

Viewed 51 times

0

Hello, I’m a programmer and I’m making a system for the school I studied, but I found a big problem.

School System

<h1 id="titulo">Sistema Escolar</h1>

    <div>
        <ul id="menu">
            <a href="inicio2.html"><li>Página inicial</li></a>
            <li>Informativos</li>
            <li>Bio dos Educadores</li>
            <li>Cursos</li>
        </ul>
    </div>

<div id="lateral">

    <img src="imagens/BrasaoDoCearaPeq.png"/>
    <h4>EEEP SALOMÃO ALVES DE MOURA</h4>

    <h2>Ranking das Salas</h2>
    <ol id="ranking">
        <li>Informática</li>
        <li>Redes</li>
        <li>Edificações</li>
        <li>Comércio</li>
        <li>Secretariado</li>
    </ol>

</div>

<div id="corpo">
    <h1>Selecione o curso desejado</h1>

    <div id="primeiroAno">
        <h3>1º Ano</h3>         
        <ul>    
            <a href="cadAluno.html"><li class="informatica" onclick="<?php $_SESSION['selctcurso'] = 'informatica1'; ?>"><img src="imagens/icones/informatica.png"><br/>Informática</li></a>
            <a href="cadAluno.html"><li class="edificacoes" onclick="<?php $_SESSION['selctcurso'] = 'edificacoes1'; ?>"><img src="imagens/icones/edificacoes.png"/><br/>Edificações</li></a>
            <a href="cadAluno.html"><li class="redes" onclick="<?php $_SESSION['selctcurso'] = 'redes1'; ?>"><img src="imagens/icones/redes.png"/><br/>Redes</li></a>
            <a href="cadAluno.html"><li class="logistica" onclick="<?php $_SESSION['selctcurso'] = 'logistica1'; ?>"><img src="imagens/icones/logistica.png"/><br/>Logística</li></a>
        </ul>
    </div>

    <div id="segundoAno">
        <h3>2º Ano</h3>
        <ul>
            <a href="cadAluno.html"><li class="informatica" onclick="<?php $_SESSION['selctcurso'] = 'informatica2'; ?>"><img src="imagens/icones/informatica.png"><br/>Informática</li></a>
            <a href="cadAluno.html"><li class="comercio" onclick="<?php $_SESSION['selctcurso'] = 'comercio2'; ?>"><img src="imagens/icones/comercio.png"/><br/>Comércio</li></a>
            <a href="cadAluno.html"><li class="redes" onclick="<?php $_SESSION['selctcurso'] = 'redes2'; ?>"><img src="imagens/icones/redes.png"/><br/>Redes</li></a>
            <a href="cadAluno.html"><li class="logistica" onclick="<?php $_SESSION['selctcurso'] = 'logistica2'; ?>"><img src="imagens/icones/logistica.png"/><br/>Logística</li>
        </ul></a>
    </div>

    <div id="terceiroAno">
        <h3>3º Ano</h3>
        <ul>
            <a href="cadAluno.html"><li class="edificacoes" onclick="<?php $_SESSION['selctcurso'] = 'edificações3'; ?>"><img src="imagens/icones/edificacoes.png"><br/>Edificações</li></a>
            <a href="cadAluno.html"><li class="comercio" onclick="<?php $_SESSION['selctcurso'] = 'comercio3'; ?>"><img src="imagens/icones/comercio.png"/><br/>Comércio</li></a>
            <a href="cadAluno.html"><li class="redes" onclick="<?php $_SESSION['selctcurso'] = 'redes3'; ?>"><img src="imagens/icones/redes.png"/><br/>Redes</li></a>
            <a href="cadAluno.html"><li class="secretariado" onclick="<?php $_SESSION['selctcurso'] = 'secretariado3'; ?>"><img src="imagens/icones/secretariado.png"/><br/>Secretariado</li></a>
        </ul>
    </div>

</div>

This is the code of the chosen file.php the problem is when the teacher will register a student he has to pass this screen to select the course when he makes the registration always the course is saved in the database as Secretariat 3 which is the last course, but it is not to happen that, he has to click on a course and the course he clicked is to be saved along with the student. Could a more experienced php programmer help me ? Already I appreciate the help of all

  • Related: https://answall.com/questions/10424/como-executar-arquivo-php-a-partir-de-fun%C3%A7%C3%A3o-em-javascript, https://answall.com/questions/36709/%C3%89-poss%C3%Advel-rodar-uma-fun%C3%A7%C3%A3o-javascript-pelo-php

  • Morpheus, PHP does not run parallel to HTML and Javascript, what PHP does is process on the server and return the download of an HTML that is later rendered and executed by the server, this is how the HTTP process occurs with the client (browser), web is this HTTP, HTML would only communicate with PHP directly if it were the Delorean... So the only approach that will work in your case is Ajax... I put some links at the top of your question, there are several examples of approaches you can try.

  • vlw man, you helped me a lot, I’ll take a look at how ajax works to try to solve this problem.

1 answer

2

The code makes no sense since the PHP will run everything and send to client.

When you do:

<li class="secretariado" onclick="<?php $_SESSION['selctcurso'] = 'secretariado3'; ?>">

It’s the same as doing:

QualquerCoisa<?php $_SESSION['selctcurso'] = 'secretariado3'; ?>TalvezOutraCoisa

PHP won’t understand anything about HTML, it doesn’t even know it exists. O onclick= is the same as Javascript .addEventListener("click", ...), this will only run on the client, but PHP (the $_SESSION['selctcurso']) is executed before, on your server.


It is more than logical that in normal situations if you are setting N * $_SESSION['selctcurso'], only the last is the one that will keep.


If you want a session value to be set when clicking, you can simply make a new request. On this requested page you set the session.

  • vlw for the answer, but it didn’t help so much if you could show me a hint of how I could do it I would appreciate it.

Browser other questions tagged

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