【问题标题】:Wordpress plugin using php method="post" not passing values使用 php method="post" 的 Wordpress 插件不传递值
【发布时间】:2014-06-21 04:21:50
【问题描述】:

我正在使用 php 为 Wordpress 制作一个插件,该插件使用 post 方法获取平方英尺和价格,然后将它们相乘并输出值。 php 代码在我的本地主机上执行时很好,但在 wordpress 上执行时它总是给我 0。

我尝试为我的变量赋予唯一的名称,并尝试将操作设置为 ''、实际 url、页码,并使用 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
}
}

?>

【问题讨论】:

    标签: php wordpress forms post


    【解决方案1】:

    在短代码中包含您的逻辑

    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
        //.........
    

    【讨论】:

    • 谢谢。那成功了。我的代码遇到的另一个问题是: $jcalcthe_estimate = ( jcalcthe_squarefeet * jcalcthe_price );平方英尺和价格中缺少 $...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多