Jump to content

Passing multiple variables...


jcbarr

Recommended Posts

I am creating a page that will allow a user to input a lot of data into a table. The number of values that will be passed for each entry is 4. For example each entry will have the following fields;

POS
NAME
LEAGUE
STAT

The user will be able to add as many entries as they see fit. So this could result in a bunch of variables. So my question is this. When the form is submitted what would be the best way to gather all of the variables so that I can insert them into the database? I'm a little stuck as to what to do. Should I name them pos1, pos2... name1...name2...? Or is there a better way to do this?

I don't want to have a hundred lines like this in my code;

[code]$pos1 = $_POST['pos1'];[/code]

I hope that I have explained this well enough. If you need more info I will be more than glad to give it to you.

Anyone have any ideas on how to do this? I have passed variables from a form before, but that was when I knew exactly what variables were being passed and exactly how many there would be, so I'm not a complete newb...

Thanks in advance to anyone willing to take the time to help.
Link to comment
Share on other sites

The problem with that is that I don't know how many there will be coming from the form.

I was wondering if there was some way to grab everything being sent with the POST command and dump them in to arrays or something like that and then do a loop where it dumps every value of the array in to a table field? I'm not sure if that can be done, and I'm not too good with arrays, but I was thinking it might be able to be done.

The problem with the form is that the user will select how many entries they make.
Link to comment
Share on other sites

try this
[code]<input type="text" name="pos[]">
<input type="text" name="name[]">
<input type="text" name="league[]">
<input type="text" name="stat[]">

<input type="text" name="pos[]">
<input type="text" name="name[]">
<input type="text" name="league[]">
<input type="text" name="stat[]">
[/code]

and you'll get postdata like this when the form is submitted
[code]<?php
$_POST['pos'][0]
$_POST['name'][0]
...

$_POST['pos'][1]
$_POST['name'][1]
...
?>[/code]

So then you can use a loop
[code]foreach($_POST['pos'] as $k=>v)
{
  echo $v;
}[/code]

hope this helps.
Link to comment
Share on other sites

[quote author=ryanlwh link=topic=102628.msg407599#msg407599 date=1154469098]
[code]<?php
$_POST['pos'][0]
$_POST['name'][0]
...

$_POST['pos'][1]
$_POST['name'][1]
...
?>[/code]

So then you can use a loop
[code]foreach($_POST['pos'] as $k=>v)
{
  echo $v;
}[/code]

hope this helps.
[/quote]

Okay I set the form names like you did, but do I still have to grab all of them with the POST lines?
Link to comment
Share on other sites

Yeah so I guess I'm not going to be able to do what I wanted. I thought there might be some way to do a catch all. Like some fucntion that would catch all the pos[] variables being passed in one shot rather than having to put multiple lines to catch each one.
Link to comment
Share on other sites

didn't you see this??
[code]<?php
foreach($_POST['pos'] as $k=>v)
{
  echo $v;
}
?>[/code]


if you want to catch "everything" easily
[code]<?php
foreach($_POST as $k=>$v)
{
  if($k=='pos')
  {
      foreach($_POST[$k] as $v)
      {
          //do stuff with each pos
      }
  }
}
?>[/code]

or in your original way
[code]foreach($_POST as $k=>$v)
{
  if (strpos($k,'pos')!== false)
    //do stuff for pos
  if (strpos($k,'name') !== false)
    //do stuff for name
}[/code]
Link to comment
Share on other sites

Yeah I did and when I add that to my code I get this error;

Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /public_html/dynamic/all_star/allstar.php on line 10

This is the only code so far in the script;

[code]<?php
$create = $_GET['CREATE'];
$edit = $_GET['EDIT'];

foreach($_POST['pos'] as $k=>v)
{
  echo $v;
}
?>[/code]
Link to comment
Share on other sites

ryanlwh if you use the foreach to get the array and post the information would you have to set the name according to the foreach to insert the information to the database.

cheers.
so you got to get the correct name for the insert but also use the $_POST statement i dont see any other way.

<input type="text" name="pos[]">
<input type="text" name="name[]">
<input type="text" name="league[]">
<input type="text" name="stat[]">
<?php
foreach($_POST['pos'] as $k=>$v) {

$name=$v[1];
$league=$v[2];
stat=$v[3];

}

if($_POST['submit']){

insert code

}

?>
Link to comment
Share on other sites

[quote author=redarrow link=topic=102628.msg407640#msg407640 date=1154471620]
ryanlwh if you use the foreach to get the array and post the information would you have to set the name according to the foreach to insert the information to the database.

cheers.
so you got to get the correct name for the insert but also use the $_POST statement i dont see any other way.

<input type="text" name="pos[]">
<input type="text" name="name[]">
<input type="text" name="league[]">
<input type="text" name="stat[]">
<?php
foreach($_POST['pos'] as $k=>$v) {

$name=$v[1];
$league=$v[2];
stat=$v[3];

}

if($_POST['submit']){

insert code

}

?>
[/quote]
:) this will give you error :) $v is not an array
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.