Jump to content

Recommended Posts

I want to create a calculator that allows you to type in how many pieces of data you have, or how many averages you have. Once that is done, the amount of boxes pop up correctly - and you can enter in your data. But how can I get all those pieces of data from EACH box using one form?

 

Current Example:  http://novacms.vacau.com/

 

Code:

 

<?php

$boxes = $_POST['input'];

if(!$boxes)
{
echo '<b>How many pieces of data do you have?</b> <form action="index.php" method="POST"><input type="text" name="input"><input type="submit"></form><br/><font size="2">Ex: 88.8,92.3,76.5 - That would be 3 pieces of data</font>';
}
else
{
echo '<form action="index.php" method="POST">';
for($i = 0; $i <= $boxes; $i++)
{
   echo '<input type="text"><br/>';
} 
   echo '<input type="submit" value="Get average"></form>';
}
?>		

Link to comment
https://forums.phpfreaks.com/topic/212815-question-about-multiple-boxes/
Share on other sites

If the user is inputting comma delimited list of data eg

88.8,92.3,76.5

Then you'll want to use explode to get each number in the list.

$data = explode(',', $_POST['input']);

 

Now $data will be contain array of each individual number, eg $data[0] will be 88.8, $data[1] will be 92.3 etc. Seeing as $data is an array you'd use a foreach loop to output each number within its own form field.

echo '<form action="index.php" method="POST">';
$i = 0;
foreach($data as $number)
{
    echo ++$i . ': <input type="text" name="data[]" value="'.$number.'" /><br />';
}
echo '</form>';

If the user is inputting comma delimited list of data eg

88.8,92.3,76.5

Then you'll want to use explode to get each number in the list.

$data = explode(',', $_POST['input']);

 

Now $data will be contain array of each individual number, eg $data[0] will be 88.8, $data[1] will be 92.3 etc. Seeing as $data is an array you'd use a foreach loop to output each number within its own form field.

echo '<form action="index.php" method="POST">';
$i = 0;
foreach($data as $number)
{
    echo ++$i . ': <input type="text" name="data[]" value="'.$number.'" /><br />';
}
echo '</form>';

 

Can you give me an example of that code in use? I'm a bit confused. :/

<?php

if(!isset($_POST['input']))
{
    echo '
    <b>How many pieces of data do you have?</b>
    <form action="" method="POST">
    <input type="text" name="input">
    <input type="submit"></form><br/>
    <font size="2">Ex: 88.8,92.3,76.5 - That would be 3 pieces of data</font>';
}
else
{
    $data = explode(',', $_POST['input']);

    echo '<form action="" method="POST">';
    $i = 0;
    foreach($data as $number)
    {
        echo ++$i . ': <input type="text" name="data[]" value="'.$number.'" /><br />';
    }
    echo '</form>';
}
?>

<?php

if(!isset($_POST['input']))
{
    echo '
    <b>How many pieces of data do you have?</b>
    <form action="" method="POST">
    <input type="text" name="input">
    <input type="submit"></form><br/>
    <font size="2">Ex: 88.8,92.3,76.5 - That would be 3 pieces of data</font>';
}
else
{
    $data = explode(',', $_POST['input']);

    echo '<form action="" method="POST">';
    $i = 0;
    foreach($data as $number)
    {
        echo ++$i . ': <input type="text" name="data[]" value="'.$number.'" /><br />';
    }
    echo '</form>';
}
?>

 

This is what I have:

 

<?php

if(!isset($_POST['input']))
{
    echo '
    <b>How many pieces of data do you have?</b>
    <form action="index.php" method="POST">
    <input type="text" name="input">
    <input type="submit"></form><br/>
    <font size="2">Ex: 88.8,92.3,76.5 - That would be 3 pieces of data</font>';
}
else
{
    if($_POST["data"])   
    {
      $add = $data + $data;
      $answer = $add / $_POST["amount"];
    }
    else
    {
    $data = explode(',', $_POST['input']);

    echo '<form action="index.php" method="POST">';
    $i = 0;
    foreach($data as $number)
    {
        echo ++$i . ': <input type="text" name="data[]" value="'.$number.'" /><br />';
    }
    echo '<input type="hidden" name="amount" value="'. $i .'"><input type="submit" value="Average"></form>';
    }
}
?>	

 

I'm trying to click "Average", and then see the average of all of the numbers. But I don't know how to get all of the numbers from the one form.

Try this instead

<?php

if(!isset($_POST['input']))
{
    echo '
    <b>How many pieces of data do you have?</b>
    <form action="" method="POST">
    <input type="text" name="input">
    <input type="submit" name="submit" value="Submit"></form><br/>
    <font size="2">Ex: 88.8,92.3,76.5 - That would be 3 pieces of data</font>';
}
else
{
    if(!is_array($_POST['input']))
        $data = explode(',', $_POST['input']); // get data from first form
    else
        $data = $_POST['input']; // get data from secound form

    echo '
    <b>Data Values</b>
    <form action="" method="POST">';
    $i = 0;
    foreach($data as $number)
    {
        echo ++$i . ': <input type="text" name="input[]" value="'.$number.'" /><br />';
    }
    
    // work out the average
    $dataSum = array_sum($data);
    $average = $dataSum / $i;
        
    echo '
    <b>Average:</b>: ' . $average . '<br />
    <input type="submit" name="submit" value="Recalculate Average" />
    </form>';
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.