Page 1 of 1
How to insert all $_POST text field values into array
Posted: Wed Feb 22, 2006 4:27 pm
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;
}
Posted: Wed Feb 22, 2006 4:30 pm
by Ree
Your avatar scares me.
Posted: Wed Feb 22, 2006 4:41 pm
by feyd
yes, it would be something like that...
and Mr Bean rules.
Posted: Wed Feb 22, 2006 4:45 pm
by ddragas
Mr Bean is legend!
Thank you for help
Posted: Wed Feb 22, 2006 4:54 pm
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?
Posted: Wed Feb 22, 2006 4:55 pm
by Gambler
Use naming convention. f_name for forms, t_name for text fields, etc. Or use arrat names like forms[name].
Posted: Wed Feb 22, 2006 4:58 pm
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.";
Posted: Wed Feb 22, 2006 5:02 pm
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>';
}
}
Posted: Wed Feb 22, 2006 5:11 pm
by ddragas
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
Posted: Wed Feb 22, 2006 5:23 pm
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
Posted: Wed Feb 22, 2006 6:46 pm
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>";