简体   繁体   English

访问 javascript 中 php 数组中的值

[英]accessing values in php array in javascript

I have created an array from an mssql table with the code below我使用下面的代码从 mssql 表创建了一个数组

$query = "SELECT RoomTypeId, RoomTypeName, RoomTypeRate FROM RoomTypes";
$result = sqlsrv_query($dbh, $query);
$series = array();
while ($record = sqlsrv_fetch_array($result1)) 
{
    $series[$record['RoomTypeId']] = array('RoomTypeName' => $record['RoomTypeName'], 'RoomTypeRate' => $record['RoomTypeRate']);
}

I pick the room type with the code below.我用下面的代码选择房间类型。

              <td align ="left">
                        <select name="RoomTypeId" style="width:100px" >
                            <option value=""  selected> </option>
                            <?php
                                while ($row = sqlsrv_fetch_array($result4, SQLSRV_FETCH_ASSOC)) {
                                    echo '<option value="' . $row['RoomTypeId'] .'">' .$row['RoomTypeName'].'   </option>';
                                }
                            ?>
                        </select>                           
                    </td>           

how can i get the RoomTypeRate with the RoomTypeId as a parameter如何以 RoomTypeId 作为参数获取 RoomTypeRate

You can add PHP code in script tags您可以在脚本标签中添加 PHP 代码

  
// Create an array 
$sampleArray = array( 
 0 => "A",  
 1 => "B",  
 2 => "C",  
) 
?> 

<script> 

// Access the array elements 
var passedArray =  
 <?php echo json_encode($sampleArray); ?>; 
    
// Display the array elements 
for(var i = 0; i < passedArray.length; i++){ 
 document.write(passedArray[i]); 
} 
</script> 



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM