PDA

View Full Version : radio button is selecting but not showing selected


songboy
07-28-2011, 03:24 PM
I really believe this could be my last 'mysterious' problem !! I have a do/while loop which iterates a list of song titles and other information about some songs. Part of the iteration is a set of radio buttons. A user will click on the button to choose a song to play. Now it all works really well in that, when you click the radio button the chosen song plays via the player. However, the actual radio button does not not stay clicked once the song begins to play (it reverts to the empty/not chosen state). I've ignored this because I was pleased by the fact that everything worked ! I now feel I have to get this issue sorted. This is the problem: how do I keep the radio button closed. Here are the essential code snippets.
<?php @$song_list = $row['song_title'];?> <input type ='radio' name = 'get_songs' value ="<?php echo $song_list;?>">&nbsp;
$row is the data array. The value attributed to each radio button is the song title. The thing I've noticed is that the code makes no reference to 'selection'. Even so, when you click a button it blackens briefly and then returns to not chosen. A key influence is that the form is sending to itself. I'm thinking that the chosen button goes out because the minute you click it, the page activates again and though the song name gets posted and the song plays, the button returns to the 'press me' state. How do I get the button to stay clicked ?
I solved a similar problem with Javascript. This function is relevant and works:

function GetSelectedItem() {
chosen = ""
len = document.songslist.get_songs.length
for (i = 0; i <len; i++) {
if (document.songslist.get_songs[i].checked) {
chosen = document.songslist.get_songs[i].value
}
}
}

The problem here is that the radio button clicks and stays on but now the song doesn't play ! I've used an event handler to kick in the function and I suspect this is part of the problem:

<form name = "songslist" action = "contributorpage.php" method="POST" onChange="GetSelectedItem();document.forms[0].submit()">
Can you get a button to stay clicked if the page is sent to itself via php ?
If it has to be javascript, how do I use my function so that it doesn't stop the song playing ?
An answer would generate a huge sense of relief !
Songboy

FethrdWlf
07-28-2011, 05:08 PM
Could you provide a link to the page? Or if it's local, the full page code?

As for a quick fix (because you think the page is refreshing upon choosing a song), then just add this code above EVERYTHING on your website (So if your HTML starts with "<html><head>....", then put it above that):
<?php session_start(); ?>
Then, set a session variable (http://www.w3schools.com/php/php_sessions.asp) with the value being the song title whenever a radio button is clicked. Last, where you echo your radio button, check to see if a variable is set. If it is, select the radio box and unset the session:
<?php
$songname = $row['song_title'];
if(isset($_SESSION[$songname])){
add the selected bit o' code here
}
unset($_SESSION[$songname]);
?>

I believe that would work (but I'm not sure. its worth a shot). :cool: Hope I helped!

songboy
07-31-2011, 06:54 PM
Hi Fethrd
Having mulled over your idea, I'm not sure I get it.
I think you are saying let's counteract the refresh.
I've already got the session start as the whole page relies heavily on the transfer of variables. I hadn't created a session for the $row array and I think you are using this to hold onto the value if a button is clicked. You lose me when you say "add the selected bit of code here". An update on what you are referring to would help.
Having thought about the problem, I'm actually at a loss as to what is happening. Basically, I'm not really sure what is going on so I'm not sure where to find an answer. I'm actually beginning to wonder if there is a problem ie if the button status goes to unchecked when the data is posted, does it really matter ? The only awkwardness is that a listener won't at once be able to see which song is playing because the relevant button is not checked. I'm wondering if this must necessarily happen, given my solution.

FethrdWlf
08-01-2011, 11:03 PM
Sorry that I hadn't replied, I don't know when you last posted (I was campin').

I didn't mean to stop the refresh. Go ahead and let it happen :). What I meant was as follows:

When someone clicks a radio button, have it set a session variable named "chosen_song" with the value being the title of the song selected. Then, after the page refreshes (if it actually isn't, then also have the radio button refresh the page upon clicking), while the $row variable equals the results of your query, to display the radio button, enter this code in...
@$song_list = $row['song_title'];
if(isset($_SESSION['chosen_song']) && $_SESSION['chosen_song'] == $song_list){
$selected = "selected = 'selected'";
}else{
$selected = "";
}
echo "<input type ='radio' name = 'get_songs' value ='".$song_list."' ".$selected.">";
I think it should come out looking kinda like this:
...HTML / Where you want your radio buttons to be...
<?php
$query = "Query for your song data";
$results = mysqli_query([mysql connection variable here],$query);
while($row = mysqli_fetch_assoc($results)){
@$song_list = $row['song_title'];
if(isset($_SESSION['chosen_song']) && $_SESSION['chosen_song'] == $song_list){
$selected = "selected = 'selected'";
}else{
$selected = "";
}
echo "<input type ='radio' name = 'get_songs' value ='".$song_list."' ".$selected.">";
?>
...more HTML...

Again, sorry for the delay in response :P, and I hope I helped this time! I was never the best at explaining things.

songboy
08-09-2011, 03:54 PM
Thanks to Wlf for your ideas. I've tried everything, every which way but the problem remains. I think the real culprit here is the onClick= document.forms[0].submit();. If I remove it, the radio status is retained (obviously nothing happens because now there's no submit). When I reinstate the event handler everything works except the radio button loses status and the pages flash significantly everytime a new song choice is made.
I decided to take a different route. I'm not sure if radio buttons are the right tool for my needs. I decided to go with the $GET method. To my do/while loop I've added the following: (it's my title array)

@$song_list = $row['song_title'];

Now I did this:
<a href= echo "\"?song_list=" . $song_list . "\"";>
<img src="images/choice.png"></a>

So, the titles are now stuck to the link as extra data.
(The choice png is a small button I made in 'fireworks'.)
Each button now represents a title.
Now I've got this:
@$pict_song = $_GET['song_list'];
if($pict_song)
{
$new_song = $pict_song;
}
if(@$new_song)
{
$chosen_song = $new_song.".mp3";
The flash player now picks up the $chosen_song variable and begins to play the song.
The upside to this is that it all works!! Also, the flashing from my index and contributor pages is now reduced.
Ironically, I have <a> styles which refer to link state colours. These are not actioned when I use my new link button above - any ideas how to sort this ?
Anyway, things are looking up. Thanks again to wlf - I'm annoyed that I couldn't make your system work (it's just me I think).
All the best -
Songboy

FethrdWlf
08-09-2011, 11:55 PM
Hmmmm... I didn't know if my route would actually work, I just thought it would. But, you might try adding the "return false;" code to the end of your onclick event. So after "document.form(0).submit" or whatever it is, add the "return false;" code. It probably won't work, but it's worth a shot...

Sorry for the trouble man! I've been having problems myself. Try the guys at http://www.phpbuilder.com/board/. They might be able to help you more than me :)