0

how to assign different names to text box using loop? I've tried some coding but it doesn't catch the value I entered.

Sorry for my lack coding skills. Thank you in advance.

if ($result->num_rows > 0) 
{
    while($row = $result->fetch_assoc()) 
    {
        echo $row["criteria_name"];
        echo '<input name="$criteria_name" type="textbox"/> <br/><br/>';
    }
}
Anis
  • 23
  • 6

3 Answers3

2

You can only use variables inside of string while using double quotes

This will work:

echo "Hello world $variable";

While this will not:

echo 'Hello world $variable';

I can see, however, that it may be an issue because there's double quotes in the HTML, this leaves you with a few options.

Option 1

Use double quotes to encase the string, and then escape the existing double quotes:

echo "<input name=\"$criteria_name\" type=\"textbox\"/> <br/><br/>";

Option 2

Concatenate the strings:

echo '<input name="' . $row['criteria_name'] . '" type="textbox"/> <br/><br/>';

Option 3

Use the alternative syntax for the control structures, and use raw HTML:

if ($result->num_rows > 0):
    while($row = $result->fetch_assoc()):
    ?>
        <?= $row["criteria_name"]; ?>
        <input name="<?= $criteria_name; ?>" type="textbox"/><br/><br/>
    <?php
    endwhile;
endif;
Daniel
  • 1,229
  • 14
  • 24
0

I assume that you want to name $row['criteria_name'] as input field name

    echo '<input name="'.$row['criteria_name'].'" type="textbox"/> <br/><br/>';

Please check the options provided by the accepted answer on how to concatenate variable within a string in the below post PHP - concatenate or directly insert variables in string

Community
  • 1
  • 1
manian
  • 1,418
  • 2
  • 16
  • 32
-1

Try this:

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo '<input name="'.$row["criteria_name"].'" type="textbox"/> <br/><br/>';
    }
}
Tim Levinsson
  • 597
  • 2
  • 7
  • 20