提交仅发送一组选项的结果
我真的是从 php 开始,但有一点 C 编程经验。因此,我尝试制作两个标签选项来添加每个类别之一,最后用提交按钮显示结果。但我只得到一个结果,如果我在第二个类别中什么都不选择,它只会给出第一个类别的结果,反之亦然。我的目标是获得两个类别的两个选择的结果。 顺便说一句,我正在使用 XAMPP 运行此页面,并使用 VS Code 处理代码。
这是代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Buying a lunch</title>
</head>
<body>
<div class="container mt-2">
<?php
$apple = 2;
$banana = 3;
$pineapple = 5;
$watermelon = 8;
$grapes = 6;
$cocacola = 8;
$mountaindew = 10;
$pepsi = 6;
$fanta = 7;
$water = 3;
$total = 0;
?>
<div class="row">
<div class="col-12">
<form>
<div class="form-group">
<label>Something to eat</label>
<?php $fruits=$_GET['fruits']??''; ?>
<select name="fruits" class="form-control">
<option value="" <?php if($fruits=='')echo "selected='selected'"; ?> >Choose a fruit</option>
<option value="apple"<?php if($fruits=='apple')echo "selected='selected'"; ?>>An apple</option>
<option value="banana"<?php if($fruits=='banana')echo "selected='selected'"; ?>>An banana</option>
<option value="pineapple"<?php if($fruits=='pineapple')echo "selected='selected'"; ?>>An pineapple</option>
<option value="watermelon"<?php if($fruits=='watermelon')echo "selected='selected'"; ?>>An watermelon</option>
<option value="grapes"<?php if($fruits=='grapes')echo "selected='selected'"; ?>>An grapes</option>
</select>
</div>
<div class="form-group">
<label>Something to drink</label>
<?php $drinks=$_GET['drinks']??''; ?>
<select name="drinks" class="form-control">
<option value="" <?php if($drinks=='')echo "selected='selected'"; ?> >Choose a drink</option>
<option value="cocacola"<?php if($drinks=='cocacola')echo "selected='selected'"; ?>>A Cocacola</option>
<option value="mountaindew"<?php if($drinks=='mountaindew')echo "selected='selected'"; ?>>A MountainDew</option>
<option value="pepsi"<?php if($drinks=='pepsi')echo "selected='selected'"; ?>>A Pepsi</option>
<option value="fanta"<?php if($drinks=='fanta')echo "selected='selected'"; ?>>A Fanta</option>
<option value="water"<?php if($drinks=='water')echo "selected='selected'"; ?>>A Water</option>
</select>
</div>
<button type="submit" name="send" value="send" class="btn btn-primary">Send</button>
</form>
</div>
<!-- -->
</div>
<div class="row">
<div class="col-12">
<div class="alert alert-primary" role="alert">
<?php
$buttonSend=$_GET['send']??'';
if($buttonSend=='send'){
if($fruits=='apple'){
$total=+$apple;
}
if($fruits=='banana'){
$total=+$banana;
}
if($fruits=='pineapple'){
$total=+$pineapple;
}
if($fruits=='watermelon'){
$total=+$watermelon;
}
if($fruits=='grapes'){
$total=+$grapes;
}
}
if($buttonSend=='send'){
if($drinks=='cocacola'){
$total=+$cocacola;
}
if($drinks=='mountaindew'){
$total=+$mountaindew;
}
if($drinks=='pepsi'){
$total=+$pepsi;
}
if($drinks=='fanta'){
$total=+$fanta;
}
if($drinks=='water'){
$total=+$water;
}
}
?>
</div>
</div>
</div>
<!-- -->
<?php
echo "The total expensive of your lunch is $" . $total . ".00";
?>
</div>
</body>
</html>
I'm really beggining with php but have a little of experience programming in C. So was trying to make two labels options to add one of each category, finally show the results with a submit button. But I just get one result, if I don't choose nothing at all in the second category, it gives only the result of the first category, and vice versa. My objective is to get the results of both selections of the two categories.
BTW, I'm running this page with XAMPP and working in the code with VS Code.
Here is the code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Buying a lunch</title>
</head>
<body>
<div class="container mt-2">
<?php
$apple = 2;
$banana = 3;
$pineapple = 5;
$watermelon = 8;
$grapes = 6;
$cocacola = 8;
$mountaindew = 10;
$pepsi = 6;
$fanta = 7;
$water = 3;
$total = 0;
?>
<div class="row">
<div class="col-12">
<form>
<div class="form-group">
<label>Something to eat</label>
<?php $fruits=$_GET['fruits']??''; ?>
<select name="fruits" class="form-control">
<option value="" <?php if($fruits=='')echo "selected='selected'"; ?> >Choose a fruit</option>
<option value="apple"<?php if($fruits=='apple')echo "selected='selected'"; ?>>An apple</option>
<option value="banana"<?php if($fruits=='banana')echo "selected='selected'"; ?>>An banana</option>
<option value="pineapple"<?php if($fruits=='pineapple')echo "selected='selected'"; ?>>An pineapple</option>
<option value="watermelon"<?php if($fruits=='watermelon')echo "selected='selected'"; ?>>An watermelon</option>
<option value="grapes"<?php if($fruits=='grapes')echo "selected='selected'"; ?>>An grapes</option>
</select>
</div>
<div class="form-group">
<label>Something to drink</label>
<?php $drinks=$_GET['drinks']??''; ?>
<select name="drinks" class="form-control">
<option value="" <?php if($drinks=='')echo "selected='selected'"; ?> >Choose a drink</option>
<option value="cocacola"<?php if($drinks=='cocacola')echo "selected='selected'"; ?>>A Cocacola</option>
<option value="mountaindew"<?php if($drinks=='mountaindew')echo "selected='selected'"; ?>>A MountainDew</option>
<option value="pepsi"<?php if($drinks=='pepsi')echo "selected='selected'"; ?>>A Pepsi</option>
<option value="fanta"<?php if($drinks=='fanta')echo "selected='selected'"; ?>>A Fanta</option>
<option value="water"<?php if($drinks=='water')echo "selected='selected'"; ?>>A Water</option>
</select>
</div>
<button type="submit" name="send" value="send" class="btn btn-primary">Send</button>
</form>
</div>
<!-- -->
</div>
<div class="row">
<div class="col-12">
<div class="alert alert-primary" role="alert">
<?php
$buttonSend=$_GET['send']??'';
if($buttonSend=='send'){
if($fruits=='apple'){
$total=+$apple;
}
if($fruits=='banana'){
$total=+$banana;
}
if($fruits=='pineapple'){
$total=+$pineapple;
}
if($fruits=='watermelon'){
$total=+$watermelon;
}
if($fruits=='grapes'){
$total=+$grapes;
}
}
if($buttonSend=='send'){
if($drinks=='cocacola'){
$total=+$cocacola;
}
if($drinks=='mountaindew'){
$total=+$mountaindew;
}
if($drinks=='pepsi'){
$total=+$pepsi;
}
if($drinks=='fanta'){
$total=+$fanta;
}
if($drinks=='water'){
$total=+$water;
}
}
?>
</div>
</div>
</div>
<!-- -->
<?php
echo "The total expensive of your lunch is quot; . $total . ".00";
?>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论