如何使PHP变量在JavaScript中工作

时间:2014-03-22 06:53:13

标签: javascript php html

<script>
function selectinventory(){
var inventorychoice = document.getElementById("selection").value;
if (inventorychoice == 'flemington'){
document.getElementById("inventorydisplay").innerHTML = '<?php echo $flemingtoninv; ?>';
}
else if (inventorychoice == 'default'){
document.getElementById("inventorydisplay").innerHTML = '<?php echo $defaultinv; ?>';   
}
}
</script>

<select onChange="selectinventory()" id="selection" name="selection">
<option value="flemington">Flemington</option>
<option value="default">Default</option>
</select>

<tr id="inventorydisplay">

<?php echo $inventorychoice; ?>

</tr>

我收到错误说&#34; selectinventory未定义&#34;但它有明确的定义。当我从js函数中删除php脚本时,它会删除错误,但我需要通过javascript函数回显php。

1 个答案:

答案 0 :(得分:0)

尝试

<script>
function selectinventory()
{
var inventorychoice = document.getElementById("selection").value;
if (inventorychoice == 'flemington')
{
    document.getElementById("inventorydisplay").innerHTML = <?php echo "$flemingtoninv"; ?>;
}
else if (inventorychoice == 'default')
{
    document.getElementById("inventorydisplay").innerHTML = <?php echo "$defaultinv"; ?>;   
}
}
</script>