1

On the first page (index.php) I wrote this:

<?php   
echo '<form action="test.php" method="get">';
$x=0;
while($x<5){
$x++;
$test='tester'.$x;
echo '<input type="text" name="$test"><br>';
};
echo '<input type="submit" value="submit">';
?>

on the second page (test.php) I wrote this:

<?php 
echo $_POST['tester1'];
echo $_POST['tester2'];
echo $_POST['tester3'];
echo $_POST['tester4'];
echo $_POST['tester5'];
?>

and when I tested it I got these errors

Notice: Undefined index: tester1 in C:\xampp\htdocs\test.php on line 2

Notice: Undefined index: tester2 in C:\xampp\htdocs\test.php on line 3

Notice: Undefined index: tester3 in C:\xampp\htdocs\test.php on line 4

Notice: Undefined index: tester4 in C:\xampp\htdocs\test.php on line 5

Notice: Undefined index: tester5 in C:\xampp\htdocs\test.php on line 6

The code below is an example for the real code being meant for users to fill in a number. I want to use these numbers later for calculations so they all need an unique name. The <input> objects are generated via a loop, the amount of times the loop runs is specified by the amount of rows in a database.

7
  • 4
    In single-quotes, PHP variables are not passed as variables, but the actual text will be output. It should look like echo '<input type="text" name="'.$test.'"><br>'; Commented Feb 2, 2016 at 14:26
  • we tested this and it didnt work Commented Feb 2, 2016 at 14:29
  • 1
    You also have method="get", so they are passed in the URL instead over POST. Change it to method="POST". Apply both changes of what I commented, that should work. Commented Feb 2, 2016 at 14:31
  • okay thanks it worked Commented Feb 2, 2016 at 14:32
  • like what @Qirel said, these are correction you need to make, on top of it, i hope you didn't miss out your closing form tag. Commented Feb 2, 2016 at 14:37

3 Answers 3

1

The two main issues are that you are using PHP variables inside single quotes, this doesn't pass the actual variable, but the actual name. As an example

$foo = "bar";
echo 'This is $foo';

would print

This is $foo

If you use double-quotes, the variable's content would be passed,

$foo = "bar";
echo "This is $foo"; // You can also do the following: echo 'This is '.$foo;

would print

This is bar

Secondly, you are using method="get" in your form, but trying to retrieve them as POST variables. This means that you have to change it to method="POST".


Another alternative, is to create an array of elements, and use a loop in PHP to retrieve the values. An example given below. The first snippet generates a form with 5 input-fields as an array.

<form action="test.php" method="POST">
    <?php for ($x=0; $x<5; $x++) { ?>
    <input type="text" name="tester[]" />
    <?php } ?>
    <input type="submit" name="submit" />
</form>

And in PHP, loop through that array.

<?php
if (isset($_POST['tester']) {
    // If the values are set, to avoid Undefined Index notices
    foreach ($_POST['tester'] as $value) {
        echo $value."<br />";
    }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

This is how it works (index.php):

<?php
    echo '<form action="test.php" method="post">';
    $x=0;
    while($x<5){
        $x++;
        $test='tester'.$x;
        echo '<input type="text" name="'.$test.'"><br>';
    };
    echo '<input type="submit" value="submit">';
?>

2 Comments

Can't use method="get" when trying to retrieve them as POST in PHP.
yeah its because i had tried it with method get and post and I didnt change it right when i posted the question
0

simply append this code to the top of test.php

if(!isset($_POST['tester1']) || !isset($_POST['tester2']) ... etc){
     exit(0); //or do something to signal index.php of missing values
}
/* (the rest of the code */

When writing a dynamic page like this, you should always expect there to be a missing variable and code an escape so you never run into an error such as that.

Comments

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.