我想使用此代码来确定usear的选择并用于进一步的计算,但它不起作用:
我的代码如下
html代码
<select class="form-control" name="currency">
<option value="default">Bitcoin(USD)</option>
<option value="gcr">GCR(USD)</option>
<option value="perfect money">Perfect Money(USD)</option>
<option value="club coin">Club Coin(USD)</option>
</select>
php code
if(isset($_POST['submit'])){
echo 'success';
$currency = $_POST["currency"];
switch($currency){
case 'Bit coin(USD)':
$fixed_price = 4.30;
break;
case 'Perfect Money(USD)':
$fixed_price = 4.30;
break;
case 'GCR(USD)':
$fixed_price = 4.30;
break;
case 'club coin':
$fixed_price = 4.30;
break;
}
}
if(isset($_POST['usd']) && !empty($_POST['usd'])){
$usd = $_POST['usd'];
$total = $usd * $fixed_price;
echo $total . "GHS";
}else if(isset($_POST['ghs']) && !empty($_POST['ghs'])){
$usd = $_POST['ghs'];
$total = $usd * $fixed_price;
echo $total . "USD";
}
提前致谢
答案 0 :(得分:2)
问题在于你的交换机,你不应该得到选项的文本,但是值,试试这个:
switch($currency){
case 'default':
$fixed_price = 4.30;
break;
case 'perfect money':
$fixed_price = 4.30;
break;
case 'gcr':
$fixed_price = 4.30;
break;
case 'club coin':
$fixed_price = 4.30;
break;
}
答案 1 :(得分:0)
这些代码对我有用
html代码
<div id="calculator">
<h3>Calculator</h3>
<form class="form-horizontal" action="index.php" method="POST" role="form">
<div class="form-group">
<label for="sel1">Select E-currency:</label>
<select class="form-control" name="currency" style="width:35%;margin-left:210px;">
<option value="default">Bitcoin(USD)</option>
<option value="gcr">GCR(USD)</option>
<option value="perfect money">Perfect Money(USD)</option>
<option value="club coin">Club Coin(USD)</option>
</select><br><br>
<div class="row">
<h3>From USD To GH¢</h3>
<div class="col-xs-3">
<input class="form-control" id="ex1" type="text" name="usd" placeholder="USD">
</div>
<div class="col-xs-2">
<input class="form-control" id="ex2" type="text" placeholder="=>" disabled>
</div>
<div class="col-xs-3">
<button class="btn btn-info">GH¢</button>
</div>
</div>
<div class="row">
<h3>From GH¢ To USD</h3>
<div class="col-xs-3">
<input class="form-control" id="ex1" type="text" name="ghs" placeholder="GHS">
</div>
<div class="col-xs-2">
<input class="form-control" id="ex2" type="text" placeholder="=>" disabled>
</div>
<div class="col-xs-3">
<button class="btn btn-info">USD</button>
</div>
</div>
<button type="submit" class="btn btn-primary" style="margin:20px 0px 0px 0px;">Convert</button>
</div>
</form>
<div id="results">
<?php include 'includes/calculator.php'; ?>
</div>
</div>
</div>
php code
<?php
if(isset($_POST['currency'])){
$currency = $_POST["currency"];
switch($currency){
case 'default':
$fixed_price = 4.30;
break;
case 'perfect money':
$fixed_price = 4.30;
break;
case 'gcr':
$fixed_price = 4.30;
break;
case 'club coin':
$fixed_price = 4.30;
break;
}
if(isset($_POST['usd']) && !empty($_POST['usd'])){
$usd = $_POST['usd'];
$total = $usd * $fixed_price;
echo '<hr width="50px">'. $total . "GH¢";
}else if(isset($_POST['ghs']) && !empty($_POST['ghs'])){
$usd = $_POST['ghs'];
$total = $usd * $fixed_price;
echo '<hr width="50px">'. $total . "USD";
}
}
?>