0

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
    }
    ?>
Chezshire
  • 713
  • 5
  • 13
  • 32
  • Error can be seen here: http://zephir.seattlecentral.edu/~jstein11/itc250/z14/w03c0102_OOPform/w03c0102_OOPform.php – Chezshire May 25 '14 at 13:10

1 Answers1

2

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....
}
Dusan Plavak
  • 4,457
  • 4
  • 24
  • 35
  • Thank you that solved my initial problem. I now have an error when the ints post to the form after clicking the submit, it's general the same thing ( 84 Error message: Undefined variable: rsTot) Do i need to redeclare it again? is this another question? – Chezshire May 25 '14 at 13:38
  • what is on that lines? 85-89? – Dusan Plavak May 25 '14 at 13:44
  • starting with line #85: echo '

    Total Red Shirts Appearing: ' . $rsTot . '
    '; echo 'Red Shirts Still Breathing at End of Episode: ' . $rsSurvived . '
    '; echo 'Red Shirt Episode Survival Ratio: ' . $rsRatio . '!

    ';
    – Chezshire May 25 '14 at 13:47
  • I set up a codeshare here: http://codeshare.io/8qzYO CodeShare will allow you to see my actual code line for line with line numbers (I just found this and it's pretty cool i think). – Chezshire May 25 '14 at 13:47
  • Looking at how you revised the code Dusan, does that mean that you have made the to ints 'global'? That is what i was trying to do from the beginning and i gained a lot of clarity on what global is from doing this, but i've also gained some confusion because some things did and some things didn't work. – Chezshire May 25 '14 at 14:02
  • yes, you make them "global", basically everything what you declare inside {} is a local variable with you can "see" only inside block {} – Dusan Plavak May 25 '14 at 14:04
  • Thanks, it's nice to know that i was looking in the right direction, but i seem to not be implementing the code correctly as undefined 'rsTot' and 'rsSurvived' and 'rsRatio' are still showing as undefined on submit :( – Chezshire May 25 '14 at 14:08
  • I did catch the missing quote on the #4 of your example solution and updated the codeshare. But that didn't resolve error. Errors, they're pesky – Chezshire May 25 '14 at 14:09
  • sorry for misleading you, if you will look over there http://www.php.net/manual/en/language.variables.scope.php you can read that in the function you need use global key word. – Dusan Plavak May 25 '14 at 14:16