简体   繁体   English

表单数据不保存到文件(通过python中的CGI)

[英]Form data doesn't save to file (through CGI in python)

I made a form in HTML that's supposed to save some data through my university's server:我用 HTML 制作了一个表单,它应该通过我大学的服务器保存一些数据:

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8"/>
    <title>TI 3</title>
    <link rel="stylesheet" href="style_form.css">
    <script type="text/javascript" src="script.js"></script>
</head>
<body>
    <iframe width="0" height="0" border="0" name="dummy" id="dummy"></iframe>

    <h1>TI 3 - form</h1>

    <div class="formSection">
        <form action="../../../cgi-bin/toCsv.py" target="dummy" method="POST" name="studentForm" onsubmit="return validate()">
            <h3>Name: (*required):</h3>
            <input type="text" name="name"/>

            <h3>Surname: (*required):</h3>
            <input type="text" name="surname"/>

            <h3>E-mail address: (*required):</h3>
            <input type="text" name="email"/>

            <h3>University year: (*required):</h3>
            <select name="year" size="1">
                <option value="0">Choose:</option>
                <option value="1">I</option>
                <option value="2">II</option>
                <option value="3">III</option>
                <option value="4">IV</option>
                <option value="5">V</option>
            </select>

            <br>

            <p>
                <input type="submit" name="submitBtn" class="subBt" value="Submit" />
            </p>
        </form>

    </div>

    <hr>

</body>
</html>

from where I call the python script called toCsv.py, that collects the data and is supposed to save them in the CSV file called data.csv:从那里我调用名为 toCsv.py 的 python 脚本,它收集数据并应该将它们保存在名为 data.csv 的 CSV 文件中:

#!/usr/bin/env python

import cgi
import csv

print "Content-Type: text/html"
print 

studForm = cgi.FieldStorage()
name = studForm.getvalue("name")
surname = studForm.getvalue("surname")
email = studForm.getvalue("email")
year = studForm.getvalue("year")

data = [name, surname, email, year]
print "data" 
print data

with open('../4/pd1/3/data.csv', 'a') as file:
    write = csv.writer(file, delimiter=",")
    write.writerow(data)

It seems that it collects and parses data the way it should.它似乎以应有的方式收集和解析数据。 I tried removing the dummy iframe to debug.我尝试删除虚拟 iframe 进行调试。 I filled the form and got the correct result: data ['Robert', 'Friedrich', 'lux@torpe.da', '1'] .我填写了表格并得到了正确的结果: data ['Robert', 'Friedrich', 'lux@torpe.da', '1']

The script saves the data correctly to data.csv, when I call it straight from the UNIX terminal: ./toCsv.py .该脚本将数据正确保存到 data.csv,当我直接从 UNIX 终端调用它时: ./toCsv.py (the data is obiously empty, but the commas are saved). (数据显然是空的,但保存了逗号)。

However, when I submit the form, nothing seems to be saved.但是,当我提交表单时,似乎没有保存任何内容。 What's the problem?有什么问题?

The js script just validates the data and I think it has nothing to do with the issue. js 脚本只是验证数据,我认为它与问题无关。 I'll post it anyway, just to be sure.无论如何我都会发布它,只是为了确定。

function validate(){
    let name = document.studentForm.name.value;
    let surname = document.studentForm.surname.value;
    let email = document.studentForm.email.value;
    let year = document.studentForm.year.value;

    let valid = 1;
    let errorMessage = "";

    if (name.length === 0){
        valid = 0;
        document.studentForm.name.style.backgroundColor = 'red';
        errorMessage += "No name!\n"
    }

    if (surname.length === 0){
        valid = 0;
        document.studentForm.surname.style.backgroundColor = 'red';
        errorMessage += "No surname!\n"
    }

    if (email.length === 0){
        valid = 0;
        document.studentForm.email.style.backgroundColor = 'red';
        errorMessage += "No e-mail!\n"
    }
    else if ((email.indexOf("@")==-1) || (email.length < 3)){
        valid = 0;
        document.studentForm.email.style.backgroundColor = 'red';
        errorMessage += "Wrong e-mail format!\n"
    }

    if(year === "0"){
        valid = 0;
        document.studentForm.year.style.backgroundColor = 'red';
        errorMessage += "No year chosen!"
    }

    if(valid){
        document.studentForm.name.style.backgroundColor = 'rgb(100,255,100)';
        document.studentForm.surname.style.backgroundColor = 'rgb(100,255,100)';
        document.studentForm.email.style.backgroundColor = 'rgb(100,255,100)';
        document.studentForm.year.style.backgroundColor = 'rgb(100,255,100)';
        alert("Form sent.");
        return true;
    }
    else{
        alert(errorMessage);
        return false;
    }
}

Alright, I figured it out myself.好吧,我自己想通了。 It was such a stupid mistake - simple access issue.这是一个如此愚蠢的错误 - 简单的访问问题。 data.csv couldn't be written to from outside.无法从外部写入data.csv What I needed to do was just one simple line: chmod 757 data.csv .我需要做的只是简单的一行: chmod 757 data.csv It used to be 755, and that's why there was a problem.以前是 755,这就是出现问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM