theITvideos Posted October 22, 2010 Share Posted October 22, 2010 Hi there, I am working on a PHP form and running a loop to generate textboxes at runtime. This code output the textboxes: foreach ($MyTest as $tst) { echo "<input name='testName' type='text' size='12' maxlength='5' />"; } I am able to display (or output) 4 or sometimes 6 textboxes depending on the variable $MyTest. Now after I enter the values in these texboxes. Lets say, it outputs 3 textboxes, I woud like to display the values like: In Textbox 1 you entered: In Textbox 2 you entered: In Textbox 3 you entered: How can I get (or fetch) values from the textboxes generated through the loop. Please reply. All comments and feedbacks are always welcomed! Thank you Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/ Share on other sites More sharing options...
atrum Posted October 22, 2010 Share Posted October 22, 2010 So you just want to get the values from an array and for each one display an text input with the values of the array? Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125072 Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 The way you're generating them will make it so that only the value of the last one will be passed through the $_POST array because they'll all have the same name= attribute. Why are you using a foreach() loop on an array, then not using any of the values from that array? Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125075 Share on other sites More sharing options...
atrum Posted October 22, 2010 Share Posted October 22, 2010 I'm half awake so I may have missed something, or maybe it just feels like it. btw I assumed the array was $_POST. foreach($_POST as $key => $value){ echo "<input name=\"$key\" value=\"$value\" type='text' size='12' maxlength='5' />"; } If that's wrong, someone please help this guy that knows what they are doing haha. Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125076 Share on other sites More sharing options...
theITvideos Posted October 22, 2010 Author Share Posted October 22, 2010 The way you're generating them will make it so that only the value of the last one will be passed through the $_POST array because they'll all have the same name= attribute. Why are you using a foreach() loop on an array, then not using any of the values from that array? Thats really a good point Pikachu2000, so how do generate or output the Textboxes field that will allows us to store the values. Now the reason why I am using the foreach loop is because with the foreach loop, the rows are associated. I mean, if there are 4 rows (or items) in the array, it must output 4 textboxes next to the values. Here is the refined code: foreach ($MyTest as $tst) { echo $tst['Prod'] . "<input name='testName' type='text' size='12' maxlength='5' />"; } So now with every $tst['Prod'] variable, the textbox will appear next to it, and we can enter values in the textboxes. This is what the requirement is. And I am able to accomplish this. Now the final step is to get the values from these textboxes appearing next to the variable. What is the better way of doing this. Whats your best suggestion. Thank you! Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125085 Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 You're on the right track with that. I'd actually set up the name the opposite, though and do it like below. That way all of those fields end up in one subarray within the $_POST array. foreach ($MyTest as $tst) { echo Prod['$tst'] . "<input name='testName' type='text' size='12' maxlength='5' />"; } Then you could loop through them with [code=php:0] foreach( $_POST['Prod'] as $key => $val ) { echo "Index key: $key holds the value: $val<br>"; } Let me know if this doesn't make sense to you. Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125089 Share on other sites More sharing options...
theITvideos Posted October 22, 2010 Author Share Posted October 22, 2010 You're on the right track with that. I'd actually set up the name the opposite, though and do it like below. That way all of those fields end up in one subarray within the $_POST array. foreach ($MyTest as $tst) { echo Prod['$tst'] . "<input name='testName' type='text' size='12' maxlength='5' />"; } Then you could loop through them with [code=php:0] foreach( $_POST['Prod'] as $key => $val ) { echo "Index key: $key holds the value: $val<br>"; } Let me know if this doesn't make sense to you. Thanks for your reply. Now the output I get is shown in the Screenshot I've attached. Now we just need to [u]get[/u] these textbox values (the textbox that we output in the foreach loop) Keeping in mind the good point you mentioned earlier that only the value of the last one will be passed through the $_POST array because they'll all have the [u]same[/u] name= attribute. So shall we assign textboxes a different name at runtime... So whats a good approach here to get the values and simply output saying this is what you entered for 1 textbox and for 2 textbox etc. Please see the screenshot. Thank you! [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125104 Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 I see I had a major typo in the post above, but also I didn't take into consideration that the values in the foreach() loop could end up being the same. So I'd make a change like this: foreach ($MyTest as $tst) { echo "<input name=\"prod[]\" type=\"text\" size=\"12\" maxlength=\"5\" />"; } Then each field will end up in the $_POST array as $_POST['prod'][0], $_POST['prod'][1], $_POST['prod'][2], etc. Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125108 Share on other sites More sharing options...
theITvideos Posted October 22, 2010 Author Share Posted October 22, 2010 I see I had a major typo in the post above, but also I didn't take into consideration that the values in the foreach() loop could end up being the same. So I'd make a change like this: foreach ($MyTest as $tst) { echo "<input name=\"prod[]\" type=\"text\" size=\"12\" maxlength=\"5\" />"; } Then each field will end up in the $_POST array as $_POST['prod'][0], $_POST['prod'][1], $_POST['prod'][2], etc. Thanks for you reply. I put prod[] for the textbox name: <input name=\"prod[]\" type=\"text\" size=\"12\" maxlength=\"5\" /> And trying to read the textbox values as: echo "Textbox 1 value entered is: " . $_POST['prod'][0]; I get no value for textbox 1. the result I'm getting is: Textbox 1 value entered is: I even tried to use $_GET instead of $_POST: echo "Textbox 1 value entered is: " . $_GET['prod'][0]; But still no output, is there something we need to do here. Please reply. Thank you. Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125122 Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 That looks fine to me. Throw this block of code at the top of the script and see what exactly shows up. echo '<pre>'; print_r($_POST); echo '</pre>'; Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125128 Share on other sites More sharing options...
theITvideos Posted October 22, 2010 Author Share Posted October 22, 2010 That looks fine to me. Throw this block of code at the top of the script and see what exactly shows up. echo '<pre>'; print_r($_POST); echo '</pre>'; Thanks for the reply. I tried this code: print_r($_POST) enclosed with <pre> but I get no output (value) for the textboxes. Lets try some more methods. Thanks! Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125143 Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 Paste in the actual code you're working with. I just set up a similar script locally, and it tested fine. Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1125198 Share on other sites More sharing options...
theITvideos Posted October 25, 2010 Author Share Posted October 25, 2010 Paste in the actual code you're working with. I just set up a similar script locally, and it tested fine. Thanks for the reply. I guess I get the picture now. I have multiple submit buttons on my page. And the output for member details that I'm creating is lying outside the submitting form thus it is not shown in that $_POST variable. I guess this is what happens when we got multiple submit buttons on a single page. (though i need those multiple submit for submitting different information). I think thats why it is working on your computer Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1126012 Share on other sites More sharing options...
Pikachu2000 Posted October 25, 2010 Share Posted October 25, 2010 That information might have been more useful at an earlier point in the thread . . . Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1126013 Share on other sites More sharing options...
theITvideos Posted October 25, 2010 Author Share Posted October 25, 2010 That information might have been more useful at an earlier point in the thread . . . Thanks bro you're right, my mistake. Link to comment https://forums.phpfreaks.com/topic/216531-how-to-get-values-from-textboxes-generated-through-a-loop/#findComment-1126014 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.