I encoded a simple form for a friend's hobby; basically, we are trying to guess the starting lineup for a couple of teams in the 2010 FIFAWorld Cup (only for strikes).
In any case, I need to check the following form. All Javascript functions work well, because if I just call them outside the tag, everything works fine. Now I want to do some basic client-side validation (I know this is not safe blabla, but it is a hobby, so it doesn’t matter) and then submit the form to another page where PHP is (currently using WAMP Server 2.0) The script will collect data and store it in the database.
Here is the form:
<form name ="formPT" id="formPT" action="" onsubmit="return ValidaTudo();" method="post"> <a><img src="icons/paises/portugal-flag-icon.png" alt="portugal" border="0" style="vertical-align:middle"> Portugal</a> <table id="tabela_PT" cellspacing="0" summary="Equipa"> <thead> <tr> <th scope="col" abbr="Nome" class="nobg">Nome</th> <th scope="col" abbr="Posicao">Posição</th> <th scope="col" abbr="Escolha">Escolha</th> <th scope="col" abbr="Anterior">Escolha Anterior</th> </tr> </thead> <tbody> <tr><td scope="row" abbr="Jogador" class="spec">Eduardo</td><td>Guarda-redes</td><td><select name="escolhaPT"><option value="EF">EF</option><option value="SP">SP</option><option value="NU" selected="selected">NU</option></select> </td><td>NU</td></tr> <tr><td scope="row" abbr="Jogador" class="spec">Beto</td><td>Guarda-redes</td><td><select name="escolhaPT"><option value="EF">EF</option><option value="SP">SP</option><option value="NU" selected="selected">NU</option></select> </td><td>NU</td></tr> <tr><td scope="row" abbr="Jogador" class="spec">Daniel Fernandes</td><td>Guarda-redes</td><td><select name="escolhaPT"><option value="EF">EF</option><option value="SP">SP</option><option value="NU" selected="selected">NU</option></select> </td><td>NU</td></tr> <tr><td scope="row" abbr="Jogador" class="spec">Paulo Ferreira</td><td>Defesa</td><td><select name="escolhaPT"><option value="EF">EF</option><option value="SP">SP</option><option value="NU" selected="selected">NU</option></select> </td><td>NU</td></tr> <tr><td scope="row" abbr="Jogador" class="spec">Miguel</td><td>Defesa</td><td><select name="escolhaPT"><option value="EF">EF</option><option value="SP">SP</option><option value="NU" selected="selected">NU</option></select> </td><td>NU</td></tr> <tr><td scope="row" abbr="Jogador" class="spec">Ricardo Carvalho</td><td>Defesa</td><td><select name="escolhaPT"><option value="EF">EF</option><option value="SP">SP</option><option value="NU" selected="selected">NU</option></select> </td><td>NU</td></tr> <tr><td scope="row" abbr="Jogador" class="spec">Bruno Alves</td><td>Defesa</td><td><select name="escolhaPT"><option value="EF">EF</option><option value="SP">SP</option><option value="NU" selected="selected">NU</option></select> </td><td>NU</td></tr> <tr><td scope="row" abbr="Jogador" class="spec">Ricardo Costa</td><td>Defesa</td><td><select name="escolhaPT"><option value="EF">EF</option><option value="SP">SP</option><option value="NU" selected="selected">NU</option></select> </td><td>NU</td></tr> <tr><td scope="row" abbr="Jogador" class="spec">Fabio Coentrao</td><td>Defesa</td><td><select name="escolhaPT"><option value="EF">EF</option><option value="SP">SP</option><option value="NU" selected="selected">NU</option></select> </td><td>NU</td></tr> <tr><td scope="row" abbr="Jogador" class="spec">Pepe</td><td>Médio</td><td><select name="escolhaPT"><option value="EF">EF</option><option value="SP">SP</option><option value="NU" selected="selected">NU</option></select> </td><td>NU</td></tr> </tbody> </table> <input type='submit' value='Check Field' /> </form>
Now a little Javascript:
function ValidaTudo() { alert('debug: validating...') if(Valida('PT')) { alert('fine!'); return true; } else { alert('useless validation message but wth'); return false; } }
There are more javascript features there, obviously, but:
- I do not get a popup with javascript function!
- The page is pretty simple redirected to index.php, so I assume this is a PHP / WAMP problem ... I reloaded WAMP just a couple of times, but so far no luck. I did not restart my computer, although I think I should.
What am I missing? Thanks in advance guys!
PS: Yes, the form does not send data to any page at all; I know it; it does not affect strange behavior, I tried it.
Edit: Here are the rest of the checking functions. They are used to verify that the user is following certains rules (for example, choosing exactly 11 players, 1 goalkeeper, at least 3 defenders, etc.). I pass a couple of parameters because in full form we are trying to guess players from 4 teams (Portugal, Spain, Brazil and Argentina) - therefore, 4 forms, each with a different identifier (based on the national identifier → {PT, BR, AR , ES} Here are the rest of the functions (keep in mind that they work outside the form) :
function Valida (textstring) { var error_msg = null; if(!valida_11(textstring.toUpperCase())) { error_msg = 'Erro para a equipa ' + textstring.toUpperCase() + ': deve selecionar apenas 11 jogadores efectivos'; alert(error_msg); } if (!valida_gk(textstring.toUpperCase())) { error_msg = 'Erro para a equipa ' + textstring.toUpperCase() + ': deve selecionar um e um só 1 guarda-redes efectivo'; alert(error_msg); } if (!valida_def(textstring.toUpperCase())) { error_msg = 'Erro para a equipa ' + textstring.toUpperCase() + ': deve selecionar um mínino de 3 defesas efectivo'; alert(error_msg); } if(error_msg == null) return true; else return false; } function valida_11(tbl) { var ef = 0; var input_list = document.getElementsByName('escolha' + tbl); for(var i = 0; i < input_list.length; i++) { var a = input_list[i].value; if(a == "EF") ef++; } if (ef == 11) return true; else return false; } function valida_gk(tbl) { var ef = 0; var gks = conta_gks(tbl); var input_list = document.getElementsByName('escolha' + tbl); for(var i = 0; i < gks; i++) { var a = input_list[i].value; if(a == "EF") ef++; } if (ef == 1) return true; else return false; } function valida_def(tbl) { var defs = conta_defs(tbl); var gks = conta_gks(tbl); var ef = 0; var input_list = document.getElementsByName('escolha' + tbl); for(var i = gks-1; i < gks + defs; i++) { var a = input_list[i].value; if(a == "EF") ef++; } if (ef >= 3) return true; else return false; } function conta_defs(tbl) { var defs = 0; var tabela = document.getElementById('tabela_' + tbl); for(var i = 1; i < tabela.getElementsByTagName("tr").length; i++) { var linha = tabela.getElementsByTagName("tr")[i]; var celula = linha.getElementsByTagName("td")[1]; if(celula.innerHTML == "Defesa") defs++; } return defs; } function conta_gks(tbl) { var gk = 0; var tabela = document.getElementById('tabela_' + tbl); for(var i = 1; i < tabela.getElementsByTagName("tr").length; i++) { var linha = tabela.getElementsByTagName("tr")[i]; var celula = linha.getElementsByTagName("td")[1]; if(celula.innerHTML == "Guarda-redes") gk++; } return gk; }
Edit 2: The same thing happens in other browsers, such as IE 7. Editing 3: rebooting the PC, but the problem remains ...: /
Ok, I managed to put the page on the net @: http://testing.freeoda.com/teste.html Give it a spin. Here I get a 404 error; JS function is not called ....