Send the specific value in inputs generated with a FOR

0

I have this problem I am generating a list of inputs with the same name but different value.

<?php    
    if(isset($_POST['SubmitButton'])){ //check if form was submitted
      $input = $_POST['inputText']; //get input text
      $message = "Success! You entered: ".$input;
    }    
?>

<html>
<body>    
<form action="#" method="post">
<?php echo $message; ?>
<?php 

    for($i = 0; $i<10; $i++){
        echo '<input type="text" name="inputText" value="'.$i.'"/>
              <input type="submit" name="SubmitButton"/>';
    }
?>
</form>    
</body>
</html>

When you click on some input, it returns the last assigned value. I guess that's why. As each input is called equal, it assigns the last value.

Any guidance to solve this problem?

I need each input to send the assigned value ..

Thank you very much

    
asked by Cesar Urrego Zapata 08.03.2018 в 17:54
source

3 answers

0

Hello! When you work with multiple HTML elements with the same name, and you want to handle them as an array or array, you must add the brackets to the name. This in your case would apply to both the input and the buttons:

for($i = 0; $i<10; $i++){
    echo '<input type="text" name="inputText[]" value="'.$i.'"/>
          <input type="submit" name="SubmitButton[]"/>';
}

Remember that this only applies to the name attribute, not the id, which should always be unique.

EDITED: I forgot to mention that later in PHP, you receive the values as array:

$inputs = $_POST["inputText"];
print_r($inputs);
// Esto mostraría algo como:
// Array( [0] => "hola", [1] => "mundo"....)
    
answered by 08.03.2018 / 18:46
source
1

A simpler solution: "surround" each "input" with a "form" instead of making a single form.

<?php
$message='First time here'; //Esto lo he añadido yo para que no dé un error la primera vez

if (isset ( $_POST ['SubmitButton'] )) { // check if form was submitted
    $input = $_POST ['inputText']; // get input text
    $message = "Success! You entered: " . $input;
}

?>

<html>
<body>

<?php echo $message; ?>
<?php

for($i = 0; $i < 10; $i ++) {
    echo '<form action="#" method="post"><input type="text" name="inputText" value="' . $i . '"/>
              <input type="submit" name="SubmitButton"/><br/></form>';
}
?>

</body>
</html>
    
answered by 17.04.2018 в 20:26
0

I have modified your code a bit but you can do what you want, depending on which button you press, it takes the value out of the input. I hope it serves you.

<html>
    <body>
    <form action="#" method="post">

        <?php    
        for($i = 0; $i<10; $i++)
        {
            echo "<button name='boton$i'>Enviar</button>
            <input type='text' name='inputText$i' value='$i'/><br>";

            if (isset($_POST["boton".$i])) //check if form was submitted
            { 
                $input = $_POST["inputText".$i]; //get input text
                $message = "Success! You entered: " . $input;
            }

        }
        echo '<br>';
        if(isset($message))echo $message;
        ?>

    </form>
    </body>
</html>

I assigned to each button and input a number, the same one you use in the for, to identify it and thus know which button you press and what input value to collect.

    
answered by 08.03.2018 в 18:55