Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
access PHP session value in other file's javascript

I'm trying to do a modal on a PHP file and I'm managing the form from the modal on another javascript file.

My question is: How can I access a session value in that javascript file.

Here is my HTML code on a php file:

<dialog class="modal" id="modal">
        <div class=modal-header>
            <h1>Insert a term</h1>
        </div>
        <form class="form" method="dialog">
            <label>Title</label>
            <input type="text" id="title">
            <label>Description</label>
            <input type="text" id="description" placeholder="(Max 140 characters)">
            <div class="btn2-group">
                <button class="button" id="submitForm" type="submit">submit form</button>
                <button class="close button"><b>Close</b></button>
            </div>
        </form>
    </dialog>

And here is my modal.js :

const modal = document.querySelector("#modal");
const openModal = document.querySelector(".open-button");
const closeModal = document.querySelector(".close");
const submitForm = document.querySelector("#submitForm")

openModal.addEventListener("click", () => {
    modal.showModal();
});

closeModal.addEventListener("click", () => {
    modal.close();
});

submitForm.addEventListener("click", () => {
    var title = document.getElementById("title").value;
    var desc = document.getElementById("description").value;
    var x = "<?=$_SESSION['user_id']?>";
    if (title && desc) {
        alert(x);
    } else {
        print("Please fill everything up!");
    }
 })
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Generate the JS file from PHP, call it modal.js.php.

<?php
session_start();
header("Content-type: text/javascript");
?>
const modal = document.querySelector("#modal");
const openModal = document.querySelector(".open-button");
const closeModal = document.querySelector(".close");
const submitForm = document.querySelector("#submitForm")

openModal.addEventListener("click", () => {
    modal.showModal();
});

closeModal.addEventListener("click", () => {
    modal.close();
});

submitForm.addEventListener("click", () => {
    var title = document.getElementById("title").value;
    var desc = document.getElementById("description").value;
    var x = <?= json_encode($_SESSION['user_id']) ?>;
    if (title && desc) {
        alert(x);
    } else {
        print("Please fill everything up!");
    }
 })
about 4 years ago · Juan Pablo Isaza Relatório

0

As I suggested in my comment, I would try to add a hidden input to PHP HTML code like that:

<dialog class="modal" id="modal">
    <div class=modal-header>
        <h1>Insert a term</h1>
    </div>
    <form class="form" method="dialog">
        <label>Title</label>
        <input type="text" id="title">
        <label>Description</label>
        <input type="text" id="description" placeholder="(Max 140 characters)">
        <input type="hidden" id="user-id" name="userId" value="<?=$_SESSION['user_id']?>">
        <div class="btn2-group">
            <button class="button" id="submitForm" type="submit">submit form</button>
            <button class="close button"><b>Close</b></button>
        </div>
    </form>
</dialog>

And then get this by JS like that:

const modal = document.querySelector("#modal");
const openModal = document.querySelector(".open-button");
const closeModal = document.querySelector(".close");
const submitForm = document.querySelector("#submitForm");
const userID = document.getElementById("user-id").value;

openModal.addEventListener("click", () => {
    modal.showModal();
});

closeModal.addEventListener("click", () => {
    modal.close();
});

submitForm.addEventListener("click", () => {
    var title = document.getElementById("title").value;
    var desc = document.getElementById("description").value;
    if (title && desc) {
        alert(userID);
    } else {
        print("Please fill everything up!");
    }
})

Of course you could get the hidden input value in different ways, like:

const userID = document.getElementsByName("userId")[0];

UPDATE:

Well, I agree with the arguments of M. Eriksson in the comments. Maybe the logic behind this code may should be reconsidered.

about 4 years ago · Juan Pablo Isaza Relatório

0

Here's my sggestion:

Don't output the user id in the JS and remove it from the form altogether.

If you know it suppose to be the current user, then fetch the user id from the session in PHP on the page that receives the form request instead. You don't need to send it to the client just to get it back in PHP.

If you do send it from the client, you still need to validate/verify the id since you should never ever trust trust data you get from the client. Anyone can modify that ID in their client/browser and send what ever they want.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda