Reputation: 23
I made a form in HTML that's supposed to save some data through my university's server:
<!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:
#!/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. I filled the form and got the correct result:
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
. (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. 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;
}
}
Upvotes: 0
Views: 774
Reputation: 23
Alright, I figured it out myself. It was such a stupid mistake - simple access issue.
data.csv
couldn't be written to from outside. What I needed to do was just one simple line: chmod 757 data.csv
. It used to be 755, and that's why there was a problem.
Upvotes: 1