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
ddragas
Forum Contributor
Posts: 445 Joined: Sun Apr 18, 2004 4:01 pm
Post
by ddragas » Wed Feb 22, 2006 4:27 pm
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 » Wed Feb 22, 2006 4:30 pm
Your avatar scares me.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Feb 22, 2006 4:41 pm
yes, it would be something like that...
and Mr Bean rules.
ddragas
Forum Contributor
Posts: 445 Joined: Sun Apr 18, 2004 4:01 pm
Post
by ddragas » Wed Feb 22, 2006 4:45 pm
Mr Bean is legend!
Thank you for help
ddragas
Forum Contributor
Posts: 445 Joined: Sun Apr 18, 2004 4:01 pm
Post
by ddragas » Wed Feb 22, 2006 4:54 pm
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 » Wed Feb 22, 2006 4:55 pm
Use naming convention. f_name for forms, t_name for text fields, etc. Or use arrat names like forms[name].
ddragas
Forum Contributor
Posts: 445 Joined: Sun Apr 18, 2004 4:01 pm
Post
by ddragas » Wed Feb 22, 2006 4:58 pm
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.";
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Wed Feb 22, 2006 5:02 pm
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>';
}
}
ddragas
Forum Contributor
Posts: 445 Joined: Sun Apr 18, 2004 4:01 pm
Post
by ddragas » Wed Feb 22, 2006 5:11 pm
data['name '] filtering is not working.
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č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
ddragas
Forum Contributor
Posts: 445 Joined: Sun Apr 18, 2004 4:01 pm
Post
by ddragas » Wed Feb 22, 2006 5:23 pm
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
ddragas
Forum Contributor
Posts: 445 Joined: Sun Apr 18, 2004 4:01 pm
Post
by ddragas » Wed Feb 22, 2006 6:46 pm
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>";