Jump to content


Photo

Get checkbox values from form


  • Please log in to reply
5 replies to this topic

#1 tomsey12

tomsey12
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 08 April 2006 - 10:44 PM

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

#2 mistergoomba

mistergoomba
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 08 April 2006 - 10:53 PM

try putting

import_request_variables('p');

at the beginning of the script

#3 tomsey12

tomsey12
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 08 April 2006 - 10:57 PM

[!--quoteo(post=362881:date=Apr 8 2006, 11:53 PM:name=mistergoomba)--][div class=\'quotetop\']QUOTE(mistergoomba @ Apr 8 2006, 11:53 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
try putting

import_request_variables('p');

at the beginning of the script
[/quote]


Didn't work :( any other ideas?

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 April 2006 - 05:39 AM

Remove the single quotes from this line:
<?php $wan = $_POST['$album']; ?>
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:
<?php $wan = $_POST[$album]; ?>

BTW, instead of doing
<?php $album = str_replace("\n", "", $album); ?>
to remove the newline character at the end of a line, just use the trim() function:
<?php $album = trim($album); ?>

Ken

#5 tomsey12

tomsey12
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 09 April 2006 - 11:44 AM

[!--quoteo(post=362933:date=Apr 9 2006, 06:39 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 9 2006, 06:39 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Remove the single quotes from this line:
<?php $wan = $_POST['$album']; ?>
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:
<?php $wan = $_POST[$album]; ?>

BTW, instead of doing
<?php $album = str_replace("\n", "", $album); ?>
to remove the newline character at the end of a line, just use the trim() function:
<?php $album = trim($album); ?>

Ken
[/quote]

Still not working :( $wan is still empty, but thats for the trim() help :)

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 April 2006 - 01:27 PM

Let me ask a few questions that I was too tired to ask last night. :-)
  • What is seen if you put the following line at the start of the processing script?
    <?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>
    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?
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:
<?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);
}
?>
If you run the above code, does it give you what you want?

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users