我只是在學習html。我需要編寫解決二次方程式的代碼。我嘗試在HTML中嵌入PHP代碼,但得到空白輸出。如何獲取用戶值a,b,c並顯示條件回答?提交表格來計算二次方程
1
A
回答
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文件不起作用。如果你需要更多的解釋 - 請在評論中提出。
相關問題
- 1. ajax表格在第二次提交時提交兩次,第三次提交時提交3次等
- 2. 二次方程式因子計算
- 3. 計算二次方程的根。 C++
- 4. IE提交表格兩次
- 5. AJAX表單提交 - 每次點擊提交表格i ++次數
- 6. ajax表單提交第二次表單提交如果成功
- 7. 如何編程二次型計算器?
- 8. 提交和計算
- 9. 第二次調用後未提交表格
- 10. ajax表單將提交後第二次提交按ckeditor
- 11. PHP二次方程計算器奇怪的輸出
- 12. 目標C中的二次方程計算
- 13. 如何計算MIPS裝配中二次方程的根?
- 14. 二次方程
- 15. Node.js:遠程提交表格
- 16. 自動錶單提交和計算
- 17. 每第二次只計算一次
- 18. Caculator - 第一次計算顯示,但第二次不計算
- 19. 僅自動提交一次表格
- 20. 表格在php中提交兩次
- 21. 聯繫表格提交兩次
- 22. 顯示提交表格的次數
- 23. jQuery對話框多次提交表格
- 24. jQuery Ajax發送第二次提交兩次,第三次提交三次
- 25. jQuery Ajax發送第二次提交兩次,第三次提交三次
- 26. 切換提交按鈕一次提交表格?
- 27. 根據來自其他表格的值計算出現次數
- 28. 提交表格
- 29. 提交表格
- 30. 提交表格
請張貼您的代碼。 – 2012-02-26 16:24:25