我正在编写一个使用HTML选择框的计算器。但是,我的PHP代码没有正确发布选择值。有人能提供可能的见解吗?我不知道出了什么问题。您可以在此处试用计算器:http://macrorevolution.com/calculators/1-rep-max/
当我按提交时,该值被清除,没有给出结果。在我的其他计算器上,例如:http://macrorevolution.com/calculators/total-daily-energy-expenditure/,输入的值在提交后保留。
PHP代码:
<?php
$repsv = "";
$weightliftv = "";
$answer = "";
if(isset($_POST['weightliftv'])) {
$repsv = $_POST['repsv'];
$weightliftv = $_POST['weightliftv'];
if ($repsv=='0');
else if($repsv=='1v') $repsv=1;
else if($repsv=='2v') $repsv=1.0285949393;
else if($repsv=='3v') $repsv=1.0588733587;
else if($repsv=='4v') $repsv=1.0909884355;
else if($repsv=='5v') $repsv=1.1251125112;
else if($repsv=='6v') $repsv=1.1614401858;
else if($repsv=='7v') $repsv=1.2001920307;
else if($repsv=='8v') $repsv=1.2416190713;
else if($repsv=='9v') $repsv=1.2860082305;
else if($repsv=='10v') {$repsv=1.3333333333; echo "10";}
else echo "error";
$answer = $weightliftv * $repsv;
}
?>
以下是同一文件中的html代码:
<form method='post' action=''>
<table width='80%' style="margin: 0 auto;">
<tr class="spaceUnder">
<td>Weight Lifted:</td>
<td><input type='text' name='agev' value="<?php echo $agev; ?>"/>lbs</td>
</tr>
<tr class="spaceUnder">
<td>Reps Done</td>
<td colspan="2">
<select name="repsv">
<option name='repsv' selected='selected' value=''>Select</option>
<option name='repsv' value='1v'>1 Reps</option>
<option name='repsv' value='2v'>2 Reps</option>
<option name='repsv' value='3v'>3 Reps</option>
<option name='repsv' value='4v'>4 Reps</option>
<option name='repsv' value='5v'>5 Reps</option>
<option name='repsv' value='6v'>6 Reps</option>
<option name='repsv' value='7v'>7 Reps</option>
<option name='repsv' value='8v'>8 Reps</option>
<option name='repsv' value='9v'>9 Reps</option>
<option name='repsv' value='10v'>10 Reps</option>
</select>
</td>
</tr>
<tr class="spaceUnder">
<td colspan="2"><input type='submit' class="button highlight small" value='Calculate'/></td>
</tr>
<tr class="spaceUnder">
<td colspan="2"><strong>Your one rep max is </strong><input type='text' style="width: 50px; font-weight:bold;" value='<?php echo round($answer,0); ?>' /><strong>lbs</strong> </td>
</tr>
</table>
</form>
如有必要,这是完整的代码:
<?php
/*
Template Name: 1repcalc
*/
?>
<?php get_header(); ?>
<?php $al_options = get_option('al_general_settings');?>
<!-- Title -->
<div class="box pt20">
<!-- Title -->
<div class="headertext">
<?php the_title() ?>
<?php $headline = get_post_meta($post->ID, "_headline", $single = false);?>
<?php if(!empty($headline[0]) ):?>
<span><?php echo $headline[0] ?></span>
<?php endif?>
</div>
<div class="clearsmall"></div>
<!-- Promo text -->
<?php $promo = get_post_meta($post->ID, "_promo", $single = false);?>
<?php if(!empty($promo[0]) ):?>
<div class="calloutcontainer">
<div class="container_12">
<div class="grid_12">
<?php echo do_shortcode($promo[0]);?>
</div>
</div>
</div>
<?php endif?>
<div class="container_12">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div>
</div>
<!-- ///////////////////////////////////////////////////////////////////////////////////////////-->
<?php
$repsv = "";
$weightliftv = "";
$answer = "";
if(isset($_POST['weightliftv'])) {
$repsv = $_POST['repsv'];
$weightliftv = $_POST['weightliftv'];
if ($repsv=='0');
else if($repsv=='1v') $repsv=1;
else if($repsv=='2v') $repsv=1.0285949393;
else if($repsv=='3v') $repsv=1.0588733587;
else if($repsv=='4v') $repsv=1.0909884355;
else if($repsv=='5v') $repsv=1.1251125112;
else if($repsv=='6v') $repsv=1.1614401858;
else if($repsv=='7v') $repsv=1.2001920307;
else if($repsv=='8v') $repsv=1.2416190713;
else if($repsv=='9v') $repsv=1.2860082305;
else if($repsv=='10v') {$repsv=1.3333333333; echo "10";}
else echo "error";
$answer = $weightliftv * $repsv;
echo $answer;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<title>One Rep Max Calculator</title>
<style>
table {
font-size:15px;
font-family: 'PT Sans', sans-serif;
background-color:#FFFFFF;
}
tr.spaceUnder > td
{
padding:0em 1em 1em 0em;
}
p.ss {
font-size:30px;
text-align:center
}
input {
margin-right:5px;
margin-left:5px;
}
</style>
</head>
<body>
<div class="box pt20">
<p class="ss">MacroRevolution One Rep Max Calculator</p><br>
<table width='80%' style="margin: 0 auto;">
<tr class="spaceUnder">
<td colspan="4">
All weight training and resistance training programs are always based off of your 1 rep max. If you are uncertain of your 1 rep max you can use the 1 rep max calculator below to help get you pretty close to that number.
In many cases we find that the 1 rep max calculator is accurate within 5%. The closer you can get to completing 1 rep at maximum effort, the more accurate it will be!
</td>
</tr>
</table>
<form method='post' action=''>
<table width='80%' style="margin: 0 auto;">
<tr class="spaceUnder">
<td>Weight Lifted:</td>
<td><input type='text' name='agev' value="<?php echo $agev; ?>"/>lbs</td>
</tr>
<tr class="spaceUnder">
<td>Reps Done</td>
<td colspan="2">
<select name="repsv">
<option name='repsv' selected='selected' value=''>Select</option>
<option name='repsv' value='1v'>1 Reps</option>
<option name='repsv' value='2v'>2 Reps</option>
<option name='repsv' value='3v'>3 Reps</option>
<option name='repsv' value='4v'>4 Reps</option>
<option name='repsv' value='5v'>5 Reps</option>
<option name='repsv' value='6v'>6 Reps</option>
<option name='repsv' value='7v'>7 Reps</option>
<option name='repsv' value='8v'>8 Reps</option>
<option name='repsv' value='9v'>9 Reps</option>
<option name='repsv' value='10v'>10 Reps</option>
</select>
</td>
</tr>
<tr class="spaceUnder">
<td colspan="2"><input type='submit' class="button highlight small" value='Calculate'/></td>
</tr>
<tr class="spaceUnder">
<td colspan="2"><strong>Your one rep max is </strong><input type='text' style="width: 50px; font-weight:bold;" value='<?php echo round($answer,0); ?>' /><strong>lbs</strong> </td>
</tr>
</table>
</form>
<table border='0' width='80%' style="margin: 0 auto;">
<td colspan="4">
<br>
If you want to calculate your formula....
<br><br><br>
</td>
</table>
</div>
</body>
</html>
<!-- ///////////////////////////////////////////////////////////////////////////////////////////-->
<?php get_footer(); ?>
答案 0 :(得分:1)
我没有看到weightliftv的输入。如果有,那么它可能是;在if之后($ repsv =='0');删除它,它应该工作。
虽然有几点注意。不需要isset。 if($ _ POST ['weightliftv']){做同样的事情。您不需要为每个选项命名,只需命名它们所属的选项。不要忘记过滤帖子,以便它们更安全(filter_var())。
<?php echo $var; ?> can be shortened to <?=$var;?>
否则if($ repsv =='10v'){$ repsv = 1.3333333333; echo“10”;}在不需要时有花括号。它们不会伤害任何东西,但它会使您的代码更加统一,更易于阅读。
可以将许多选择选项放入循环中。
for($i=1;$i<11;$i++) {
<option name='repsv' value='1v'>1 Reps</option>
}
<?php
$repsv = "";
$weightliftv = "";
$answer = "";
if(isset($_POST['weightliftv'])) {
$repsv = $_POST['repsv'];
$weightliftv = $_POST['weightliftv'];
if ($repsv=='0');
else if($repsv=='1v') $repsv=1;
else if($repsv=='2v') $repsv=1.0285949393;
else if($repsv=='3v') $repsv=1.0588733587;
else if($repsv=='4v') $repsv=1.0909884355;
else if($repsv=='5v') $repsv=1.1251125112;
else if($repsv=='6v') $repsv=1.1614401858;
else if($repsv=='7v') $repsv=1.2001920307;
else if($repsv=='8v') $repsv=1.2416190713;
else if($repsv=='9v') $repsv=1.2860082305;
else if($repsv=='10v') {$repsv=1.3333333333; echo "10";}
else echo "error";
$answer = $weightliftv * $repsv;
}
?>