Jump to content


Photo

accessing the vaule of multiple radio buttons


  • Please log in to reply
3 replies to this topic

#1 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 25 August 2006 - 05:48 AM

Hello, sorry for the dumb question, but my js is working like I was expecting it to. I know nothing about javascript, but I need to use it in my php script since there is no easy way to do what I need without it.

What I need to do is print out a list of radio buttons, then have a few buttons at the bottom of the form with onclick's that call the apropriate function with this.form as a parameter. I then need to find out which radio button was selected by accessing the value in the function. However, it comes back undefined.

<form action="select_user.php" method="POST"> 
<?php #print selection list of users. note: select_user.php is this file
$arNames = retriveAllUserNames(); # get a list of users from DB
foreach ( $arNames as $strName) {  # assign elemnt in array to $strName until none left
  print "<input type=\"radio\" name=\"selectedUser\" value=\"" .$strName. "\" />" .$strName;
  print "<br>\n";
}?>
<br />
<input type="button" value="Edit" onclick="javascript:callEditUser(this.form)" />
<input type="button" value="Delete" onclick="javascript:callDeleteUser(this.form)" />
</form>

My Java script section looks like this.
function callEditUser( selectUserForm ) {
  var strName = selectUserForm.selectedUser.value;
  var url = "edit_user.php?name="+strName
  document.write(url);  //debug
 // window.open(url, "_self");
}

function callDeleteUser( selectUserForm ) {
  var strName = selectUserForm.selectedUser.value;
  if (confirm(strConfirmMsg) ) {
    var url = "delete_user.php?name="+strName
    document.write(url);  //debug
   // window.open(url, "_self");
  }
}

This is what prints to the screen when I hit a button:

edit_user.php?name=undefined

This is how actually get's printed (from view->source in browser)
<form action="/select_user.php" method="POST">

<input type="radio" name="selectedUser" value="admin" />admin<br>
<input type="radio" name="selectedUser" value="drkstr" />drkstr<br>
<br />
<input type="button" value="Edit" onclick="javascript:callEditUser(this.form)" />
<input type="button" value="Delete" onclick="javascript:callDeleteUser(this.form)" />
</form>


#2 radalin

radalin
  • Members
  • PipPipPip
  • Advanced Member
  • 179 posts

Posted 25 August 2006 - 11:16 AM

replace this:
<input type="radio" name="selectedUser" value="admin" />admin<br>
with
<input type="radio" name="selectedUser" id="selectedUser" value="admin" />admin<br>
and do the changement for every radio button.

your form tag should be
<form id="selectUserForm" ....>

then in your js change
var strName = selectUserForm.selectedUser.value;
with
var strName = document.getElementById('selectUserForm').selectedUser.value;

it should work
Roy Simkes
Yet Another Parkyeri Developer

#3 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 25 August 2006 - 02:13 PM

replace this:

<input type="radio" name="selectedUser" value="admin" />admin<br>
with
<input type="radio" name="selectedUser" id="selectedUser" value="admin" />admin<br>
and do the changement for every radio button.

your form tag should be
<form id="selectUserForm" ....>

then in your js change
var strName = selectUserForm.selectedUser.value;
with
var strName = document.getElementById('selectUserForm').selectedUser.value;

it should work


Thanks for the help radalin. It's still coming back undefined though.

This is now what's getting printed to the html source:

//note: < > removed from SCRIPT tags since it wouldn't let me post it like that
SCRIPT language="javascript"
function callEditUser( selectUserForm ) {
  var strName = document.getElementById('selectUserForm').selectedUser.value;
  var url = "/edit_user.php?name="+strName;
  document.write(url);
 // window.open(url, "_self");
}

function callDeleteUser( selectUserForm ) {
  var strName = document.getElementById('selectUserForm').selectedUser.value;
    var url = "/delete_user.php?name="+strName;
    document.write(url);
   // window.open(url, "_self");
  }
}
/SCRIPT
<!-- note: select_user.php is this file -->
<form id="selectUserForm" action="/select_user.php" method="POST">

<input type="radio" name="selectedUser" id="selectedUser"  value="admin" />admin<br>
<input type="radio" name="selectedUser" id="selectedUser"  value="drkstr" />drkstr<br>
<br />
<input type="button" value="Edit" onclick="javascript:callEditUser(this.form)" />
<input type="button" value="Delete" onclick="javascript:callDeleteUser(this.form)" />
</form>
Did I miss something small?

Thanks again!
...drkstr

#4 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 25 August 2006 - 04:13 PM

I've been reading up on javascript, and I noticed that checkbox elements are are accessed through an array ( form.elements[num].value. Do I need to do the same with the radio buttons? If so, what's the easiest way to see which element is checked? Or should I just loop though until I find it?

Thanks!
...drkstr




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users