3

So I'm currently working on a little project that allows a user to input a number into a textbox, after clicking a button that says "add" it should store that value into an array and then allow the user to input another value into that array. There is also a button on the page when the user is finished and wants to sum the values called "Submit". The problem I'm running into is everytime the form posts back, it recreates a new blank array. Any tips?

See the code below:

<html>
<head>
</head>
<body>
<h2>Please Select your title and name:</h2>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method='post'>
    <p>
        <label for="strFirstname">Type number to add: </label>
        <input type='text' name='strNumber' id='strNumber'/>
    </p>
    <p>
        <input type='submit' name='submit' value='Add' />
    </p>
    <p>
        <input type='submit' name='calculate' value='Compute' />
    </p>
    <?php

    $array = array();

    if (isset($_POST['submit']))
    $num = $_POST['strNumber'];
    $array[] = $num;
        foreach($array as $num)
        echo $num . ' + ';

    if(isset($_POST['calculate']))
        foreach($array as $num)
            echo $num . ' + ';
    ?>
</form>
</body>
</html>
2
  • 3
    Note that arrays only "live" during the page execution. You will have to use sessions, or cookies, or hidden inputs, or whatever else. However, for me it sounds like a client-side solution (JS, for example) will be the most suitable. Commented Nov 10, 2015 at 4:54
  • I was thinking about using a session, but am unfamiliar with the process @YeldarKurmangaliyev Commented Nov 10, 2015 at 4:58

1 Answer 1

1
<?php
session_start();
?>
<html>
    <head>
    </head>
    <body>
        <h2>Please Select your title and name:</h2>
        <form action='' method='post'>
            <p>
                <label for="strFirstname">Type number to add: </label>
                <input type='text' name='strNumber' id='strNumber'/>
            </p>
            <p>
                <input type='submit' name='submit' value='Add' />
            </p>
            <p>
                <input type='submit' name='calculate' value='Compute' />
                <input type='submit' name='clear' value='clear' />
            </p>
            <?php
                if (isset($_POST['submit'])) {
                    if(!array_key_exists("numbers", $_SESSION)) {
                        $_SESSION["numbers"] = array();
                    }

                    array_push($_SESSION["numbers"], $_POST["strNumber"]);
                }

                if(isset($_POST['clear'])) {
                    $_SESSION["numbers"] = array();
                }

                if(array_key_exists("numbers", $_SESSION)) {
                    echo implode("+", $_SESSION["numbers"]);
                }

                if(isset($_POST['calculate'])) {
                    if(array_key_exists("numbers", $_SESSION)) {
                        $expression = implode("+", $_SESSION["numbers"]);
                        eval( '$result = (' . $expression . ');' );
                        echo "=" . $result;
                    }
                }
            ?>
        </form>
    </body>
</html>
  • Start a session
  • When the action is "submit"
    • Check if the session which will store the numbers is initialized
    • If it's not initialize it as an array
    • Finally push the number into the array
  • Check if there is a session initialized if there is print all the numbers ( you can use implode to do that)
  • if the action is calculate .. just make the calculation ( check eval function )
Sign up to request clarification or add additional context in comments.

3 Comments

This is working, now I just need to echo all the numbers such as if I input 5, 6, 8. I need to echo 5 + 6 + 8 = 19
Sorry SO is buging me this is not the entire source 1 min
What did you change, I don't see the difference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.