tomsey12 Posted April 8, 2006 Share Posted April 8, 2006 "albums" is a text file (without ".txt")<? $albums = file(albums); foreach ($albums as $album) {$album = str_replace("\n", "", $album); echo "<input name=\"$album\" type=\"checkbox\" value=\"1\" />"; }?>this form this script is part of points to the below script<? $albums = file(albums);foreach ($albums as $album){$album = trim($album);$wan = $_POST[$album];if ($wan == "1"){ $data = "$data\n$wan";} else {}}?>It doesn't seem to be able to get the value from the checkbox when its checked (1) using $_POST[$album];Can anyone help me?Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/6908-get-checkbox-values-from-form/ Share on other sites More sharing options...
mistergoomba Posted April 8, 2006 Share Posted April 8, 2006 try putting[code]import_request_variables('p');[/code]at the beginning of the script Quote Link to comment https://forums.phpfreaks.com/topic/6908-get-checkbox-values-from-form/#findComment-25102 Share on other sites More sharing options...
tomsey12 Posted April 8, 2006 Author Share Posted April 8, 2006 [!--quoteo(post=362881:date=Apr 8 2006, 11:53 PM:name=mistergoomba)--][div class=\'quotetop\']QUOTE(mistergoomba @ Apr 8 2006, 11:53 PM) [snapback]362881[/snapback][/div][div class=\'quotemain\'][!--quotec--]try putting[code]import_request_variables('p');[/code]at the beginning of the script[/quote]Didn't work :( any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/6908-get-checkbox-values-from-form/#findComment-25104 Share on other sites More sharing options...
kenrbnsn Posted April 9, 2006 Share Posted April 9, 2006 Remove the single quotes from this line:[code]<?php $wan = $_POST['$album']; ?>[/code]A variable name that is contained withing single quotes is not evaluated, so you end up looking for an entry in the $_POST array with the index of the string '$album', not the value of the variable $album. What you want is:[code]<?php $wan = $_POST[$album]; ?>[/code]BTW, instead of doing [code]<?php $album = str_replace("\n", "", $album); ?>[/code] to remove the newline character at the end of a line, just use the trim() function: [code]<?php $album = trim($album); ?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/6908-get-checkbox-values-from-form/#findComment-25150 Share on other sites More sharing options...
tomsey12 Posted April 9, 2006 Author Share Posted April 9, 2006 [!--quoteo(post=362933:date=Apr 9 2006, 06:39 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 9 2006, 06:39 AM) [snapback]362933[/snapback][/div][div class=\'quotemain\'][!--quotec--]Remove the single quotes from this line:[code]<?php $wan = $_POST['$album']; ?>[/code]A variable name that is contained withing single quotes is not evaluated, so you end up looking for an entry in the $_POST array with the index of the string '$album', not the value of the variable $album. What you want is:[code]<?php $wan = $_POST[$album]; ?>[/code]BTW, instead of doing [code]<?php $album = str_replace("\n", "", $album); ?>[/code] to remove the newline character at the end of a line, just use the trim() function: [code]<?php $album = trim($album); ?>[/code]Ken[/quote]Still not working :( $wan is still empty, but thats for the trim() help :) Quote Link to comment https://forums.phpfreaks.com/topic/6908-get-checkbox-values-from-form/#findComment-25178 Share on other sites More sharing options...
kenrbnsn Posted April 9, 2006 Share Posted April 9, 2006 Let me ask a few questions that I was too tired to ask last night. :-)[list][*]What is seen if you put the following line at the start of the processing script?[code]<?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>[/code]Do you see the values of your checkboxes?[*]The code for your form as posted only displays the checkboxes with no labels indicating what they are. Is this really the code you're using?[*]What data do you expect to be returned from the form?[*]Do you realize that only checked boxes return a value?[/list]Here's a different way of getting the data I think you're after, but I'm not sure, since your code is not clear as to what you're trying to do:[code]<?phpif (isset($_POST['submit'])) { if (isset($_POST['checkedalbums'])) { $data = implode("\n", $_POST['checkedalbums']); echo '<pre>' . $data . '</pre>'; } else echo 'No albums were selected';} else { $albums = file('albums'); $tmp = array(); $tmp[] = '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">'; foreach ($albums as $album) $tmp[] = trim($album) . ': <input type="checkbox" name="checkedalbums[]" value="' . trim($album) . '" /><br />'; $tmp[] = '<input type="submit" name="submit" value="Send Information" />'; $tmp[] = '</form>'; echo implode("\n",$tmp);}?>[/code]If you run the above code, does it give you what you want?Ken Quote Link to comment https://forums.phpfreaks.com/topic/6908-get-checkbox-values-from-form/#findComment-25192 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.