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!");
}
})
<?=$_SESSION['user_id']?>as a value and then get the hidden input value with JS?document.getElementByIdas well?var x = "<?=$_SESSION['user_id']?>";at all in your JS? If you need to call the back end, you can get that value there instead.