karldesign Posted April 18, 2008 Share Posted April 18, 2008 I have a for loop that populates a series of select boxes, giving them a name of form_$i... How do i then check the post value within this for loop to see if it has been checked: ie: if($_POST['form_23'] == $i){ // do this } Link to comment https://forums.phpfreaks.com/topic/101706-dynamic-form-variables/ Share on other sites More sharing options...
haku Posted April 18, 2008 Share Posted April 18, 2008 How about some code? Link to comment https://forums.phpfreaks.com/topic/101706-dynamic-form-variables/#findComment-520349 Share on other sites More sharing options...
PFMaBiSmAd Posted April 18, 2008 Share Posted April 18, 2008 You should be using an array instead of sequentially named variables/indexes. With an array your code can use a simple foreach() loop to iterate over all the values. Link to comment https://forums.phpfreaks.com/topic/101706-dynamic-form-variables/#findComment-520357 Share on other sites More sharing options...
haku Posted April 18, 2008 Share Posted April 18, 2008 An array isn't possible if the number of inputs is set dynamically. I've been in the same position as the OP before. Link to comment https://forums.phpfreaks.com/topic/101706-dynamic-form-variables/#findComment-520362 Share on other sites More sharing options...
PFMaBiSmAd Posted April 18, 2008 Share Posted April 18, 2008 An array is possible when a form dynamically adds fields (or if php dynamically generates and outputs the form). Instead of creating name="form_1", name="form_2", you just change it to name="form[]" or if you want to give them specific indexes name="form[1]", name="form[2]" It makes no difference what name you generate but it makes a big difference in the form processing code on how easy it is to iterate over one method and not the other. Sequentially named variables/indexes require you to carry along how many of them there are. An array does not need that extra information. Just because you could not get it to work, does not mean it does not work. Link to comment https://forums.phpfreaks.com/topic/101706-dynamic-form-variables/#findComment-520368 Share on other sites More sharing options...
haku Posted April 18, 2008 Share Posted April 18, 2008 And just because your way works doesn't mean its the only way. for($i = 0; isset($_POST['form_' . $i]); $i++) { // execute code here } Link to comment https://forums.phpfreaks.com/topic/101706-dynamic-form-variables/#findComment-520377 Share on other sites More sharing options...
PFMaBiSmAd Posted April 18, 2008 Share Posted April 18, 2008 That type of code only works if the values are continuous. For a check box or radio button, where nothing will exist if they are not selected, the loop will stop on the first unchecked input and will never see the following fields. Arrays solve all problems like that. Link to comment https://forums.phpfreaks.com/topic/101706-dynamic-form-variables/#findComment-520383 Share on other sites More sharing options...
haku Posted April 18, 2008 Share Posted April 18, 2008 And if you are looking for non-continuous stuff, there are other ways to do it. I've done it more than one way. My point is your saying "You should be using an array instead of sequentially named variables/indexes" is absolute, when there are many ways to do something. Which is why I asked to see his code. You were taking a cocky attitude, when there may not have been issues with the way he was doing it. Until we see his code, there is no way of knowing. Link to comment https://forums.phpfreaks.com/topic/101706-dynamic-form-variables/#findComment-520392 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.