Jump to content

accessing the vaule of multiple radio buttons


drkstr

Recommended Posts

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.

[code]<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>[/code]

My Java script section looks like this. [code]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");
  }
}[/code]

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)
[code]<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>[/code]
Link to comment
Share on other sites

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

your form tag should be
[code]
<form id="selectUserForm" ....>
[/code]

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

it should work
Link to comment
Share on other sites

[quote author=radalin link=topic=105572.msg421872#msg421872 date=1156504561]
replace this:
[code]
<input type="radio" name="selectedUser" value="admin" />admin<br>
[/code]
with
[code]
<input type="radio" name="selectedUser" id="selectedUser" value="admin" />admin<br>
[/code]
and do the changement for every radio button.

your form tag should be
[code]
<form id="selectUserForm" ....>
[/code]

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

it should work
[/quote]

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

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

[code]//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[/code]
[code]<!-- 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>[/code]Did I miss something small?

Thanks again!
...drkstr
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.