ajay kumar ajay kumar - 15 days ago 5
Javascript Question

i am not getting dropdown with this code

//here str is a variable, i just trying to assign php variable to the srt variable but i am getting an error i.e:-


Parse error: syntax error, unexpected 'var' (T_VAR)


<script>
<?php
while($rowss = mysql_fetch_array($result2, MYSQL_ASSOC)){
?>
var str = <?php echo $rowss["ingredient"];?>;
var str_array = str.split(',');
for(var i = 0; i < str_array.length; i++) {
// Trim the excess whitespace.
str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
// Add additional code here, such as:
var opt = document.createElement('option');
opt.innerHTML = str_array[i];
opt.value = str_array[i];
sel.appendChild(opt);
}
}
</script>

Answer
?>
<script>
<?php
while($rowss = mysql_fetch_array($result2, MYSQL_ASSOC)){   
?>
var str = '<?=$rowss["ingredient"]?>';
var str_array = str.split(',');
    for(var i = 0; i < str_array.length; i++) {
        // Trim the excess whitespace.
        str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
        // Add additional code here, such as:
        var opt = document.createElement('option');
        opt.innerHTML = str_array[i];
        opt.value = str_array[i];
        sel.appendChild(opt);
    }
<?php
}
?>
</script>