Jump to content

[SOLVED] Counting Variables


rvdb86

Recommended Posts

Hi, I hope someone can help me out!

 

I have created a script that allows my users to dynamically create a form giving each field their own atributes. The form by default has the action sendform.php and method=post.

 

the problem is i do not know how many fields the user has created and their names  :( is there any way in php that i can get ALL posted variables into an array or something?

 

I would really appreciate any suggestions!

Link to comment
Share on other sites

wow aebstract thanks for the quick reply!

 

My knowldege of arrays is really weak and i have never 100% understood them  :-[

do you know of a good tutorial that could fill me in or do you have a small example i could look at?

 

Sorry for my lack of knowledge but through trial and error i hope to learn

Link to comment
Share on other sites

Once you start to get them down, they can be fairly easy and very very useful. First look here: http://us3.php.net/manual/en/function.array.php, read through till you get to user comments, that will help you out in understanding how they are setup and their form.

 

I'm not sure I have any quick easy examples, but if you read over that page and take a stab at it, you can come ask on anything you get stuck at and we can work through it.

Link to comment
Share on other sites

hi Keith,

 

Could you please explain in more detail? If the form method is post would i still use

$_REQUEST as $field => $value

 

or would i use:

$_POST as $field => $value

 

am i on the right track with this:?

$i = 0;

while($i < count($field){
echo $value;
$i++
}

 

thanks for the suggestions!

Link to comment
Share on other sites

Hi

 

There are several arrays of variables in php when a form is submitted. $_POST is just the post method ones, $_GET is the get method ones and $_REQUEST is both combined. So you could quite happily use $_POST as $field => $value.

 

foreach ($_REQUEST as $field => $value) is looping through all the variables in the $_REQUEST array (ie all the POST and GET items), with each turn through the loop dealing with one field with the field name in $field and the value in $value.

 

You cannot use your count example. You would need something like :-

 

$i=0;
foreach ($_POST as $field => $value)
{
echo "$field $value <br />";
$i++
}
echo $i;

 

This would give you a list of the field names and their values, and then the count of them at the end of it.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.