tedhan
tedhan

Reputation: 73

Combine variable with string in a input name of form in PHP

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?

Upvotes: 0

Views: 291

Answers (1)

PlanetCloud
PlanetCloud

Reputation: 312

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.

Upvotes: 2

Related Questions