Using For To Create HTML Input Box and Retreiving Results with Post

Hi

I am trying to use a For statement to create multiple input boxes:

<?php for ($i = 1; $i <= $x; $i++);?>
<post Form>
<input class-"text-primary" type="text" class="form-control" name="username<?php$i;?>" maxle    ngth="40" id="usernameField" placeholder="Your Username" />

<submit type button>
And then retreiv the results using POST

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
        $trimmed = array_map('trim', $_POST);
        for ($j = 1; $j < $attendeeNO; $j++)
{
        // Check for a username:
        if (preg_match ('/^\w{4,20}$/', $trimmed["username$j"])) {
                $un = mysqli_real_escape_string ($dbc, $trimmed["username$j"]);
        } else {
                echo '<p class="error">Please enter username' .$j. '!</p>';
        }
}
}

However, I cannot seem to retreive the HTML form input boxes and mainpulate them with the $_POST array.

The error is that the index is undefined?

The problem is that the index is not correctly being passed to the post array upon submission. I have been trying various forms of quotation for the submission but am wondering if I am correctly retrieving / looping through the $_POST form upon submission.

Any thoughts?

Thanks,
Karen

use

<input type="text" name="username[]">

for defining a list of inputs. this also gives you an array in POST, so you can use foreach() directly.

Whatever is that??? You probably mean:

<form method="post">

but that’s just a guess.

Thanks Dormlich,

I’m a little rusty on this (mixing up C) It is still throwing an error:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
        $trimmed = array_map('trim', $_POST);
        foreach ($trimmed['username'] as $username)
        {
                echo $username;
        }
} 

echo '<input class-"text-primary" type="text" class="form-control" name="username[]" maxlength="40" id="usernameField" placeholder="Your Username" />

Produces:

Warning: trim() expects parameter 1 to be string, array given in xx/xx/xx on line 20

Notice: Undefined index: username in /xx/xx/ on line 21

Warning: Invalid argument supplied for foreach() xx/xx/ on line 21

Sorry, let me be more precise:

// server response once submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
        $trimmed = array_map('trim', $_POST);
        foreach ($trimmed['username[]'] as $username)
        {
                echo $username;
        }
}

?>
<!-- html form -->
<form class="form" action="x.php" method="POST">

<div class="form-group">
                        <?php for ($i = 1; $i <= $attendeeNO; $i++)
                        {
                         echo '<label class="text-primary" for="nameField">Student Username '. $i .'</label>';
                         echo '<input class-"text-primary" type="text" class="form-control" name="username[]" maxlength="40" id="usernameField" placeholder="Your Username" />
                        ';
                        }
                        ?>

Is throwing the following errors:

Warning: trim() expects parameter 1 to be string, array given xx.php on line 20

Notice: Undefined index: username xx.php on line 21

Warning: Invalid argument supplied for foreach() xx.php on line 21

Off Topic

@karentutor1: when you post code on the forums, you need to format it so it will display correctly.

You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

Thanks, yes I was wondering why half of my code was disappearing and then magically ‘re-appearing’.

Thanks for fixing and ok!

1 Like

Hi

I have figured it out, the suggestion up above cracked it for me.

Thanks!

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
        $trimmed = array_map('trim', $_POST['username']);

        foreach ($trimmed as $value)
        {
                //echo $value;
                if (preg_match ('xx', $value)) {
                        echo $value . '<br />';
                }
                else echo 'Please enter username at least 4 characters long and containing letter or number characters';
        }
}
<div class="form-group">
                        <?php for ($i = 1; $i <= $attendeeNO; $i++)
                        {
                         echo '<label class="text-primary" for="nameField">Student Username '. $i .'</label>';
                         echo '<input class-"text-primary" type="text" class="form-control" name="username[]" maxlength="40" id="usernameField" placeholder="Your Username" />
                        ';
                        }
                        ?>
                        </div>

for creating multiple textboxes

<?php 
$a = 0;
while ($a == $x)
{
    echo '<input type="text" name="username[]">';
}
?>

in this code you will recive on post request in single array of username
like $a = $_POST['username'];
then you will have all value of textboxces in array
 
your mistake is in this line :
<input class-"text-primary" type="text" class="form-control" name="username<?php$i;?>" maxlength="40" id="usernameField" placeholder="Your Username" />
 
 
in name you have given  ' username<?php$i?>'
this is not good way still if you want to do like then you have to like 
name = username<?=$i?>  
or
name = username<?php echo $i; ?>
 
in these method you will recive this username in post request like 
$_POST['username1']
$_POST['username2']

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.