Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
prepopulate radio button
Old 09-28-2006, 07:02 PM prepopulate radio button
Junior Talker

Posts: 3
Name: Don Diego
Trades: 0
This script prepopulates select box, state, but not radio button, tobacco.

Code:
<script language="javascript" type="text/javascript">

function prepop()
{
document.long_form.state.value="<?php echo $_SESSION['state']; ?>";
document.long_form.tobacco.value="<?php echo $_SESSION['tobacco']; ?>";
}
window.onload=prepop;

</script>
The value for tobacco, yes or no, shows on the page with view source, so it got to the page, but I can't find how to address a radio button so the value is preselected., and I'm out of ideas.

Any suggestions?
tableman is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-28-2006, 09:44 PM Re: prepopulate radio button
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
Why use JS for this? Why not just use PHP to add the "checked" attribute?
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 09-28-2006, 10:01 PM Re: prepopulate radio button
Average Talker

Posts: 18
Name: goo
Trades: 0
if you know javascript, why NOT use it?
why learn php for one thing?
__________________

Please login or register to view this content. Registration is FREE
boredportal is offline
Reply With Quote
View Public Profile
 
Old 09-28-2006, 10:23 PM Re: prepopulate radio button
Junior Talker

Posts: 3
Name: Don Diego
Trades: 0
PHP could be used, funkdaddu, but the same question arises for me in PHP, as boredportal indicates.

It does not matter which I use. I just need to figure it out.
tableman is offline
Reply With Quote
View Public Profile
 
Old 09-29-2006, 04:28 AM Re: prepopulate radio button
Junior Talker

Posts: 3
Name: Don Diego
Trades: 0
So I figured it out, with a little help elsewhere.

Here is the javascript solution for prepopulating a radio button that receives its value from a php session (substitute input code accordingly for any other source):

Code:
var tob; 
var tabac="<?php echo $_SESSION['tobacco']; ?>"; 
{
if (tabac=="yes") 
{tob="0"} 
else if (tabac=="no")
{tob="1"} 
document.long_form.tobacco[tob].checked = true;
This line is then not needed for the radio button:

Code:
document.long_form.tobacco.value="<?php echo $_SESSION['tobacco']; ?>";
It would be nice to know how to do this in php also, but this javascript works.
tableman is offline
Reply With Quote
View Public Profile
 
Old 10-01-2006, 10:46 PM Re: prepopulate radio button
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
You used PHP in your example so I figured you were fluent in it... but this is how you would do it with pure PHP:

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<title>Untitled Page</title>
	</head>

	<body bgcolor="#ffffff">
		<form id="FormName" action="" method="get" name="FormName">
			Tobacco: <input type="radio" name="Tobacco" value="Yes" <?php if ($_SESSION['tobacco'] == 'yes') { echo 'checked'; } ?>>Yes  <input type="radio" name="Tobacco" value="No" <?php if ($_SESSION['tobacco'] == 'no') { echo 'checked'; } ?>>No
		</form>
		<p></p>
	</body>

</html>
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 10-19-2006, 03:42 AM Re: prepopulate radio button
seomumbai's Avatar
Skilled Talker

Posts: 98
Trades: 0
You can change the checked status of the radio button to true.
Document.long_form.radiobutton.chaecked = true.
seomumbai is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to prepopulate radio button
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31394 seconds with 12 queries