1

I have a form in a loop that iterates 4 times. I want to process and print the form data dynamically.

This is not showing the required output. Am I doing something wrong ?

index.php

<?php
for($i = 1; $i<5; $i++)
{
?>
<form action = 'index.php' method = 'post'>
<input type = 'text' name = 'name<?php echo $i; ?>'>

<?php
}
?>

<input type = 'submit' value = 'submit'>

<?php

for($i = 1; $i<5; $i++)
{
$namee = $_POST['name.$i'];
echo $namee;
}

?>

2 Answers 2

1

The problem is in this line :

$namee = $_POST['name.$i']; //you cannot concatenate variables this way,

it should be (note where the single quote is):

$namee = $_POST['name'.$i];

Sign up to request clarification or add additional context in comments.

Comments

0

Single quotes not evaluate variables, you can use concatenation:

 $namee = $_POST['name'.$i];

Or double quotes

$namee = $_POST["name$i"];

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.