簡體   English   中英

PHP在表單上提交后如何保持按鈕的適當檢查

[英]PHP after submit on a form how to I keep the button appropriate checked

我正在建立一個計算器,我快完成了。 在我的表格上,我在頂部有2個單選按鈕,分別用於SAE單位或公制單位。

當我單擊“計算”時,計算器將正常運行,但是我選中的單選按鈕又回到未選中狀態。 如果有人單擊度量標准,然后單擊“提交”,那么如何在結果后保持選中度量標准單選按鈕,這與SAE相同?

這是我的整個代碼:

<?php
if (isset($_POST['units'])) $units = $_POST['units'];
if (isset($_POST['dia'])) $dia = $_POST['dia'];
if (isset($_POST['wt'])) $wt = $_POST['wt'];
if (isset($_POST['L'])) $L = $_POST['L'];
if (isset($_POST['num'])) $num = $_POST['num'];
if (isset($_POST['waste'])) $waste = $_POST['waste'];
if (isset($_POST['surface'])) $surface = $_POST['surface'];
$denom = ($units == "US" ? 12 : 100);
$coverage = ($units == "US" ? 1000 : 92.9);
//$measurements = ($units == "US" ? "in" : "cm");
$measurements == $units;
if($units == 'US')
$measurements = 'in';
else if($units == 'Metric')
$measurements = 'cm';
else 
$measurements = 'in or cm';
$length == $units;
if($units == 'US')
$length = 'feet';
else if($units == 'Metric')
$length = 'meters';
else 
$length = 'feet or meters';
$answer1 = pi() * ($dia / $denom) * $L * $num * ($waste + 1) / $coverage;
$answer2 = pi() * (($dia + ($wt * 2)) / $denom) * $L * $num * ($waste + 1) / $coverage;
//$answer = ($answer1 + $answer2);
$answer = ($answer);
if($surface == TRUE)
$answer = ($surface * ($waste + 1) / $coverage);
else 
$answer = ($answer1 + $answer2);

echo <<<_END
<form method='post' action=''>
<table border='0' width='500px' cellpadding='2' cellspacing='1' class="table">
<tr class="calcheading"><td><label><input type="radio" name="units" value="US" />S.A.E.        </label>
<label><input type="radio" name="units" value="Metric" />Metric</label> </td></tr>
<tr class="calcheading"><td colspan="2"><strong>ConBlock MIC Circular Surface     Area</strong></td></tr>
<tr class="calcrow"><td>Surface Area ($length):</td><td align="center"><input type='text' name='surface' value="$surface"/></td></tr>
<tr class="calcrow"><td>Diameter ($measurements):</td><td align="center"><input type='text' name='dia' value="$dia"/></td></tr>
<tr class="calcrow2"><td>Wall Thickness ($measurements):</td><td align="center"><input type='text' name='wt' value="$wt"/></td></tr>
<tr class="calcrow"><td>Section Length ($length):</td><td align="center"><input type='text' name='L' value="$L"/></td></tr>
<tr class="calcrow"><td>Number of Sections:</td><td align="center"><input type='text' name='num' value="$num"/></td></tr>
<tr class="calcrow"><td>Waste Variance % (Please add in decimal form):</td><td     align="center"><input type='text' name='waste' value="$waste"/></td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>

<tr class="calcrow">
<td><i>Gallons Needed for Interior Coating:</td>
<td align="center"><input type="text" value="<?php echo round($answer1, 2)?>"></td></i>
</tr>
<tr class="calcrow">
<td><i>Gallons Needed for Exteror Coating:</td>
<td align="center"><input type="text" value="<?php echo round($answer2, 2)?>"></td></i>
</tr>
<tr class="calcrow">
<td><i>Total Gallons of ConBlock MIC needed:</td>
<td align="center"><input type="text" value="<?php echo round($answer, 2)?>"></td></i>
</tr>
</table>
</form>

我到處都在尋找答案,但我想我不確定在這個問題上該尋找什么。 看看我的意思是網址:

http://www.launchrun.com/consealtest/ConBlockMIC-circular.php

感謝任何可以幫助我闡明此主題的人!

<input type="radio" name="units" <?php if (isset($_POST['units']) && $_POST['units'] == "Metric"){ print("checked=\"checked\""); } ?> value="Metric" />

您可以這樣:

<?php

$units = null;

if (!empty($_POST)) {
    $units = $_POST['units'];
}

?>

<input type="radio" name="units" value="Metric" <?php echo (($units == 'Metric') ? 'checked="checked"' : ''); ?> />
<input type="radio" name="units" value="US" <?php echo (($units == 'US') ? 'checked="checked"' : ''); ?> />

通過檢查輸入的units的值,我們可以確定輸入元素上已checked屬性是否應該存在。

<?php
function selected( $val = "")
{
    $units = isset( $_POST['units'] ) ? $_POST['units'] : null;
    echo ( $units == $val ) ? "CHECKED='CHECKED'" : "";
}
$units = isset( $_POST['units'] ) ? $_POST['units'] : null;

$dia = isset( $_POST['dia'] ) ? $_POST['dia'] : null;

$wt = isset( $_POST['wt'] ) ? $_POST['wt'] : null;

$L = isset( $_POST['L'] ) ? $_POST['L'] : null;

$num = isset($_POST['num']) ? $_POST['num'] : null;

$waste = isset($_POST['waste']) ? $_POST['waste'] : null;

$surface = isset($_POST['surface']) ? $_POST['surface'] : null;

$denom = ( ( $units == "US" ) ? 12 : 100 );

$coverage = ( ( $units == "US" ) ? 1000 : 92.9);


$measurements = 'in or cm';
if( $units == 'US' ){
    $measurements = 'in';
}
if($units == 'Metric')
{
    $measurements = 'cm';
}




if($units == 'US')
$length = 'feet';
else if($units == 'Metric')
$length = 'meters';
else 
$length = 'feet or meters';

$answer1 = pi() * ($dia / $denom) * $L * $num * ($waste + 1) / $coverage;
$answer2 = pi() * (($dia + ($wt * 2)) / $denom) * $L * $num * ($waste + 1) / $coverage;
$answer = ($answer1 + $answer2);
$answer = ($answer);
if($surface == TRUE)
$answer = ($surface * ($waste + 1) / $coverage);
else 
$answer = ($answer1 + $answer2);

?>
<form method='post' action=''>
<table border='0' width='500px' cellpadding='2' cellspacing='1' class="table">
<tr class="calcheading"><td><label>
<input type="radio" name="units" <?php selected("US")?> value="US" />S.A.E.</label>
<label>
<input type="radio" name="units"  <?php selected("Metric")?> value="Metric" />Metric</label> </td></tr>
<tr class="calcheading"><td colspan="2"><strong>ConBlock MIC Circular Surface     Area</strong></td></tr>
<tr class="calcrow"><td>Surface Area (<?php echo $length ?>):</td><td align="center"><input type='text' name='surface' value="<?php echo $surface ?>"/></td></tr>
<tr class="calcrow"><td>Diameter (<?php echo $measurements ?>):</td><td align="center"><input type='text' name='dia' value="<?php echo $dia ?>"/></td></tr>
<tr class="calcrow2"><td>Wall Thickness ($measurements):</td><td align="center"><input type='text' name='wt' value="<?php echo $wt ?>"/></td></tr>
<tr class="calcrow"><td>Section Length ($length):</td><td align="center"><input type='text' name='L' value="<?php echo $L ?>"/></td></tr>
<tr class="calcrow"><td>Number of Sections:</td><td align="center"><input type='text' name='num' value="<?php echo $num ?>"/></td></tr>
<tr class="calcrow"><td>Waste Variance % (Please add in decimal form):</td><td     align="center"><input type='text' name='waste' value="<?php echo $waste ?>"/></td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
<tr class="calcrow">
<td><i>Gallons Needed for Interior Coating:</td>
<td align="center"><input type="text" value="<?php echo round($answer1, 2)?>"></td></i>
</tr>
<tr class="calcrow">
<td><i>Gallons Needed for Exteror Coating:</td>
<td align="center"><input type="text" value="<?php echo round($answer2, 2)?>"></td></i>
</tr>
<tr class="calcrow">
<td><i>Total Gallons of ConBlock MIC needed:</td>
<td align="center"><input type="text" value="<?php echo round($answer, 2)?>"></td></i>
</tr>
</table>
</form>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM