Reputation: 395
I'm coding a calculator that uses an HTML select box. However, the select values aren't posting properly with my PHP code. Can anyone offer possible insight? I have no idea what is wrong. You can try the calculator here: http://macrorevolution.com/calculators/1-rep-max/
When I press submit, the value is cleared and no result is given. On my other calculators such as this one: http://macrorevolution.com/calculators/total-daily-energy-expenditure/ , the inputted value stays after submitting.
PHP Code:
<?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;
}
?>
Here is the html code in the same file:
<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>
Here is the full code if necessary:
<?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(); ?>
Upvotes: 0
Views: 109
Reputation: 154
I don't see an input for weightliftv. If there is one then it might be the ; after the if ($repsv=='0'); remove it and it should work.
A few notes though. Don't need the isset. if($_POST['weightliftv']) { does the same thing. You don't need to name each option, just the select they belong to. Don't forget to filter your posts so they are more secure (filter_var()).
<?php echo $var; ?> can be shortened to <?=$var;?>
else if($repsv=='10v') {$repsv=1.3333333333; echo "10";} has curly braces when not needed. They don't hurt anything, but it makes your code more uniform and easier to read.
That many select options can be put into a loop.
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;
}
?>
Upvotes: 1