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
https://forums.phpfreaks.com/topic/16253-passing-multiple-variables/
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.
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.
[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?
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]
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]
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 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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.