2017-07-03 99 views
0

當嘗試根據用戶輸入設置數值變量時,我正在運行到未定義的變量問題。如何根據用戶輸入設置一個數值變量

我以前曾分配只是通過一個下拉菜單前選擇一個選項,人數驗證了數學邏輯變量:如果if語句基礎/其他關

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

,並創建多個選擇什麼:

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 
    } 

?> 
+0

什麼和你在哪裏得到錯誤?將var設置爲'null'實際上會使其爲空,而不是*讀取輸入的數字*。另外你的HTML與PHP部分無關。 – Justinas

+0

@Justinas感謝您讓我知道我的HTML部分不會導致錯誤,並且將var設置爲null將導致它無法讀取輸入的數字。我得到的確切的錯誤是,當我輸入我爲兩個變量手動輸入的數字時,數學結果顯示爲零。在這種情況下,我會將變量設置爲「'; ? – Gilgamesh90

+0

我沒有得到你的PHP如何從你的窗體獲得值?您的代碼中是否缺少部件?如果沒有,您需要了解服務器端(PHP)和客戶端(HTML/JS)之間的區別。順便說一句,你目前正在設置一個變量($ head_count)爲空,那麼你試圖將它作爲一個數字?這將被視爲零,並使用零乘法將會導致零。 –

回答

0

實際結果出現過一次,我刪除了這兩個變量空,並將其設置爲:

$head_count = (int)$_POST['head_count']; 
    $square_footage = (int)$_POST['sq_ft']; 

謝謝@MagnusEriksson和@Justinas的PHP 101.

相關問題