ICubis2 Posted April 8, 2011 Share Posted April 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233137-can-a-_postvariable-use-a-variable-in-the-argument-position/ Share on other sites More sharing options...
dcro2 Posted April 8, 2011 Share Posted April 8, 2011 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/233137-can-a-_postvariable-use-a-variable-in-the-argument-position/#findComment-1198987 Share on other sites More sharing options...
ICubis2 Posted April 9, 2011 Author Share Posted April 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233137-can-a-_postvariable-use-a-variable-in-the-argument-position/#findComment-1199256 Share on other sites More sharing options...
BlueSkyIS Posted April 9, 2011 Share Posted April 9, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/233137-can-a-_postvariable-use-a-variable-in-the-argument-position/#findComment-1199359 Share on other sites More sharing options...
ICubis2 Posted April 9, 2011 Author Share Posted April 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/233137-can-a-_postvariable-use-a-variable-in-the-argument-position/#findComment-1199403 Share on other sites More sharing options...
sasa Posted April 10, 2011 Share Posted April 10, 2011 or just $temp = $_SESSION['Num_Part']; //$count = 0; //while ($count <= $temp){ $temp3=$_POST['Participant_P']; $temp5=$_POST['Result_P']; // $count++; //} Quote Link to comment https://forums.phpfreaks.com/topic/233137-can-a-_postvariable-use-a-variable-in-the-argument-position/#findComment-1199541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.