Jump to content

Can a $_POST[$variable] use a variable in the argument position?


Recommended Posts

I have a form that creates rows of data input textboxes depending on a user input number of things.  I have a naming convention for all these textboxes that basically just keeps incrementing a number suffix for each row.  All this is working fine.

 

My problem is I need to get the data inserted into this table of textboxes into an array.

 

Here's my code where I attempt to to this (it does not work):

 

$temp = $_SESSION['Num_Part'];
$count = 1;
while ($count <= $temp){
  $temp2[$count] = "'Participant_P".$count."'";
  //echo $temp2[$count]."<br/>";
  $temp3[$count]=$_POST[$temp2[$count]];  //here's the problem
  $temp4[$count] = "'Result_P".$count."'";
  $temp5[$count]=$_POST[$temp4[$count]];  //here's the problem
  //echo $temp4[$count]."<br/>";
  $count++;
}

 

The problem is that the $_POST does not work with the variable in the argument position - even though the argument is formatted with single quotes.

 

Can a variable be used in a POST argument and if so what is the correct syntax?

 

If not, is there some other simple solution to harvest the data into an array.  I understand I can harvest by explicitly accessing each key in the post assoc array.  But this could be dozens of rows of input fields.

 

Thanks in advance for your help here.  I couldn't find anything online re this topic.

You added single quotes inside $temp2[$count], meaning that you're trying to get the value of $_POST["'Participant_P1'"] for example, which won't work.

Do this instead:

  $temp2[$count] = "Participant_P".$count;

 

But you should really make your input boxes like this:

<input type="text" name="Participants[]">
<input type="text" name="Participants[]">
<input type="text" name="Participants[]">
<input type="text" name="Participants[]">
<input type="text" name="Participants[]">
<input type="text" name="Participants[]">
<input type="text" name="Participants[]">
<input type="text" name="Participants[]">

That way you get a nice array in $_POST like this:

Array
(
    [Participants] => Array
        (
            [0] => Jane
            [1] => Dane
            [2] => Grain
            [3] => Pain
            [4] => Cain
            [5] => Bane
            [6] => Jake
            [7] => Cake
        )

)

Thank you for your reply.

 

I thought I understood what you were saying but I cannot get it to work.

 

First, re naming input boxes...  You say to name them all as one array name leaving the argument blank.  Does php automatically assign and increment the argument with each subsequent input box?

 

So I will have textboxes named:

Participant_P[0]

Participant_P[1]

Participant_P[2]

etc

etc

 

Then in my routine that harvests the user inputs from these textboxes...

 

I'm confused still about how to access each array value.

 

Is it $_POST['Participant_P[1]'] or $_POST['Participant_P1']?

 

I tried both ways and neither seem to work.

 

Thanks for your time.

HTML:

<code>

<input type="text" name="Participant_P[]">

<input type="text" name="Participant_P[]">

<input type="text" name="Participant_P[]">

</code>

 

PHP:

<code>

$P0 = $_POST['Participant_P'][0];

$P1 = $_POST['Participant_P'][1];

$P2 = $_POST['Participant_P'][2];

</code>

BlueSkyIS,

 

That is the syntax I was looking for.  Thank you.

 

And I with this code I can harvest the inputs in a while loop that is based on the random number the user inputs to generate the number of different participants:

 

$temp = $_SESSION['Num_Part'];
$count = 0;
while ($count <= $temp){
  $temp3[$count]=$_POST['Participant_P'][$count];
  $temp5[$count]=$_POST['Result_P'][$count];
  $count++;
}

 

Many thanks.

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.