Start New Discussion within our Web Development Community

so here it is, first you will input how many textfield you have then click ok after clicking ok input the value in all the textfield after that if you click add you will see the sum of all the value in the textfield help pls

<html>
<head>
<title>Margz</title>
</head>

<form method="POST" action="">
<body>
<p>Number:
  <label for="textfield"></label>
<input type="text" name="textfield" id="textfield" />
<input type="submit" name="button1" id="button1" value="OK" />
<input type="submit" name="button2" id="button2" value="ADD" />
</p>
</body>
</form>

<?PHP
function display ($x)
{
    for($i=1;$i<=$x;$i++)
{
    echo "Num " . $i ." ------------------ <br>";
?>

<input type="text" name=<?PHP echo $i;?>

<?PHP
echo "<br>";
echo "<br>";
}}
?>

<?PHP
if(isset($_POST['button1']))
{
    display($_POST['textfield']);
}

if(isset($_POST['button2']))

{



}

?>
</html>

Are there multiple text fields for the values?

Or, are the values entered like:

1234567

OR

1, 2, 3, 4, 5, 6, 7

This isn't tested.. But I think this is what you mean..

<?php

    function display($x)
    {
        $sum = (int) 0;
        foreach($x as $xs)
        {
            $sum += $xs;
        }

        return $sum;
    }

    if(!isset($_POST['submitted']))
    {
        echo '<form method="post" action="">';
        for($i=0; ($i < 10); $i++)
        {
            echo '<input type="text" name="values[]" id="textfield" /><br />';
        }
        echo '<input type="submit" name="button2" id="button2" value="ADD" />';
        echo '<input type="hidden" name="submitted" value="TRUE">';
    }else{

        $values = $_POST['values']; 

        echo display($values);  
    }


?>

Your problem looks like this:

You cannot have your text fields with id's such as "1", "2", "3", "4" etc..

You need to pass them as an array, so in your html just have values[] this implies that there is an array of fields.

You can then pass this array into a function to calculate the values.

Hope this makes sense.

Notice: Undefined index: values in - on line 62

Warning: Invalid argument supplied for foreach() in - on line 43
0

<html>

<form method="POST" action="">
<body>
<p>Number:
  <label for="textfield"></label>
<input type="text" name="textfield" id="textfield" />
<input type="submit" name="button1" id="button1" value="OK" />
<input type="submit" name="button2" id="button2" value="ADD" />
</p>
</body>
</form>





<?PHP
function display ($x)
{
    for($i=1;$i<=$x;$i++)
{
    echo "Num " . $i ." ------------------ <br>";
?>

<input type="text" name="values[]" id="textfield">

<?PHP
echo "<br>";
echo "<br>";
}}
?>


<?PHP
  function get($x)
    {
        $sum = (int) 0;

        foreach($x as $xs)
        {
            $sum += $xs;
        }
        return $sum;
    }

?>

<?PHP
if(isset($_POST['button1']))
{
    display($_POST['textfield']);
}

if(isset($_POST['button2']))

{

 $values = $_POST['values']; 

 echo get($values);  

}

?>
</html>
This article has been dead for over six months. Start a new discussion instead.