dumebi dumebi - 7 months ago 9
Javascript Question

I want to append indexes of an array into a <td> where select options are indexes of the array

I want a situation where I can append the indexes of an array as options in a select. The code below fails.

<?php
$str4 = "select * from fee_names where status = '1' ";
$res4 = mysql_query($str4) or die(mysql_error());
while ($r4 = mysql_fetch_assoc($res4)){
$name = $r4['NAME'];
array_push($fee_nameArray,$name);
}
>?
<input type="text" name="fee_name" id="fee_name" value="<?php echo $fee_nameArray; ?>">


$('#addClasses').click(function(){
var arrayNAme = $('#fee_name').val();
//alert(arrayNAme.length); return false;
row++;
$('#count').val(row);
var feeName = "feeName"+row;
var feeCat = "feeCat"+row;
var freq = "freq"+row;
var others = "others"+row;
var mandate = "mandate"+row;
var rowID = "rowID"+row;
$('#table_mile35').prepend('<tr id="' + rowID + '"><td><select class="form-control" id="' + feeName + '" name="' + feeName + '" required><option value="" selected="selected">--Choose Class--</option>'
for (i = 0; i < arrayNAme.length; i++) {
'<option value="' + arrayNAme[i] + '">' + arrayNAme[i] + '</option>'
}
'</select></td><td><input type="text" class="form-control input-sm " name="' + feeCat + '" id="' + feeCat + '" placeholder="school fees related, club related" required></td><td><input type="text" class="form-control input-sm " name="' + freq + '" id="' + freq + '" placeholder="Yearly, Monthly" required></td><td><textarea type="text" class="form-control input-sm" name="' + others + '" id="' + others + '" placeholder="other relevant information" ></textarea></td><td><input type="checkbox" name="' + mandate + '" id="' + mandate + '" value="1"></td></tr>');
});

Answer

if arrayNAme is an array, Use like this:

$('#addClasses').click(function(){
    var arrayNAme = $('#fee_name').val();
    arrayNAme = arrayNAme.split(',');
    row++;
    $('#count').val(row);
    var feeName = "feeName"+row;
    var feeCat = "feeCat"+row;
    var freq = "freq"+row;
    var others = "others"+row;
    var mandate = "mandate"+row;
    var rowID = "rowID"+row;
    var html = '<tr id="' + rowID + '"><td><select class="form-control" id="' + feeName + '" name="' + feeName + '" required><option value="" selected="selected">--Choose Class--</option>';
    for (i = 0; i < arrayNAme.length; i++) {
        html += '<option value="' + arrayNAme[i] + '">' + arrayNAme[i] + '</option>';
    }
    html += '</select></td><td><input type="text" class="form-control input-sm "  name="' + feeCat + '" id="' + feeCat + '" placeholder="school fees related, club related" required></td><td><input type="text" class="form-control input-sm "  name="' + freq + '" id="' + freq + '" placeholder="Yearly, Monthly" required></td><td><textarea type="text" class="form-control input-sm" name="' + others + '" id="' + others + '" placeholder="other relevant information" ></textarea></td><td><input type="checkbox"  name="' + mandate + '" id="' + mandate + '" value="1"></td></tr>';
    $('#table_mile35').prepend(html);
 });

I am supposing value of arrayNAme is comma seperated (for example: Tuition Fee,Book Fee,Registration Fee)

Comments