retaining value of radio buttons

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
prachu
Forum Newbie
Posts: 6
Joined: Thu Sep 20, 2007 3:49 pm

retaining value of radio buttons

Post by prachu »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello

I am having a tough time with radio buttons. All I want to accomplish is this :

I have queried and got a set of place names from my mysql db and i have listed it in php form with the use of radio buttons. When the user clicks on a radio button, i want to capture the onclick event and i want to send the info regarding which button was clicked  to the server. And also display the form again with the selected radio button. I do not want to use submit button here because this happens in the middle of a long data entry form and I have a submit button at the end of that form.

Code: Select all

while($placeRecord= mysql_fetch_array($placeSet)){
    echo "<input type='radio' name='placeChoice' value='$placeRecord[locationID]'   onClick='reloadWPlace(this.form);'";
}
Here is the javascript fn that i am calling

Code: Select all

function reloadWPlace(form){
var placeVal=0;
var i;
for(i = 0; i < document.dataEntry.placeChoice.length; i++ )
{
if( document.dataEntry.placeChoice[i].checked == true )
placeVal = document.dataEntry.placeChoice[i].value;
}
self.location='dataEntry.php?placeChoice=' + placeVal; // append selectes place to address bar
}
Then to display the selected radio button again I grab the selection from $_REQUEST[placeChoice] and assign it to $selPlace . The whole chunk is here. This includes the first code snippet in this post

Code: Select all

echo "<br/>List of Places<br/><br/>";
while($placeRecord= mysql_fetch_array($placeSet)){
echo "<input type='radio' name='placeChoice' value='$placeRecord [locationID]' onClick='reloadWPlace(this.form);'";
if ($placeRecord['locationID'] == $selPlace) {
echo "checked";
}
echo ">";
echo "<a href='placeDetails.php?id=$placeRecord[locationID]' onClick=\"return popup(this, 'Details')\">$placeRecord[place]</a><br/>";
}
But for some reason, nothing happens at all. When i select the radio button, the js function is not called, nothing is appended to the address bar and page is not reloaded with the selected radio button. What could be the problem. Any help would be greatly appreciated!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by prachu on Thu Sep 20, 2007 4:23 pm, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You don't close your input tag in your PHP code snippets - both of them.
There are 10 types of people in this world, those who understand binary and those who don't
prachu
Forum Newbie
Posts: 6
Joined: Thu Sep 20, 2007 3:49 pm

Post by prachu »

I actually do. echo ">" outside the if loop
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Oh, yes you do :) But... I took a deeper look in you JS code ...
First of all - why do you want to pass the form on click, instead of the radio button itself? If you do it will simplify your work :)

Code: Select all

while($placeRecord= mysql_fetch_array($placeSet)){
    echo "<input type='radio' name='placeChoice' value='$placeRecord[locationID]'   onClick='reloadWPlace(this);'";
}

Code: Select all

function reloadWPlace(elem){
    self.location='dataEntry.php?placeChoice=' + elem.value; // append selectes place to address bar
} 
There are 10 types of people in this world, those who understand binary and those who don't
prachu
Forum Newbie
Posts: 6
Joined: Thu Sep 20, 2007 3:49 pm

Post by prachu »

Thanks. thats a good suggestion. I am new to JS and PHP.

But why is it that the value is not getting appended to the address bar?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

I've tested it and it works ... Ctrl-F5 ;)
There are 10 types of people in this world, those who understand binary and those who don't
prachu
Forum Newbie
Posts: 6
Joined: Thu Sep 20, 2007 3:49 pm

Post by prachu »

I am sorry I dont get it? CTRL + F5? how did you test it?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

http://ipclassify.relef.net/1.php
http://ipclassify.relef.net/1.phps

Ctrl-F5 is for reloading your page without any caching.
There are 10 types of people in this world, those who understand binary and those who don't
prachu
Forum Newbie
Posts: 6
Joined: Thu Sep 20, 2007 3:49 pm

Post by prachu »

Thanks for your time and response. Even I wrote a small script and it does work. But for some reason it is not working in my project :( .. Could there be any other factors that influence this behavior? Thanks for your help
prachu
Forum Newbie
Posts: 6
Joined: Thu Sep 20, 2007 3:49 pm

Post by prachu »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Alright, I found where the error is.. In the JS function:
[syntax="javascript"]
function reloadWPlace(radio){
	var countryVal=form.country.options[form.country.options.selectedIndex].value;
	var stateVal=form.state.options[form.state.options.selectedIndex].value;
	var countyVal=form.county.options[form.county.options.selectedIndex].value;
	var placeVal=radio.value;

        // everything is fine upto this point. The value of placeVal is also correct. But the following line is not getting executed. &placeChoice=placeVal is not getting appended to self.location!

	self.location='dataEntry.php?country=' + countryVal + '&state=' +stateVal +'&county=' +countyVal+ '&placeChoice=' + placeVal ;
}
Any thoughts?


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

:)
First of all - why do you want to pass the form on click, instead of the radio button itself? If you do it will simplify your work
It was in case you needed only the radio buton value. But in your case you'll need the form itself. You can still use my solution because you have back reference to the form element:

Code: Select all

function reloadWPlace(radio){
   var form = radio.form;
   var countryVal=form.country.options[form.country.options.selectedIndex].value;
   var stateVal=form.state.options[form.state.options.selectedIndex].value;
   var countyVal=form.county.options[form.county.options.selectedIndex].value;
   var placeVal=radio.value;

        // everything is fine upto this point. The value of placeVal is also correct. But the following line is not getting executed. &placeChoice=placeVal is not getting appended to self.location!

   self.location='dataEntry.php?country=' + countryVal + '&state=' +stateVal +'&county=' +countyVal+ '&placeChoice=' + placeVal ;
} 
PS: I'm not sure it is the problem, because I need the rest of your HTML code.
PPS: I think this thread is for "Client Side" :)
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply