我正在使用php制作Wordpress插件,使用post方法获取平方英尺和价格,然后将它们相乘并输出值。在我的localhost上执行时,php代码很好,但在wordpress上执行时总是给我0。
我试过给我的变量赋予唯一的名称,我尝试将动作设置为'',实际的网址,页码,并使用php获取页眉。我在堆栈溢出时看到了几个像我一样的问题,但是我找到的所有解决方案都没有为我工作。任何帮助将不胜感激。
<?php
//WordPress Hooks
add_shortcode('jcalc','includeJonnyCalculatorCustom');
if (isset($_POST['jcalcthe_squarefeet']))
{
$jcalcthe_squarefeet = $_POST['jcalcthe_squarefeet'];
}
if (isset($_POST['jcalcthe_price']))
{
$jcalcthe_price = $_POST['jcalcthe_price'];
}
function includeJonnyCalculatorCustom()
{
?>
<p> some text here...
</p>
<?php
if ( isset($_POST['jcalc_calculate']) )
{
$jcalcthe_estimate = ( jcalcthe_squarefeet * jcalcthe_price );
$jcalcthe_estimate = number_format( $jcalcthe_estimate, 2 );
?>
<div>
<form method="post" action=''>
<p> The estimate for your inspection is $
<?php echo $jcalcthe_estimate; ?>
</p>
<button> Back to calculator </button>
</form>
</div>
<?php
}
else
{
?>
<div>
<form method="post" action=''>
<h3> Estimate Calculator </h3>
<label> Square footage of your home: </label>
<input name="jcalcthe_squarefeet" value="<?php echo $jcalcthe_squarefeet ?>"> </input><br /><br />
<label> Price per square foot: </label><br />
<select name="jcalcthe_price" value="<?php echo $jcalcthe_price ?>" style="width: 160px;">
<option value=".15" > $0.15 </option>
<option value=".2" > $0.20 </option>
</select><br /><br />
<button type="submit" style="width: 160px; background-color: lightgrey;" name="jcalc_calculate"> Calculate estimate </button>
</form>
</div>
<?php
}
}
?>
答案 0 :(得分:0)
在短代码中包含您的逻辑
function includeJonnyCalculatorCustom() {
if ( isset($_POST['jcalc_calculate']) ) {
if (isset($_POST['jcalcthe_squarefeet'])) {
$jcalcthe_squarefeet = $_POST['jcalcthe_squarefeet'];
}
if (isset($_POST['jcalcthe_price'])) {
$jcalcthe_price = $_POST['jcalcthe_price'];
}
$jcalcthe_estimate = ( $jcalcthe_squarefeet * $jcalcthe_price );
$jcalcthe_estimate = number_format( $jcalcthe_estimate, 2 );
// Rest code here
}
// Rest Code
//.........