使用全局数组存储来自表单函数的数据并在另一个函数中访问数组

问题描述 投票:0回答:0

所以我正在为一个学校项目创建一个网站,我有一个表单可以提交用户输入的数据,将其格式化为变量,并将结果传递给模态。

我试图将变量传递给一个全局数组,这样用户就可以按下一个按钮并调用另一个函数来在一个单独的模式中显示该数组。如果我在第一个函数中显示它,该数组将显示正常。但是第一个函数只是用户端的,第二个函数是供后端访问的。我想我已经将范围缩小到我将数据推送到数组的方式,但我仍然不确定。帮助!

JS:

`。 //存放客户信息和预约时间的数组 常量保留 = []; 最终变种;

// Function to return contact form information back to the user in a popup window
function target_popup(form) 
{
    var custFName = document.getElementById('FName').value;
    var custLName = document.getElementById('LName').value;
        var peopleAmt = document.getElementById('People').value;
    var dateTime = document.getElementById('Date').value;
    var message = document.getElementById('Message').value;
    
    // Convert Date to local time
    var convDate = new Date(dateTime);
    
    
    //Convert First and Last names to proper formatting
    var fNameU = custFName.toUpperCase().substr(0,1);
    var fNameL = custFName.toLowerCase().substr(1);
    var fName = fNameU + fNameL;
    
    var lNameU = custLName.toUpperCase().substr(0,1);
    var lNameL = custLName.toLowerCase().substr(1);
    var lName = lNameU + lNameL;
    
    
    // Display final message
    final = "Name: " + fName + " " + lName + '\n' + " Amount of people: " +   peopleAmt + '\n' + " Date and time of reservation: " + convDate.toLocaleString() + '\n' + " Additional details: " + message;
    document.getElementById('spanResult').textContent = final;
    
    //Assign customer information and reservation time to the array
    reservations.push(final);
}


// Function to print array
function printArray()
{
    document.getElementById('spanResult1').textContent = reservations;
}

`

arrays function modal-dialog global-variables
© www.soinside.com 2019 - 2024. All rights reserved.