Hello DaniWeb.

It's my first time to do PHP, so please bear with me.
I've read books and viewed tutorials just to solve this one.

I want to have a variable number of text fields. Then, I want to have the contents of those text fields loaded in an array in &_POST, because I want to be able to use those values as an array in the next page.
How do I implement that?

I tried this one, but it doesn't work:

<?php 
	$n = $_POST["var_n"];
?>

<html>
<body>
	<br>
	<center><h2>Reverse Array</h2></center>
	
	<form action="Result.php", method="post">
		<center>				
			<br>Please input the contents of the array: 
			<br>
				<?php 
					$the_array = array_fill(0, $n, 1);
					createTextBoxes($the_array, $n);
					//$array_ref = array_fill(0, $n, 1);
				?>
			<br><input type="submit" value="Next">
		</center>
	</form>
</body>
</html>
				
<?php 
	function createTextBoxes($the_array, $n){
		for($i = 0; $i < $n; $i++)
			echo "<input type=\"text\" name=\"\$the_array[", $i, "]\" size=\"3\">";
	}
?>

Recommended Answers

All 2 Replies

Member Avatar for rajarajan2017

Please test the attached file to know how to pass and read the array values from one page to another page.

Hi I hope this will give you a little information regarding how to use an array in next page.....

<?php 
	$n = 6;//$_POST["var_n"];
	echo "<pre>";
	print_r($_POST);
	echo "</pre>";
?>

<html>
<body>
	<br>
	<center><h2>Reverse Array</h2></center>
	
	<form action="", method="post">
		<center>				
			<br>Please input the contents of the array: 
			<br>
				<?php 
					createTextBoxes($n);
					//$array_ref = array_fill(0, $n, 1);
				?>
			<br><input type="submit" value="Next">
		</center>
	</form>
</body>
</html>
				
<?php 
	function createTextBoxes($n){
		for($i = 0; $i < $n; $i++)
			echo "<input type=\"text\" name='the_array[".$i."]' size=\"3\"><br />";
	}
?>

If you want to send the data to next page, add method='next.php' in form tag, in next.php page add the code print_r($_POST);

you can view the posted contents...

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.