rschaft Posted April 18, 2012 Share Posted April 18, 2012 Hi, I'm trying to pass values from an array into a form. As a newbie, I've came up with the code underneath, but I want to pinpoint all the first values toward 1 selection list in the form. Any suggestions how to accomplish this? Thanks in advance. Ruud <html> <head> <title>Test formulier</title> </head> <body> <h2>Test formulier</h2> <form action="test.php" method="post"> <p> <?php $prijzen = array( array("01", "125","150", "A" => true,"B" => true), array("02", "125","150", "A" => true,"B" => true), array("03", "125","150", "A" => true,"B" => false), array("04", "125","150", "A" => true,"B" => true) ); ?> </P> <select name="prijzen[]" id="prijzen" multiple="multiple" size="7" class="none"> <?php while(list($key, $val) = each($prijzen)){ while(list($key2, $val2) = each($val)){ echo '<option value="'.$key2.'">'.$val2.'</option>'.PHP_EOL; } } ?> </select> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/261191-pass-values-array-into-form/ Share on other sites More sharing options...
plznty Posted April 18, 2012 Share Posted April 18, 2012 I really don't understand what you are trying to do Quote Link to comment https://forums.phpfreaks.com/topic/261191-pass-values-array-into-form/#findComment-1338553 Share on other sites More sharing options...
rschaft Posted April 18, 2012 Author Share Posted April 18, 2012 I'm trying to use the array values to populate a selection list in a form. The intentio is to use all first values in the same list, the second values in aonthter list and so on. Quote Link to comment https://forums.phpfreaks.com/topic/261191-pass-values-array-into-form/#findComment-1338555 Share on other sites More sharing options...
samshel Posted April 18, 2012 Share Posted April 18, 2012 array("01", "125","150", "A" => true,"B" => true), This is an associative array. It actually is the same as: array(0=>"01",1=> "125",2=>"150", "A" => true,"B" => true), The keys are numeric for first 3 elements and A and B for 4th and 5th element respectively. do you really want to do this? You code will consider the keys 1,2,3,A,B as drop down values. Also what is the result you are getting now? and what is the result you are expecting? Quote Link to comment https://forums.phpfreaks.com/topic/261191-pass-values-array-into-form/#findComment-1338569 Share on other sites More sharing options...
rschaft Posted April 18, 2012 Author Share Posted April 18, 2012 Oke, that's not what I need. It should be 01, 125, 150, A, B. Is this possible with the lay-out of the array? Quote Link to comment https://forums.phpfreaks.com/topic/261191-pass-values-array-into-form/#findComment-1338576 Share on other sites More sharing options...
samshel Posted April 18, 2012 Share Posted April 18, 2012 An Array has 2 parts keys and values. I suppose the one you described above are values. What is the purpose of this form ? Do you want to update these in some database? Quote Link to comment https://forums.phpfreaks.com/topic/261191-pass-values-array-into-form/#findComment-1338577 Share on other sites More sharing options...
algidDes702 Posted April 18, 2012 Share Posted April 18, 2012 With what you just put, you are asking to print a mix of your array's keys and its values. What ever list you want to print into your should either be keys or it should be values of an array. Mixing them is not good or make printing them difficult. What do the "A" and "B" need to be in your selection element? Does the 'true' value need to be passed on once the form is submitted and A or B is selected? if you are trying to print this list: 01, 125, 150, A, B into your selection then your array should just look similar to this: <?php $list1 = array("01","125","150","A","B"); echo "<select name='firstSelector'>".PHP_EOL; foreach ($list1 as $key=>$value) { echo '<option value="'.$key."'>'.$value.'</option>'.PHP_EOL; } echo "</select>".PHP_EOL; But really if you need specific values passed with each option when the form is submitted then you can make it an associative array where the array keys get printed into the options value like so: <?php $list1 = array("prijzen0"=>"01","prijzen1"=>"125","prijzen2"=>"150","prijzenTrue1"=>"A","prijzenTrue2"=>"B"); echo "<select name='firstSelector'>".PHP_EOL; foreach ($list1 as $key=>$value) { echo '<option value="'.$key.'">'.$value.'</option>'.PHP_EOL; } echo "</select>".PHP_EOL; ?> The above would print this: <select name='firstSelector'> <option value="prijzen0">01</option> <option value="prijzen1">125</option> <option value="prijzen2">150</option> <option value="prijzenTrue1">A</option> <option value="prijzenTrue2">B</option> </select> let me know if that helps or not thanks for sharing Quote Link to comment https://forums.phpfreaks.com/topic/261191-pass-values-array-into-form/#findComment-1338582 Share on other sites More sharing options...
rschaft Posted April 18, 2012 Author Share Posted April 18, 2012 Hi, Thanks for your comment. I need <option value="prijzen">01</option> <option value="prijzen">02</option> <option value="prijzen">03</option> Quote Link to comment https://forums.phpfreaks.com/topic/261191-pass-values-array-into-form/#findComment-1338596 Share on other sites More sharing options...
samshel Posted April 18, 2012 Share Posted April 18, 2012 Thats not going to work. If you keep the values same for all options, you will not be able to tell what the user selected from drop down and use it for any decision making in your program. Quote Link to comment https://forums.phpfreaks.com/topic/261191-pass-values-array-into-form/#findComment-1338597 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.