Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

php and Javascript call to change radio button

7 views
Skip to first unread message

IchBin

unread,
Nov 15, 2006, 8:37:07 AM11/15/06
to
I am trying to set the state of a radio button. I do not see what I am
doing wrong. Sorry, I am new at this.. I need another set of eyes to
look at this snip of code. I am trying to set the radio button with this
link of code:

echo 'SCRIPT language=JavaScript
setCheckedValue("'.$_SESSION['abbr_letter'].'");</SCRIPT>'; //?

<snip of code>

<?php
session_start();
if(!isset($_SESSION['abbr_letter'])) {
$_SESSION['abbr_letter'] = 'A';
}
require_once 'includes/config.inc.php';
require_once 'includes/header.inc.php';

if (xdebug_is_enabled())
echo "enable";
else
xdebug_enable();

//
// Start a Form
?>
<HTML>
<HEAD>
<link href="quotesCss.css" rel="stylesheet" type="text/css">
<SCRIPT TYPE="text/javascript">
<!--
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(newValue) {
var len = document.author_abbrv.abbr_letter.length
for(var i = 0; i < len; i++)
{
document.author_abbrv.abbr_letter[i].checked = false;
if(document.author_abbrv.abbr_letter[i].value ==
newValue.toString())
{
document.author_abbrv.abbr_letter[i].checked = true;
}
}
}
-->
</SCRIPT>
</HEAD>
<b><FORM NAME="author_abbrv" method="POST" action="<?php echo
$_SERVER['PHP_SELF'];?>"></b>
<table>
<tr>
<td><input type="radio" name="abbr_letter" value="A"
onclick="submit()"> A </td>
<td><input type="radio" name="abbr_letter" value="B"
onclick="submit()"> B </td>
<td><input type="radio" name="abbr_letter" value="C"
onclick="submit()"> C </td>
<td><input type="radio" name="abbr_letter" value="D"
onclick="submit()"> D </td>
<td><input type="radio" name="abbr_letter" value="E"
onclick="submit()"> E </td>
<td><input type="radio" name="abbr_letter" value="F"
onclick="submit()"> F </td>
<td><input type="radio" name="abbr_letter" value="G"
onclick="submit()"> G </td>
<td><input type="radio" name="abbr_letter" value="H"
onclick="submit()"> H </td>
<td><input type="radio" name="abbr_letter" value="I"
onclick="submit()"> I </td>
<td><input type="radio" name="abbr_letter" value="J"
onclick="submit()"> J </td>
<td><input type="radio" name="abbr_letter" value="K"
onclick="submit()"> K </td>
<td><input type="radio" name="abbr_letter" value="L"
onclick="submit()"> L </td>
<td><input type="radio" name="abbr_letter" value="M"
onclick="submit()"> M </td>
</tr>
<tr>
<td><input type="radio" name="abbr_letter" value="N"
onclick="submit()"> N </td>
<td><input type="radio" name="abbr_letter" value="O"
onclick="submit()"> O </td>
<td><input type="radio" name="abbr_letter" value="P"
onclick="submit()"> P </td>
<td><input type="radio" name="abbr_letter" value="Q"
onclick="submit()"> Q </td>
<td><input type="radio" name="abbr_letter" value="R"
onclick="submit()"> R </td>
<td><input type="radio" name="abbr_letter" value="S"
onclick="submit()"> S </td>
<td><input type="radio" name="abbr_letter" value="T"
onclick="submit()"> T </td>
<td><input type="radio" name="abbr_letter" value="U"
onclick="submit()"> U </td>
<td><input type="radio" name="abbr_letter" value="V"
onclick="submit()"> V </td>
<td><input type="radio" name="abbr_letter" value="W"
onclick="submit()"> W </td>
<td><input type="radio" name="abbr_letter" value="X"
onclick="submit()"> X </td>
<td><input type="radio" name="abbr_letter" value="Y"
onclick="submit()"> Y </td>
<td><input type="radio" name="abbr_letter" value="Z"
onclick="submit()"> Z </td>
</tr>
</TABLE>
</FORM>
<b><FORM NAME="author" method="POST" action="<?php echo
$_SERVER['PHP_SELF'];?>"></b>
<SELECT NAME="author_pk" SIZE="20" COLS="10" onclick="submit()">

<?php
//
//Check for the first time pass
if( !isset($_POST['abbr_letter']) ) {
$_POST['abbr_letter'] = $_SESSION['abbr_letter'];
}
//
//Store the latest selected option to the Session var
$_SESSION['abbr_letter'] = $_POST['abbr_letter'];
echo 'SCRIPT language=JavaScript
setCheckedValue("'.$_SESSION['abbr_letter'].'");</SCRIPT>'; //?

--
Thanks in Advance... http://ichbin.9999mb.com
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

VK

unread,
Nov 15, 2006, 9:29:02 AM11/15/06
to

IchBin wrote:
> I am trying to set the state of a radio button. I do not see what I am
> doing wrong.

PHP is executed server-side; JavaScript is executed client-side. That
would be most helpful:
1) To know what exactly "wrong" in your case (radio button states do
not change; they change but on wrong buttons; browser crashes;
something else)
2) To see the page as it is at the moment of the JavaScript code
execution (thus served from the server to the user-agent). For this
generate HTML output out of your PHP (<http>...</http>) and post it
here.

IchBin

unread,
Nov 15, 2006, 11:19:24 AM11/15/06
to
VK wrote:
> IchBin wrote:
>> I am trying to set the state of a radio button. I do not see what I am
>> doing wrong.
>
> PHP is executed server-side; JavaScript is executed client-side. That
> would be most helpful:
> 1) To know what exactly "wrong" in your case (radio button states do
> not change; they change but on wrong buttons; browser crashes;
> something else)
> 2) To see the page as it is at the moment of the JavaScript code
> execution (thus served from the server to the user-agent). For this
> generate HTML output out of your PHP (<http>...</http>) and post it
> here.
>
Sorry all works, app wise, it is just the radio buttons do not stay
selected after selecting one once coming back from the server.. I do
post the button that is changed (Have echo'ed that value so I know I
trapped it, and trying to reset to that selected button.

The actual running code is here: http://ichbin.9999mb.com

Not sure how I can trap the the generated HTML code coming back from the
server..

Here is the entire script:

<?php
/**
*
http://localhost/quotes/quotesCss.php?XDEBUG_SESSION_START=tswebeditor
*
* @version $Id$
* @copyright 2006 weconsultants, http://weconsultants.phpnet.us
*/


session_start();
if(!isset($_SESSION['abbr_letter'])) {
$_SESSION['abbr_letter'] = 'A';

// echo 'setCheckedValue("abbr_letter",
"'.$_SESSION['abbr_letter'].'")';
}
require_once 'includes/config.inc.php';
require_once 'includes/header.inc.php';

//Store the lastest selected option to the Session var


$_SESSION['abbr_letter'] = $_POST['abbr_letter'];
echo 'SCRIPT language=JavaScript
setCheckedValue("'.$_SESSION['abbr_letter'].'");</SCRIPT>'; //?

//
//Setup for the database call for authors by first letter
$sqlcmd = $author_detail_select_by_Letter . $_POST['abbr_letter']
. $author_detail_orderBy;
if ($result = mysql_query($sqlcmd))
{
while ($row = mysql_fetch_object($result))
{
printf("<option value='%d'>%s %s, %s %s %s</option>n",
$row->id, $row->TITLE, $row->lastname,
$row->firstname, $row->middlename, $row->SUFFIX);
}
mysql_free_result($result);
}
else
{
die('Database Error: ' . $mysql->error);
exit;
}
?>
</SELECT>
<?php
//
// Add the list of top 20 authors
require_once 'top20.php';
?>
</FORM>

<BODY>
<?php
//
// This form uses the POST method, so process any post selections here
if( isset($_POST['author_pk']) )
{
//
// Log what author the user is looking for to the DB
include 'quoteLogging.php';
//
// Get all of the Quotes associated with this author
$sqlcmd = $author_detail_select . $_POST['author_pk'];
if ($author_detail = mysql_query($sqlcmd))
{
while( $row = mysql_fetch_object($author_detail) )
{
printf('<span style="font-weight: bold;">%s %s, %s %s
%s </span>' .
' %s <br> <div style="margin-left: 80px;">%s '.
'<span style="font-weight: bold;"></div><br>'.
'<div style="margin-left: 80px;"><span
style="font-weight: bold;">' .
" Link: </span><a href='%s'> Authors detail
information</a></span></div><br>",
$row->TITLE, $row->lastname,
$row->firstname, $row->middlename, $row->SUFFIX,
$row->BIRTHDEATH,$row->DESCRIPTION,$row->LINK);
}
}
else
{
die('Database Error: ' . mysql_error());
exit;
}
//
// Select the number of Quotes the selected author has.
$sqlcmd = $author_quote_number . $_POST['author_pk'];
if ($numberOfQuotes = mysql_query($sqlcmd))
{
$row = mysql_fetch_object($numberOfQuotes);
printf( '<div style="margin-left: 80px;"><span
style="font-weight: bold;">'.
'Number of Quotes: </span> %s<br>',
number_format($row->COUNT) .
' </div>');
}
else
{
die('Database Error: ' . mysql_error());
exit;
}
//
//
$sqlcmd = $author_quote_select . $_POST['author_pk'] . " ORDER
BY quote;";
if ($resultSet = mysql_query($sqlcmd))
{
while( $row = mysql_fetch_object($resultSet))
{
printf('<span style="font-weight:
bold;background-color: white;">Quote:</span> %s<br>',
$row->QUOTE);
if ($row->QUOTELITREF != '' )
{
printf( '<span style="font-weight: bold;
background-color: white;">'.
'Quote Reference:</span> %s<br><br>',
$row->QUOTELITREF);
}
else
{
print('<br>');
}
}
}
else
{
mysql_free_result($result);
die('Database Error==> ' . mysql_error());
exit;
}
}
//
// Add a footer for all to know
require_once 'includes/footer.inc.php';
//
// Close DB
include 'includes/closedb.inc.php';
?>

VK

unread,
Nov 15, 2006, 12:47:12 PM11/15/06
to

IchBin wrote:
> The actual running code is here: http://ichbin.9999mb.com
> Not sure how I can trap the the generated HTML code coming back from the
> server..

The URL (as seen in browser) *is* the HTML code coming back from the
server :-) This is what I meant, thank you.

In your case the page starts from <center> tag and after a bunch of
other code (inscluding script) there is finally <html> block wrapped
into <div>. I'm very far of being a pedantical person here, but some
*very minimum* respect to basic programming rules has to be served in
any solution IMHO. Try do not get upset: but your pages are totally
dependant on the particular UA's error correction mechanics - this way
some generic advises are futile.
I just say that if your bring you pages into a descent form the problem
very well may disappear by itself. You may just try.

Tom Cole

unread,
Nov 15, 2006, 12:54:20 PM11/15/06
to

It appears that your PHP is generating the option list part of your
page, but not the radio buttons. When you reload the page, the radio
buttons are always going to load in their default state (unchecked as
NON of them say CHECKED).

Either have your PHP script generate the radio button output (and
output CHECKED for the radio button that corresponds to the abbr_letter
value) or generate some script that onload checks the abbr_letter value
and CHECKS the corresponding radio button.

That should do it.

IchBin

unread,
Nov 15, 2006, 1:24:52 PM11/15/06
to
Sorry I am a Java programmer. I am new to Javascript\css\php. So you
would recommend that I rewrite this script?

VK

unread,
Nov 16, 2006, 6:37:38 AM11/16/06
to

IchBin wrote:
> Sorry I am a Java programmer. I am new to Javascript\css\php. So you
> would recommend that I rewrite this script?

I would recommend to make your server-side PHP to output a conventional
HTML page first. Skipping on such luxiry as doctype :-) the ouput would
be something like:

[[ Content-Type: text/html; charset=[YourCharset] ]]
<html>
<head>
<title>Your Title</head>
[[ Style data ]]
<script type="text/javascript">
[[ served generated script content ]]
</script>
</head>
<body>
[[body content ]]
<form>
[[ form content ]]
</form>
</body>
</html>

I don't give you 100% guarantee that your client-side script will start
to work as expected (only 90%) - but at least it will be possible to
make some reasonable analysis of what and why is not working.

Tom Cole

unread,
Nov 16, 2006, 7:52:13 AM11/16/06
to

IchBin wrote:
> VK wrote:
> > IchBin wrote:
> >> The actual running code is here: http://ichbin.9999mb.com
> >> Not sure how I can trap the the generated HTML code coming back from the
> >> server..
> >
> > The URL (as seen in browser) *is* the HTML code coming back from the
> > server :-) This is what I meant, thank you.
> >
> > In your case the page starts from <center> tag and after a bunch of
> > other code (inscluding script) there is finally <html> block wrapped
> > into <div>. I'm very far of being a pedantical person here, but some
> > *very minimum* respect to basic programming rules has to be served in
> > any solution IMHO. Try do not get upset: but your pages are totally
> > dependant on the particular UA's error correction mechanics - this way
> > some generic advises are futile.
> > I just say that if your bring you pages into a descent form the problem
> > very well may disappear by itself. You may just try.
> >
> Sorry I am a Java programmer. I am new to Javascript\css\php. So you
> would recommend that I rewrite this script?

I too develop in Java so if it were me I'd have a nice JSP page here :)

But to answer your question I think you will find it easier if you have
your button group code also generated by your PHP script. Use the same
logic that you use to determine which subgroup to load to determine
which radio button to write CHECKED next to...

Javascript can handle it, but it's not very graceful. AFAIK the only
way to access an individual radio button within a group is through an
array. Therefore to find it programmatically you'd have to have your
radio buttons return values like 0, 1, 2, 3 instead of A, B, C, D and
this would force you to do some converting on the server anyway.

Just my $.02

IchBin

unread,
Nov 16, 2006, 2:59:34 PM11/16/06
to

Tom, your $.02 is very much appreciated.. AS to writing it in JSP. Well,
I just wanted to try something different and expand my brain. They are
different animals and wanted to get handle around html\javascript\css
and php combined. It's just a little blurry for me now. I am hearing
what all the people are saying, that is, to have php process this on the
server.

IchBin

unread,
Nov 16, 2006, 3:08:02 PM11/16/06
to
Thank you for you suggestions. I will try to follow your advice.

ewosch

unread,
Dec 7, 2006, 3:37:16 AM12/7/06
to
Hi,
is this problem resolved in the mean time ?
Independant of missing Body-tag, I got some problems with the vector of
radio-type input tags: it did not work in all our applications. In the
actual application, I had to collect these elements by myself with a
code like
fy=document.getElementsByTagName("input");
and
if ( fy[j].checked && fy[j].type=="radio" &&
fy[j].name==name) {ii++;break;}

is this helping ? Whats your solution ?
Wolfgang

IchBin schrieb:

0 new messages