如何根据用户输入设置数值变量

时间:2017-07-03 05:24:58

标签: php

我在尝试根据用户输入设置数值变量时遇到了未定义的变量问题。

我之前已经分配了变量,只是为了测试数学逻辑,通过下拉菜单选择人员选项,例如:

<option value="Less Than 20">Less Than 20</option>
<option value="Less Than 50">Less Than 50</option>

根据所选内容创建多个if / else if语句:

if($head_count == "Less Than 20"){
            $head_count = 15;
            $taskMeetings_int = $head_count * 1.6;
            $taskMeetings_low = 420 * $taskMeetings_int;
            $taskMeetings_high = 1250 * $taskMeetings_int;
}else if($head_count == "Less Than 50"){
            $head_count = 40;
            $taskMeetings_int = $head_count * 1.6;
            $taskMeetings_low = 420 * $taskMeetings_int;
            $taskMeetings_high = 1250 * $taskMeetings_int;
}

但是在将实际数字输入到未定义变量的表单时,现在无法使用变量。我认为将HeadcountSquare Footage的变量设置为null会读取输入的数字。

以下是我目前的PHP表单:

<form id="Corporate" action="<?php the_permalink(); ?>" method="post">
    <div class="col-lg-6">
    <p>Head Count<span>*</span></p>
    <input min="0" id='' type="number" value placeholder="Enter the Headcount" name="head_count" required>

    </div>
    <div class="col-lg-6">
    <p>Square Footage<span>*</span></p>
    <input min="0" type="number" value placeholder="Enter the Square Footage" name="sq_ft" required> 

    </div>
    <div class="col-lg-6">
        <p>What's your territory?<span>*</span></p>
        <select name="territory" required>
            <option value="">Select One</option>
            <option value="Louisiana">Louisiana</option>
            <option value="Mississippi">Mississippi</option>
        </select>
    </div>
    <div class="col-lg-6">
        <p>Email<span>*</span></p>
        <input style="width:100%;" type="email" name="email" required>
    </div>
    <div class="col-lg-12">
        <p style="text-align:center;"><i>Don't forget to hit submit below!</i></p>
    </div>
        <div class="col-lg-12" style="text-align:center; margin-top:15px;">
            <input class="BudgetSubmit" type="submit" name="submit">
        </div>
</form>

< ?php
     //run math depending on user input
            $head_count = null; // I previously set this to be a random number Ex: $head_count = 55; for testing purposes
            $taskMeetings_int = $head_count * 1.9;
            $taskMeetings_low = 520 * $taskMeetings_int;
            $taskMeetings_high = 1050 * $taskMeetings_int;

            $square_footage = null; // I previously set this to be a random number Ex: $square_footage = 13000; for testing purposes
            $conf_tables_int = $square_footage/5000;
            $conf_tables_low = 1050 * $conf_tables_int * .1;
            $conf_tables_high = 2000 * $conf_tables_int * .16;

?> 

我是否需要在表单中为人数和平方英尺设置一个id?提前谢谢。

编辑:我很抱歉,以下是提交表单后出现的内容:

<?php

    //my resource library function
    function mrllinks($link,$title,$image){
        echo "<p><a target='_blank' href='$link' title='$title'><img style='width:100%;' src='$image'/></p>";
    }

    function RepLayout1($link,$image,$name){
        echo "<div class='flex-item'>
                    <a target='_blank' href='$link'><img src='$image'/></a>                
             </div>";
    }



    // displays results after initial submission
    if($submitted){
        $lowTotal = $taskMeetings_low + $guestLounge_low + $gen_tables_low + $workspaceStation_low + $workspaceOffice_low + $techErgo_low + $ancillary_low + $taskLighting_low + $soundMasking_low;
        $highTotal = $taskMeetings_high + $guestLounge_high + $gen_tables_high + $workspaceStation_high + $workspaceOffice_high + $techErgo_high + $ancillary_high + $taskLighting_high + $soundMasking_high;
      ?>


      <div class="col-lg-12 corporatecalculator_answer">
            <p style="margin:0px; padding-top:5px; padding-bottom:5px;"><b style="color:#000;">Total estimated budget (Based on list prices):</b><?php echo '<b style="color:#EE3B33;"> &#36;'.number_format($lowTotal).' - '.number_format($highTotal) .'</b>';?><p>
            <hr>
             <div class="BudgetResultsBorderHeader"><b style="color:#000;">Task & Meeting Chairs:</b><?php echo '&nbsp; &#36;'.number_format($taskMeetings_low).' - '.number_format($taskMeetings_high);?></div>
              <?php if ($territory == "Louisiana"){?>
          <div class=" flex-container "> <?php
              RepLayout1('https://stackoverflow.com','/wp-content/uploads/2017/06/2.jpg','Stack Overlflow');  ?>
          </div>
        <?php  
              }     
                else if($territory == "Mississippi"){?>
          <div class=" flex-container "> <?php
              RepLayout1('https://stackoverflow.com','/wp-content/uploads/2017/06/3.jpg','Stack Overlflow');  ?>
          </div>             
        <?php
                }

        } ?> 


        </div>

        <?php
    } 

?>

1 个答案:

答案 0 :(得分:0)

一旦我删除了两个变量的null并将其设置为:

,就会出现实际结果
    $head_count = (int)$_POST['head_count'];
    $square_footage = (int)$_POST['sq_ft'];

感谢@MagnusEriksson和@Justinas提供的PHP 101.