简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM