Possible Duplicate:
How to debug Javascript error?
I have created a table with HTML form as follow: // $usr is just a result of another process:
<form name="myForm" action="addAccessories.php" method="post" onsubmit="return validateForm(this);">
<table border="1" id="IT">
<tr>
<th>Barcode<em>*</em></th>
<th>Current stock</th>
</tr>
<?php
for($id=1; $id<=$usr; $id++){
?>
<tr>
<td><input type="hidden" name="id[]" value="<?php echo $id; ?>" />
<input type="text" name="bar_code<?php echo $id; ?>" id="bar_code<?php echo $id; ?>" value="" /></td>
" type="text" id="description" value="" />
<td><input type="text" name="num_stock<?php echo $id; ?>" value="0" id="num_stock<?php echo $id; ?>"/></td>
<?php
}
?>
<tr>
<td> </td>
<td> <button data-theme="b" input type="submit" name="Submit" value="Submit">Add accessories</button></td>
</tr>
</table>
</form>
After the inputs are taken and table is created I used the validation form codes to validate it (validation and auto-completion codes):
<?php
$query= "SELECT description from accessories_type ORDER BY description";
$result= mysqli_query($link, $query) or die(mysqli_error($link));
?>
<script>
$(document).ready(function() {
for(i = 1; i < document.getElementById("IT").rows.length; i++) {
var availableConsoles = [
<?php
while($row = mysqli_fetch_array($result)){
echo '"' . $row['description'] . '"' . ',';
}
?>
];
$( "#description " ).autocomplete({
source: availableConsoles
});
}
});
</script>
<script type="text/javascript">
function validateForm(form){
var errors = [];
var c, cs = form.elements;
var reB = /^bar_code/;
var reN = /^num_stock/;
var reD = /^[0-9]+$/;
for (var i=0;i<cs.length; i++) {
c = cs[i];
if (reB.test(c.name) && c.value == '') {
errors[errors.length]= 'You must enter barcode.';
}
if (reN.test(c.name) && !reD.test(c.value)) {
errors[errors.length] = 'Stock number must be a positive integer.';
}
if (errors.length > 0) {
reportErrors(errors);
return false;
} return true;
}
}
function reportErrors(errors){
var msg = "There were some problems...\n";
for (var i = 0; i<errors.length; i++) {
var numError = i + 1;
msg += "\n" + numError + ". " + errors[i];
}
alert(msg);
}
</script>
However, it does not perform as I wanted. Can anyone point out what I did wrong and how to fix it? Also, how to debug javascripts codes in general (like print out to terminal the result of codes execution similar to "echo .... in PHP") Thank you for your help.