I'm printing database content on a HTML page like this:
<form name="result_form" action="dostuff.php" onsubmit="return validateResult()" method="post">
<table>
<?php
$my_object_array = getStuffFromDatabase();
for ($x = 0; $x < count($my_object_array ); $x++) {
echo '<tr>';
echo '<td>';
echo 'Hemmalag: <input type="number" id="result' . $x. '" name="result' . $x . '" value = "' . $my_object_array[$x]->getResult(). '"></input>';
echo '</td>';
echo '</tr>';
}
?>
</table>
<button type="submit">Save</button>
</form>
I would like to validate all these input elements using javascript. The fields are gonna be checked as not being empty. I won't use required since Safari doesn't support it. I'm thinking something like:
form_validate.js:
function validateResult(){
var row_count = document.getElementById("row_counter").value;
var validate_success = true;
for (i = 0; i < row_count; i++) {
var tmp_home_result = document.getElementById("result" + i).value;
if(tmp_home_result == "" || tmp_home_result == null){
validate_success = false;
}
}
return validate_success;
}
However, this doesn't seem to work. The form is still being shipped off to the server. What's wrong here?