我是初学者,所以问题可能是愚蠢的,有一个从html表单传递的POST数据,我希望它根据该值打印一些字符串,让我们说
$_POST['veggie']
数据可能是1,2,3。我想要它打印" apple"," banana"," carrot"。我尝试过使用JavaScript但它不会读取POST数据
<script>
whatVeggie(){
if ($_POST['veggie'] == 1) {
document.getElementById('myVeggie').innerHTML="apple";
} else if ($_POST['veggie'] == 2){
document.getElementById('myVeggie').innerHTML="banana";
} else if $_POST['veggie'] == 3){
document.getElementById('myVeggie').innerHTML="carrot";
}
}
</script>
<body onload="whatVeggie();">
I like to eat<p id="myVeggie"></p>
</body>
答案 0 :(得分:0)
这里不需要JS就可以使用PHP代码:
<?php
$id='default_id';
$veggies = ["","apple","banana","carrot"];
if ( isset($_POST['veggie']) ){
$id = $veggies[$_POST['veggie']];
}
?>
<body>
I like to eat<p id="<?php echo $id; ?>"></p>
</body>
希望这有帮助。