Solarpitch Posted July 30, 2008 Share Posted July 30, 2008 Hi, I'll try and explain this best I can. I have a result set being pulled from the database from a products table. Each product will have a textfield associated with it so the user can enter in the quantity they would like to order. So each textfield is named after the id of the product, that seems to make sense. What I dont understand is, how to get the values of the textfields that have data entered in them when the form is submitted. eg. prod1 - quantity: (0) prod2 - quantity: (15) prod3 - quantity: (2) prod4 - quantity: (0) prod5 - quantity: (1) So this result pulled 5 results from the database, each with its respective textfield. When the form is submitted how to I know that prod2, prod3 and prod5 have data in them? Quote Link to comment https://forums.phpfreaks.com/topic/117317-solved-dynamic-textfields/ Share on other sites More sharing options...
trq Posted July 30, 2008 Share Posted July 30, 2008 A simple example. foreach ($_POST as $k => $v) { if ($v != '' && $k != 'submit') { echo "$k - quantity: ($v)<br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/117317-solved-dynamic-textfields/#findComment-603440 Share on other sites More sharing options...
Solarpitch Posted July 30, 2008 Author Share Posted July 30, 2008 Thanks thorpe... I was certain I would need to use an array alright but wasn't sure in which context or how to handle it. I'll have a go at implementing it now Quote Link to comment https://forums.phpfreaks.com/topic/117317-solved-dynamic-textfields/#findComment-603441 Share on other sites More sharing options...
trq Posted July 30, 2008 Share Posted July 30, 2008 $_POST is an array. Quote Link to comment https://forums.phpfreaks.com/topic/117317-solved-dynamic-textfields/#findComment-603445 Share on other sites More sharing options...
Solarpitch Posted July 30, 2008 Author Share Posted July 30, 2008 I think there might be a problem with how I use that and the way I check for submission. It seems to print.. process - quantity: (execute) Do I need to link the textfield's with the $v array value some how? .. if that makes any sense! <?php //Check to see if form has been submited $process = $_POST['process']; if($process != "execute") { //Do noting... } else { foreach ($_POST as $k => $v) { if ($v != '' && $k != 'submit') { echo "$k - quantity: ($v)<br />"; } } } ?> ........... <input type="hidden" name="process" value="execute" /> <button type="submit">Submit</button> Quote Link to comment https://forums.phpfreaks.com/topic/117317-solved-dynamic-textfields/#findComment-603450 Share on other sites More sharing options...
trq Posted July 30, 2008 Share Posted July 30, 2008 Just name your submit button 'submit'. <?php if (isset($_POST['submit'])) { foreach ($_POST as $k => $v) { if ($v != '' && $k != 'submit') { echo "$k - quantity: ($v)<br />"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117317-solved-dynamic-textfields/#findComment-603454 Share on other sites More sharing options...
Solarpitch Posted July 30, 2008 Author Share Posted July 30, 2008 I've changed it to that alright but still cant get it to work. I understand what the code is trying to do alright. I added a print "Hello World" just to make sure its going in after submission and it is, but the echo quantity will not print. <?php if (isset($_POST['submit'])) { echo "Hello World"; foreach ($_POST as $k => $v) { if ($v != '' && $k != 'submit') { echo "$k - quantity: ($v)<br />"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117317-solved-dynamic-textfields/#findComment-603464 Share on other sites More sharing options...
trq Posted July 30, 2008 Share Posted July 30, 2008 This test.... <html> <head> <title>test</title> </head> <body> <?php if (isset($_POST['submit'])) { foreach ($_POST as $k => $v) { if ($v != '' && $k != 'submit') { echo "$k - quantity: ($v)<br />"; } } } ?> <form method="post"> <input type="text" name="foo"><br /> <input type="text" name="bar"><br /> <input type="text" name="bob"><br /> <input type="submit" name="submit"><br /> </form> </body> </html> Works fine on my server. Quote Link to comment https://forums.phpfreaks.com/topic/117317-solved-dynamic-textfields/#findComment-603477 Share on other sites More sharing options...
Solarpitch Posted July 30, 2008 Author Share Posted July 30, 2008 I've modified it slightly to this and it seems to work for me now... <?php if (isset($_POST['submit'])) { foreach ($_POST['val'] as $k => $v) { if ($v != '' && $k != 'submit') { echo "$k - quantity: ($v)<br />"; } } } ?> ...... <input type='text' name='val[".$row[0]."]' style='width:35px;' /> Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/117317-solved-dynamic-textfields/#findComment-603480 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.