2016-11-22 21 views
-5

//這裏str是一個變量,我只是想PHP變量賦值給變量SRT,但我得到一個錯誤,即: -我沒有得到下拉列表,此代碼

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> 
+0

https://eval.in/682550和https://eval.in/682552 –

+2

@RuchishParikh內'php'文件'變種str'不會辨認,直到它成爲內側''

0

另一種方法可能是用PHP生成一個數組,其中包含所有ingredients,稍後將其指定爲javascript中的變量。

<?php 
    $strs = array(); 
    while($rowss = mysql_fetch_array($result2, MYSQL_ASSOC)){ 
     $strs[]=$rowss["ingredient"]; 
    } 
?> 

<script> 
    <?php 
     echo "var strs=JSON.parse(" . json_encode($strs) . ");\n"; 
    ?> 
    for(var n in strs){ 

     var str = strs[ n ]; 
     var str_array = str.split(','); 

     for(var i = 0; i < str_array.length; i++) { 
      str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, ""); 
      var opt = document.createElement('option'); 
       opt.innerHTML = str_array[i]; 
       opt.value = str_array[i]; 
      sel.appendChild(opt); 
     } 
    } 
</script> 
+0

我無法生成下拉菜單。請幫幫我。 –