2012-02-26 24 views
1

我只是在學習html。我需要編寫解決二次方程式的代碼。我嘗試在HTML中嵌入PHP代碼,但得到空白輸出。如何獲取用戶值a,b,c並顯示條件回答?提交表格來計算二次方程

+2

請張貼您的代碼。 – 2012-02-26 16:24:25

回答

0

編輯:從源頭上學習,官方PHP網站:http://php.net/manual/en/tutorial.forms.php

1,創建你想要的字段的表格。 <form method='post' ....>...</form>

2.用戶提交表單,然後編寫一個獲取發佈數據($_POST) 的PHP代碼,並根據二次方程公式對其進行處理。

3. Echo結果。

3

這是簡單你需要做的一個例子。首先做一個HTML表單:

<form method="post" action="index.php"> 
    <input type="text" name="a" value="Enter 'a'" /> 
    <input type="text" name="b" value="Enter 'b'" /> 
    <input type="text" name="c" value="Enter 'c'" /> 
    <input type="submit" name='calc' value="Calculate" /> 
</form> 

還有就是你的表格。現在計算:

<?php 
if (isset($_POST['calc'])) //Check if the form is submitted 
{ 
    //assign variables 
    $a = $_POST['a']; 
    $b = $_POST['b']; 
    $c = $_POST['c']; 
    //after assigning variables you can calculate your equation 
    $d = $b * $b - (4 * $a * $c); 
    $x1 = (-$b + sqrt($d))/(2 * $a); 
    $x2 = (-$b - sqrt($d))/(2 * $a); 
    echo "x<sub>1</sub> = {$x1} and x<sub>2</sub> = {$x2}"; 
} else { 
    //here you can put your HTML form 
} 
?> 

你需要對它做更多的檢查,但正如我之前所說的,這是一個簡單的例子。

0

我有一個小例子。

該文件將數據從表單發送到它自己。當它發送一些東西 - 條件的結果

$_SERVER['REQUEST_METHOD']=='POST' 

是正確的。如果它的真實服務器進程代碼在「if」塊中。它將從表單發送的數據分配給2個變量,然後將它們添加並存儲在「$ sum」變量中。顯示結果。

<html> 
    <body>  
     <form method="POST"> 

      <p> 
      A: <br /> 
       <input name="number_a" type="text"></input> 
      </p> 

      <p>B: <br /> 
       <input name="number_b" type="text"></input> 
      </p> 

      <p> 
       <input type="submit"/> 
      </p> 

     </form> 

<?php 


    if ($_SERVER['REQUEST_METHOD']=='POST') // process "if block", if form was sumbmitted 
    { 
     $a = $_POST['number_a'] ; // get first number form data sent by form to that file itself 
     $b = $_POST['number_b'] ; // get second number form data sent by form to that file itself 

     $sum = $a + $b; // calculate something 

    echo "A+B=" . $sum; // print this to html source, use "." (dot) for append text to another text/variable 
    } 

?> 

    </body> 
</html> 

您需要PHP服務器來測試/使用它! PHP文件必須由創建頁面的Web服務器處理。從磁盤打開php文件不起作用。如果你需要更多的解釋 - 請在評論中提出。