PHP表单未声明

Newb here trying to build a form that works with ints to count how many red shirts die in a classic episode of star trek (exercise to get better with forms and ints). I have an error which reads as '7 Error message: Undefined index: rsTot'. I have tried a few things and read that setting it(casting it?) as a float was the safest way to work with the int, however each thing i've attempted results in some sort of error so after a day of reading and trying i'm very confused and hoping for an answer I can understand here.

I have read through stackOverflow for an answer but not seen one which i can understand and apply that speaks to the problem i'm having of initially entering and process a number as an int which i can later do some math on.

    <?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
    }
    ?>
dongqiu5184
dongqiu5184 错误可以在这里看到:zephir.seattlecentral.edu/~jstein11/itc250/z14/w03c0102_OOPform/...
6 年多之前 回复

1个回答



正如错误所说, $ _ POST ['rsTot'] </ code>该索引不存在且它是 很简单因为在第一次加载页面时你没有任何POST值... </ p>

所以基本上你需要做这样的事情:</ p>

 <  code> //你的隐藏字段,换句话说,如果表单已提交
if(isset($ _ POST ['act']))
{
$ rsTot =(float)$ _ POST ['rsTot']; // 浮动更宽容int
$ rsSurvived =(float)$ _ POST ['rsSurvived'];

$ rsSum = $ rsTot - $ rsSurvived;
$ rsRatio = 0;

并执行所有其他操作 基于表单输入的东西....
}
</ code> </ pre>
</ div>

展开原文

原文

Well as the error say, $_POST['rsTot'] that index does not exist and it is simple because on first load of page you do not have any POST values...

So basicaly you need to do something like this:

//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....
}

dounieliang4712
dounieliang4712 全局关键字 - 我会去阅读。
6 年多之前 回复
dsfsda121545
dsfsda121545 很抱歉误导你,如果你在那里看看php.net/manual/en/language.variables.scope.php,你可以阅读在你需要的功能中使用全局关键词。
6 年多之前 回复
dqgo99177
dqgo99177 我确实在你的示例解决方案的#4上找到了缺失的引用并更新了代码共享。 但这并没有解决错误。 错误,他们讨厌
6 年多之前 回复
dqde43215
dqde43215 谢谢,很高兴知道我正在寻找正确的方向,但我似乎没有正确实现代码,因为未定义'rsTot'和'rsSurvived'和'rsRatio'仍然显示为提交时未定义:(
6 年多之前 回复
dqk77945
dqk77945 是的,你让它们“全局”,基本上你在{}内声明的一切都是一个局部变量,只能在块{}内“看到”
6 年多之前 回复
douxigai8757
douxigai8757 看看你如何修改代码Dusan,这是否意味着你已经使全局'全局'? 这就是我从一开始就想要做的事情,并且我已经从全球范围内获得了很多清晰度,但是我也因为有些事情做了而且有些事情无法奏效而引起了一些困惑。
6 年多之前 回复
dongyue8640
dongyue8640 看到更新答案
6 年多之前 回复
du4373
du4373 我在这里设置了一个代码共享:codeshare.io/8qzYO CodeShare将允许您查看我的实际代码行与行号(我刚发现这个,我认为这很酷)。
6 年多之前 回复
dp20011
dp20011 从第85行开始:echo'<p align =“center”>出现红色衬衫总数:<b>'。 $ rsTot。 '<br />'; echo'红色衬衫在剧集结束时仍然呼吸:<b>'。 $ rsSurvived。 '<br />'; echo'Red Shirt Episode Survival Ratio:<b>'。 $ rsRatio。 '!</ B> </ p>';
6 年多之前 回复
dongse7261
dongse7261 那条线上是什么? 85-89?
6 年多之前 回复
duanhongxian6982
duanhongxian6982 谢谢你解决了我最初的问题。 我现在有一个错误,当点击提交后单击post到表单,它一般是相同的事情(84错误信息:未定义的变量:rsTot)我是否需要重新声明它? 这是另一个问题吗?
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐