I'm trying to create a loop to create 20 text input fields for username & 20 text input fields for password and my current attempt is listed below. It isn't working & if I view source the $i variable doesn't seem to be expanding properly.

<?php
    for ($i = 0; $i < 20; $i++) {
        echo ('<input type="text" id="username . $i" class="userInfo" value="<?php echo $userName1; ?>" />');
        echo ('<input type="text" id="password . $i" class="userInfo" value="<?php echo $password1; ?>" />');
    }
?>
echo "<input type='text' id='username'". $i." class='userInfo' value='".$userName1."'/>";

use the above code i hope you will get solved your issue thanks!

If you want to use a variable inside a string then the string must be enclosed in double quotes.

$var = 'foo';
echo "Variable value: $var";

will display:

Variable value: foo

while

$var = 'foo';
echo 'Variable value: $var';

will display:

Variable value: $var

See http://www.php.net/manual/en/language.types.string.php

Try this
input_form.php

<?php
	echo "<form method = 'POST' action = 'process.php'>";
	for($counter = 0; $counter < 10; $counter++)
	{
		echo "<input type = 'text' name = 'username[]' class = 'class_name'/><br/>";
		echo "<input type = 'password' name = 'password[]' class = 'class_name'/><br/>";
	}
	echo "<input type = 'submit' value = 'SEND'/>";
	echo "</form>";
?>

Now to process inputs from the above form, do something like this.
process.php

<?php
	$usernames = array();
	$passwords = array();
	
	$usernames = $_POST['username'];
	$passwords = $_POST['password'];
	
	for($counter = 0; $counter < sizeof($usernames); $counter++)
	{
		echo "Username #".($counter + 1).": ".$usernames[$counter]."<br />";
		echo "Password #".($counter + 1).": ".$passwords[$counter]."<br />";
	}
?>

i want to input number of form i want to generate please i need a code that does that.
eg. if i want to register 50 students at a time with the following fileds first name lastname sex addres class date of birth and submit. if i submit it should go to the database at once.

i want to input number of form i want to generate please i need a code that does that.
eg. if i want to register 50 students at a time with the following fileds first name lastname sex addres class date of birth and submit. if i submit it should go to the database at once.

Member Avatar for diafol

Start your own thread. Include any code you've tried

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.