Hello all,
I just joined this forum and this is my first post, so I wanted to thank you all in advance for your help.
I’m pretty much a newbie when it comes to PHP and Javascript, as I’ve always been involved mainly in the designing part of website building (HTML and CSS).
Anyway, here’s my problem (hope I’ll explain it clearly):
I’m building this website that features a contact form where visitors can type in their name, email address and whatnot to receive feedback from the admins.
This form features two select fields labeled “area” and “city”.
The “city” field is initially set to a blank value, and has no other options selectable,
By selecting the “area” first, the selected area’s cities appear in the “city” field.
Onto the code:
1) here’s the form HTML code (shortened version):
[code=html]<select name=”area” onchange=”SelectArea()”>
<option value=”” selected=”selected”>Area</option>
<option value=”101″>Piemonte</option>
<option value=”102″>Lombardia</option>
<option value=”103″>Veneto</option>
</select>
<select name=”city” onmouseup=”CheckCity()”>
<option selected=”selected” value=””>City</option>
</select>
2) here’s the relevant Javascript that displays the cities available for each area (“prof” is the form name):
[CODE]function SelectArea(){
GetCityCode();
if (document.prof.area.value > 0){
selectVal = document.prof.area.value;
ClearCity(nMax);
j = 1;
for (i = 1; i <= nMax; i++){
if (n[i][1] == selectVal){
document.prof.city.options[j] = new Option(n[i][2],n[i][0]);
j = j + 1;
}
}
}else{
ClearCity(nMax);
}
}
function ClearCity(nMax){
for (i = 1; i < nMax; i++){
document.prof.city.options[1] = null;
}
}
3) the list with all the cities is in another attached .js file (here I paste just a few lines to give you the idea):
[CODE]function GetCityCode(){
n = new Array;
n[1] = new Array(11011,101,”Torino”);
n[2] = new Array(11029,101,”Biella”);
n[44] = new Array(22012,102,”Milano”);
n[45] = new Array(22021,102,”Como”);
n[53] = new Array(32018,103,”Venezia”);
n[54] = new Array(32026,103,”Verona”);
…
nMax = 904;
}
So far everything works like a charm. The website visitor chooses the area they’re from, the “city” field gets populated with all the cities in that area and they choose the city they’re from.
Now,
here’s the problem:
I compiled a PHP script which retrieves all the data submitted in the form and posts it to a .csv file.
here’s the relevant PHP code:
[code=php]<?php
$area = $_POST[‘area’];
$city = $_POST[‘city’];
$csv_file = ‘contacts.csv’;
if (is_writable($csv_file)) {
if (!$csv_handle = fopen($csv_file,’a’)) {
exit;}
}
$csv_item = “”$area”,”$city”n”;
if (is_writable($csv_file)) {
if (fwrite($csv_handle, $csv_item) === FALSE) {
exit; }
}
fclose($csv_handle);
?>
The script works, it writes data on the .csv file, but for “area” and “city” all i get are their numeric values and not the names.
Say for example that a visitor submits “Piemonte” as area and “Torino” as city,
in the .csv file I get “101” for the area and “11011” for the city.
Is there a way to get the area and city names instead of numbers in the .csv?
Excuse this long post and thanks in advance!
Cyrus