0

i declared array score, and move data input HTML from user to array score PHP here

<?php
    $score= [];

    if(isset($_POST['send'])){
        for ($i = 0; $i < 2; $i++)
            $score[] = $_POST['ID_score'];
    }
    
?>

so the user inputed data from here

    <form method="post" action="index.php">
        <?php for($i = 0; $i < 2; $i++){ ?>
            <label>input your ID score -[<?php echo $i++ ?>] : </label>
            <input type="text" name='ID_score'>
        <br>
        <?php } ?>

        </br><input type="submit" name="send" value="send it">

and i make a var dump to see vakue array of index...

        <?php if(isset($_POST['send'])){ ?>
            <h3><?php var_dump($score); ?></h3>
        <?php } ?>

i input 2 score, first value i'm input 100 , in the second textbox i'm input 200
but the output array is : array(1) { [0]=> string(1) "200" }

why the output just one last array index? even though i enter two data value 100 and 200

4
  • change input name attribute like this: <input type="text" name='ID_score[<?php echo $i++ ?>]'> Commented Jun 28, 2022 at 5:21
  • how attribute in $_POST ??? Commented Jun 28, 2022 at 5:47
  • It's because you are using the same name ('ID_Score') for both inputs. Since the last one you enter data for is 200 that is what you get. You either need different names or you need to use array syntax for the input name as in <input type="text" name='ID_score[]'> Commented Jun 28, 2022 at 5:49
  • Also, don't use a for loop in your post handler. Use foreach($_POST['ID_score'] as $value) { $score[] = $value; } Commented Jun 28, 2022 at 5:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.