0

I'm new to PHP, and I'm trying to write a iteration that post a value to another page with auto-increment, this is the code:

$j = 1;
    if (isset($emails))
    {
      foreach ($emails as $email)
      {
        echo "<input type='text' name='email_[$j]' value='{$email['email']}' form='saveForm'><br>";
        $j++;
      }
    }

Then on the other page, I could access the value by

>

for ($i=1; $i<=$email_count; $i++)
{
  $email = mysqli_real_escape_string($conn, $_POST['email_'.$i]);
  }

The error shows

Undefined index: email_4

So I'm pretty sure it's problem of the first code that should pass the value as "email_1", anyone knows what is correct way to combine the string and variable in the input name?

1 Answer 1

2

The code echo "<input type='text' name='email_[$j]' value='{$email['email']}' form='saveForm'><br>"; makes the name of the input to be email_[1].
Change it to name='email_$j' or name='email_{$j}' and you're good to go.

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

1 Comment

Thanks! It works! I tried in a new page and it turned the problem was caused by nested forms, anyway, thank you!

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.