Transfer variable javascript to php

  ajax, html, javascript, php, variables

I am coding a reservation system. I make my links appear in javascript and when I press the next button I have a function which allows to add +7 (days) to my variable. The problem is that this is done in javascript and I would like to retrieve the data in php.

let d = 25;
let m = 08;
let a = 2021;
let date = {"jour":d, "mois":m, "annee":a};
 
const suivant = document.querySelector("#btn-date-suivant");
const precedent = document.querySelector("#btn-date-precedent");
const marcillac = document.querySelector("#pills-marcillac-tab");
 
const dText = document.querySelector("#d-text");
const lien_marcillac= document.getElementById("lien_marcillac");
 
const writemarcillac = () => {
    dText.innerHTML = d + "/" + m + "/" + a;
    lien_marcillac.innerHTML = '<div id="lien_1" class="lien_horaire " style="width: 940px; margin: 0 auto;"><a id="lien_id" href="forms.php?id=' + d + '-' + m + "-" + a + ' 15:00 marcillac">15:00</a></div><div id="lien_2" class="lien_horaire " style="width: 940px; margin: 0 auto;"><a href="forms.php?id=' + d + '-' + m + "-" + a + ' 15:15 marcillac">15:15</a></div><div id="lien_3" class="lien_horaire " style="width: 940px; margin: 0 auto;"><a href="forms.php?id=' + d + '-' + m + "-" + a + ' 15:30 marcillac">15:30</a></div><div id="lien_4" class="lien_horaire " style="width: 940px; margin: 0 auto;"><a href="forms.php?id=' + d + '-' + m + "-" + a + ' 15:45 marcillac">15:45</a></div><div id="lien_5" class="lien_horaire " style="width: 940px; margin: 0 auto;"><a href="forms.php?id=' + d + '-' + m + "-" + a + ' 16:00 marcillac">16:00</a></div><div id="lien_6" class="lien_horaire " style="width: 940px; margin: 0 auto;"><a href="forms.php?id=' + d + '-' + m + "-" + a + ' 16:15 marcillac">16:15</a></div><div id="lien_7" class="lien_horaire " style="width: 940px; margin: 0 auto;"><a href="forms.php?id=' + d + '-' + m + "-" + a + ' 16:30 marcillac">16:30</a></div>'
}
 
 
 
const write = () => {
    if (marcillac.classList.contains("active")){
        writemarcillac();
    }
}
 
const datesuivant = () => {
    d += 7;
    if ( m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
        if ( d > 31){
            m += 1;
            d -= 31;
        }
        if ( m > 12){
            a += 1;
            m = 1;
        }
    }else if(m == 4 || m == 6 || m == 9 || m == 11){
        if ( d > 30){
            m += 1;
            d -= 30;
        }
        if ( m > 12){
            a += 1;
            m = 1;
        }
    }else if(m == 2){
        if ( d > 28){
            m += 1;
            d -= 28;
        }
        if ( m > 12){
            a += 1;
            m = 1;
        }
    }
    write();   
}
 
const dateprecedent = () => {
    d -= 7;
    if ( m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12){
        if ( d < 1){
            m -= 1;
            d += 31;
        }
        if ( m < 1){
            a -= 1;
            m = 12;
        }
    }else if(m == 4 || m == 6 || m == 9 || m == 11){
        if ( d < 1){
            m -= 1;
            d += 30;
        }
        if ( m < 1){
            a -= 1;
            m = 12;
        }
    }else if(m == 2){
        if ( d < 1){
            m -= 1;
            d += 28;
        }
        if ( m < 1){
            a -= 1;
            m = 12;
        }
    }
    write();
}
 
write();
 
 
suivant.addEventListener("click", datesuivant);
precedent.addEventListener("click", dateprecedent)

function ajaxPost(url, data, callback) {
    var req = new XMLHttpRequest();
    req.open("POST", url);
    req.onreadystatechange = function () {
        if (this.readyState == XMLHttpRequest.DONE && this.status == 200) {
            callback(req.responseText);
        }
    };
        req.setRequestHeader("Content-Type", "application/json");
        data = JSON.stringify(data);
    req.send(data);
}



ajaxPost("http://localhost/planning%20amiante%20brut/index.php", date,
    function (reponse) {
        date = json.stringify(reponse)
        console.log(date) 
    }
);

and in my file.php i got this

 <?php
       $d = json_decode($_POST["jour"]);
            echo $d;
 ?>

But in my file.php it says that "date" is an undefined index. Could you help me please, i’m lost :’)
Sorry for my english i’m french !
Thank you

Source: Ask PHP

LEAVE A COMMENT