How to insert all $_POST text field values into array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

How to insert all $_POST text field values into array

Post by ddragas »

How to insert all $_POST text field values into array?


form_file.php

Code: Select all

$m = 6;//this could be any number
		
	for ($i=0; $i < $m; $i++)
      	{
	  	$broj = $i+1;
      		echo "<td width=\"40%\" align=\"right\">" . "Polje " . $broj . ": ";
		echo "</td><td>"; 
		echo "<input type="text" name="polje$i" />";
		echo "</td>";
		echo "</tr>";
      	}
array_file.php

Code: Select all

$polje=array();
//would it be something like this?
foreach($_POST as $data)
{
$polje[]=$data;
}
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Your avatar scares me.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yes, it would be something like that...



and Mr Bean rules.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Mr Bean is legend! 8) 8) 8)


Thank you for help
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

I also have on form 3 drop down menues and submit button.

How to avoid inserting selected values from drop down menues and submit button into array?
Gambler
Forum Contributor
Posts: 246
Joined: Thu Dec 08, 2005 7:10 pm

Post by Gambler »

Use naming convention. f_name for forms, t_name for text fields, etc. Or use arrat names like forms[name].
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

I have tried this but it is not working

Code: Select all

foreach($_POST as $data)
		{
			
			if($data['name'] == "broj")
				{
					$broj = $data;
				}
			
			if(($data['name'] <> "broj") and ($data['name'] <> "Submit"))
			{
				echo $data . "<br>";
			}
		} 
		echo "Total " . $broj . " fields.";
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What is not working??

You can try

Code: Select all

if($data['name'] != "broj" and $data['name'] != "Submit") {
   echo $data . "<br>";
}
edit | I don't think you understood what is happening wih the data, perhaps your looking for something more like

Code: Select all

foreach ($_POST as $fieldName => $value) {
   if ($fieldName != 'broj' && $fieldName != 'Submit') {
      echo $value .'<br>';
   }
}
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

data['name '] filtering is not working. :evil:

Here is complete code

Code: Select all

<?

if(isset($_POST['Submit']))
	{
		foreach($_POST as $data)
		{
			
			if($data['name'] == "broj")
				{
					$broj = $data;
				}
			
				if($data['name'] != "broj" or $data['name'] != "Submit")
				{
				   echo $data . "<br>";
				}  
		}
		echo "Total  " . $broj . " fields.";	
	}
		else
	{

$m = 6;
		
		echo "<br>";
		echo "<form enctype=\"multipart/form-data\" action=\"\" method=\"POST\">";
		echo "<table width=\"80%\"  border=\"0\" align=\"center\" bordercolor=\"#000000\">";
		echo "<input type=\"hidden\" name=\"broj\" value=\"$m\">";
		for ($i=0; $i < $m; $i++)
      	{
	  		$broj = $i+1;
      		echo("<td width=\"40%\" align=\"right\">" . "Slika " . $broj . ": " . 
			"</td><td>" . 
			"<input type=text name=polje" . $i ." />
			</td>
		    </tr>");
      	}
		echo "<td></td></tr>";
        echo "<td></td><td><input type=\"submit\" name=\"Submit\" value=\"Pridruži / izmjeni slike\" onclick=\"value='Molim pri&#269;ekajte...'\" class=\"txt\">";
        echo "<input name=\"Reset\" type=\"reset\" id=\"Reset\" value=\"Poništi\" class=\"txt\"></td></tr>";
	   
		echo "</table>";
		echo "</form>";
	}
?>
and result is (afters inserting values from 1 - 6 into text fields):

6
1
2
3
4
5
6
Molim prièekajte...
Total fields.

result should be;

1
2
3
4
5
6

Total 6 fields
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Jcart wrote:What is not working??

You can try

Code: Select all

if($data['name'] != "broj" and $data['name'] != "Submit") {
   echo $data . "<br>";
}
edit | I don't think you understood what is happening wih the data, perhaps your looking for something more like

Code: Select all

foreach ($_POST as $fieldName => $value) {
   if ($fieldName != 'broj' && $fieldName != 'Submit') {
      echo $value .'<br>';
   }
}

Thank you for help
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Ok

here comes another problem with $_POST values (array)

form_file.php

in form x number of rows

Code: Select all

name         surname
       name         surname
       name         surname
       name         surname
       name         surname
       name         surname
       name         surname

       etc...
how to inser into db posted values



insert into users (name, surname) values ('$name', '$surname')
etc..etc..etc..

code to create form

Code: Select all

$m = 19;
		
		echo "<br>";
		echo "<form enctype=\"multipart/form-data\" action=\"\" method=\"POST\">";
		echo "<table width=\"80%\"  border=\"0\" align=\"center\" bordercolor=\"#000000\">";
		echo "<input type=\"hidden\" name=\"broj\" value=\"$m\">";
		for ($i=0; $i < $m; $i++)
      	{
	  		$broj = $i+1;
      		echo("<td width=\"40%\" align=\"right\">" . "Slika " . $broj . ": " . 
			"</td><td>" . 
			"<input type=text name=surname" . $i ." />
			</td>
			<td>
			<input type=text name=name" . $i ." />
			</td>
		    </tr>");
      	}
		echo "<td></td></tr>";
        echo "<td></td><td><input type=\"submit\" name=\"Submit\" value=\"Accept\" onclick=\"value='Please wait...'\" class=\"txt\">";
        echo "<input name=\"Reset\" type=\"reset\" id=\"Reset\" value=\"Reset\" class=\"txt\"></td></tr>";
	   
		echo "</table>";
		echo "</form>";
Post Reply