Newb는 고전적인 스타 트렉 에피소드에서 얼마나 많은 빨간 셔츠가 죽었는지 계산하기 위해 int와 함께 작동하는 양식을 구축하려고합니다 (forms 및 int로 더 나아지기위한 운동). '7 오류 메시지 : 정의되지 않은 인덱스 : rsTot'라는 오류가 있습니다. 나는 몇 가지를 시도하고 그것을 float로 설정하는 것이 int로 작업하는 가장 안전한 방법이라는 것을 읽었습니다. 그러나 내가 시도한 각 것은 일종의 오류가 발생하므로 하루를 읽고 시도한 후에 나는 매우 혼란스럽고 여기서 이해할 수있는 대답을 기대하고 있습니다.
나는 대답을 위해 stackOverflow를 읽었지만 나중에 수학을 할 수있는 정수로 처음 입력하고 처리하는 문제에 대해 이해하고 적용 할 수있는 것을 보지 못했습니다.
<?php //w03c0102_OOPform
require '../inc_0700/config_inc.php'; #provides configuration, pathing, error handling, db credentials
//END CONFIG AREA ----------------------------------------------------------
$rsTot = (float)$_POST['rsTot'];// float more forgiving int
$rsSurvived = (float)$_POST['rsSurvived'];
$rsSum = $rsTot - $rsSurvived;
$rsRatio = 0;
# Read the value of 'action' whether it is passed via $_POST or $_GET with $_REQUEST
if(isset($_REQUEST['act'])){$myAction = (trim($_REQUEST['act']));}else{$myAction = "";}
switch ($myAction)
{//check 'act' for type of process
case "display": # 2)Display user's name!
showName();
break;
default: # 1)Ask user to enter their name
showForm();
}
function showForm()
{# shows form so user can enter their name. Initial scenario
get_header(); #defaults to header_inc.php
?>
<script type="text/javascript" src="<?=VIRTUAL_PATH;?>include/util.js"></script>
<script type="text/javascript">
function checkForm(thisForm)
{//check form data for valid info
if(empty(thisForm.YourName,"Field Empty, please fill out")){return false;}
return true;//if all is passed, submit!
}
</script>
<p align="center"><?=smartTitle();?></p>
<h3 align="center">Star Trek Classic</h3>
<h2 align="center">Death-Shirt Calculator</h2>
<form action="<?=THIS_PAGE;?>" method="post" onsubmit="return checkForm(this);">
<p align="center">Classic Star Trek Episode Name<br />
<input type="text" name="epName" /><br /><br />
Esitmated number of officers with red shirts<br />
<input type="text" name="rsTot" /><br /><br />
Esitmated number of officers with red shirts<br />
to actually survive the episode<br />
<input type="int" name="rsSurvived" /><br /><br />
<input type="submit" value="Go!">
</p>
<input type="hidden" name="act" value="display" />
</form>
<?php
get_footer(); #defaults to footer_inc.php
}
function showName()
{#form submits here we show entered name
get_header(); #defaults to footer_inc.php
if(!isset($_POST['epName']) || $_POST['epName'] == '')
{//data must be sent
feedback("No form data submitted"); #will feedback to submitting page via session variable
myRedirect(THIS_PAGE);
}
if(!ctype_alnum($_POST['epName']))
{//data must be alphanumeric only
feedback("Only letters and numbers are allowed. Please re-enter your name."); #will feedback to submitting page via session variable
myRedirect(THIS_PAGE);
}
$epSubmitted = strip_tags($_POST['epName']);# strip data entered
echo '<h3 align="center">' . smartTitle() . '</h3>';
echo '<p align="center">Episode Name: <b>' . $epSubmitted . '</b><br />';
echo '<p align="center">Total Red Shirts Appearing: <b>' . $rsTot . '<br />';
echo 'Red Shirts Still Breathing at End of Episode: <b>' . $rsSurvived . '<br />';
echo 'Red Shirt Episode Survival Ratio: <b>' . $rsRatio . '</b>!</p>';
echo '<p align="center"><a href="' . THIS_PAGE . '">RESET</a></p>';
get_footer(); #defaults to footer_inc.php
}
?>
오류가 말했듯 $_POST['rsTot']
이 색인이 존재하지 않으며 페이지를 처음로드 할 때 POST 값이 없기 때문에 간단합니다.
따라서 기본적으로 다음과 같이해야합니다.
//your hidden field, in other words if form was submitted
if(isset($_POST['act']))
{
$rsTot = (float)$_POST['rsTot'];// float more forgiving int
$rsSurvived = (float)$_POST['rsSurvived'];
$rsSum = $rsTot - $rsSurvived;
$rsRatio = 0;
and do all other stuff based on form input here....
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다