在PHP中无法从会话到索引获取值

时间:2019-12-23 09:43:15

标签: javascript php

我在评论中有此链接

此代码中有一个问题:有人可以帮忙吗? 无法打印我有星座运势的索引中的值,但我做了列表,但不想打印该值。当我单击“保存”按钮时,在索引中的日期会在控制台中显示,但在div中不会显示,有人可以帮助我吗?

JavaScript

function add() {
    let url = "./server/addhoroscope.php"
    let method = "POST"
    let formData = new FormData()
    let theDate = document.getElementById('inpdate').value;
    formData.set("date", theDate)
    makeRequest("./server/addhoroscope.php", "POST", formData, (result) => {
        console.log(result)
        // console.log($fromMonthAndDay)
    })
}

function makeRequest(url, method, formData, callback) {
    fetch(url, {
        method: 'post',
        body: formData
    }).then((response) => {
        console.log(response)
        return response.json()
    }).then((result) => {
        callback(result)
    }).catch((err) => {
        console.log("Error: ", err)
    })
};

function showTheDate() {
    let url = "./server/viewHoroscope.php"
    let method = "GET"
    fetch(url, {
        method: method,
    }).then((Response) => {
        return Response.json()
    }).then((body) => {
        if (body) {
            echo("hej");
        } else {
            echo("hej");
        }
        let contant = document.getElementById("outPUT");
        contant.innerText = body;
    }).catch((err) => {})
};

PHP

<?php
session_start();    
if($_SERVER["REQUEST_METHOD"] == 'GET') {
if(isset($_SESSION['horoscope'])) {
    $horos = $_SESSION['horoscope'];    
    echo json_encode($horos);
} else {
    echo json_encode('');}} 
?>

0 个答案:

没有答案