473,382 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Foreach element in $_POST?

Hey

Is there a way to go through each element in the $_POST supervariable?

I'm making a page with dynamic forms, so I don't know for sure how many
$_POST variables there will be (the number of textboxes, radio buttons,
etc changes).

How can I either count them or do a foreach loop to do this?

Thanks

iw****@gmail.com

Dec 6 '05 #1
4 4581
IW****@gmail.com wrote:
Hey

Is there a way to go through each element in the $_POST supervariable?

I'm making a page with dynamic forms, so I don't know for sure how many
$_POST variables there will be (the number of textboxes, radio buttons,
etc changes).

How can I either count them or do a foreach loop to do this?

Thanks

iw****@gmail.com


iwp506,

Easy, just treat $_POST as an array and use foreach.
http://www.php.net/manual/en/control...es.foreach.php

Don't forget to make the post values safe before using them if they were
input by the users...

Carl.
Dec 6 '05 #2

Carl wrote:
IW****@gmail.com wrote:
Hey

Is there a way to go through each element in the $_POST supervariable?

I'm making a page with dynamic forms, so I don't know for sure how many
$_POST variables there will be (the number of textboxes, radio buttons,
etc changes).

How can I either count them or do a foreach loop to do this?

Thanks

iw****@gmail.com


iwp506,

Easy, just treat $_POST as an array and use foreach.
http://www.php.net/manual/en/control...es.foreach.php

Don't forget to make the post values safe before using them if they were
input by the users...

Carl.


Ok, thanks

Dec 6 '05 #3
Following on from Carl's message. . .

Easy, just treat $_POST as an array and use foreach.
http://www.php.net/manual/en/control...es.foreach.php

Don't forget to make the post values safe before using them if they were
input by the users...

Further don't forget that elements of $_POST may be arrays in their own
right. eg Checkboxes. Here is a snippet of code as illustration
#---------------------------------------------------------------------
function POSTget($VarName,$tidy=TRUE){
# read POSTed value from HTTP_POST_VARS
# Normally we'll tidy up the input to remove leading and trailing spaces
and control chars
# but this can be overridden.
#---------------------------------------------------------------------
if (isset($_POST[$VarName])){
$rv = $_POST[$VarName];
if(!is_array($rv)){
if ($tidy) {$rv = TidyInput($rv);}
}
}else{
$rv='';
}
return $rv;
}

--
PETER FOX Not the same since the porcelain business went down the pan
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Dec 7 '05 #4
fun with forms... iterate_form() not only spits out the values, but
will give you the $var = $_POST['var'] code for the receiving page...
have fun

function iterate_form()
{
echo "<b>Values:</b><br>";
foreach($_POST as $key => $value)
{
echo $key . " = " . $value . "<br>";
}
echo "<br><br><b>Happy Coding:</b><br>";
foreach($_POST as $key => $value)
{
echo "$". $key . " = \$_POST['". $key . "'];<br>";
}
build_back_form();
exit;
}

function build_back_form()
{
echo "<form name='error' method='post' action='".
$_SERVER['HTTP_REFERER'] ."'>\n";
foreach ($_POST as $key => $value)
{
echo "<input type='hidden' name='" . $key . "' value='" .
$value . "'>\n";
}
echo "<input type='submit' name='submit' value='Go
Back'></form>\n";
}

Dec 8 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Blaine HIlton | last post by:
I've been trying for 2 days now to solve this problem, which should be pretty simple, but it is turning off to not be as simple. I am trying to pull data out from a form that was POSTed on a...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
8
by: Gopal Krish | last post by:
Any sample code that uses foreach statement to walkthrough a two dimensional array? My requirement is to process data in only one dimension (say column 0 in each row of the array) Example...
9
by: Anthony Bouch | last post by:
Everything I know about looping structures says to be careful about expressions that need to be evaluated again and again for each test/increment of a loop. I came across this piece of code the...
4
by: Jack E Leonard | last post by:
I'm looping through the keys and values of a form submission using foreach($_POST as $key => $value) echo "$key = $value<br>"; No problems there. All works as expected. But seveal of the...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
14
by: rashgang | last post by:
i have dynamically created checkbox which will like yahoo mail if check box selected it will highlight a row but deleteall function is not working plz chek it function doDeleteAll($objArray){ ...
21
by: rashgang | last post by:
no check box selected when i gave delete all link the error is coming <?php include "includes/connection.php"; include "includes/Functions_category.php"; include...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.