Jump to content

Archived

This topic is now archived and is closed to further replies.

tomsey12

Get checkbox values from form

Recommended Posts

"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!

Share this post


Link to post
Share on other sites
[!--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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[!--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 :)

Share this post


Link to post
Share on other sites
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]<?php
if (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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.