PDA

View Full Version : Losing Array Values after Text Split...



Nightrider
11-04-2007, 04:24 AM
I'm trying to code a Resume utility for a friend using php, MySql, and html through phpBB, but I am fighting some strange problems that don't make sense to me. This is my code:


function cat_select(cat_index)
{
var txtarea = document.main.transportation;
var salaray = document.main.salary_range_select;
var relocate = document.getElementById("main_relocate");
var travel = document.getElementById("main_travel");
var resume_list = "{RESUME_LIST}";
var resume_list_array = resume_list.split('@');

//Reset the fields
salaray.selectedIndex = 0;
relocate.checked = 2;
travel.checked = 2;
txtarea.value = '';

for (i=0;i<resume_list_array.length;i++)
{
var next_record = resume_list_array[i];
var resumes_array = next_record.split("|");

if (resumes_array[0] = cat_index)
{
salaray.selectedIndex = parseInt(resumes_array[1]);
relocate.checked = parseInt(resumes_array[2]);
travel.checked = parseInt(resumes_array[3]);
txtarea.value = resume_list_array[i];
return;
}
}
return;
}

The following is what is assigned to {RESUME_LIST}


10|9|1|0|plane, car@21|0|2|2|@89|7|0|0|Car@93|10|1|1|plane, car
Once this is split, based on the @ character, it should be assigned to an array with the following values, which appears to be working correctly:


10|9|1|0|plane, car
21|0|2|2|
89|7|0|0|Car
93|10|1|1|plane, car
Then I split this on "|" to retrieve 5 sub array items, which also initially seems to be working correctly. The following seems to correctly compare the first item in the sub array (89) to the cat_index value:


if (resumes_array[0] = cat_index)
But once inside the if statement, the matched value in the resume_list_array[i] item is wrong. IOW, when I match on the cat_index 89, the value assigned to resume_list_array[i] seems to be "10|9|1|0|plane, car" rather than "89|7|0|0|Car" like I would expect. All the items in the resumes_array[1-4] array are assigned incorrectly at that point as well...

Why wouldn't the value assigned in resume_list_array[i] be "89|7|0|0|Car" once I match resumes_array[0] = cat_index???

Also, I have a an object with 3 radio buttons assigned 0,1,2, representing yes, no, and maybe. I can set the radio check to 1 (yes), but I can't get it assigned to 0 (no) or 2 (maybe). I tried using both the following with no success:


travel.checked = 0
travel.value = 0
Thanks in advance...

http://img398.imageshack.us/img398/3663/dontknow6sf.gif

Nightrider
11-04-2007, 05:07 AM
Ok, I figured out the first part of the problem. I used a single equal sign in my if statement, losing the values assigned to resumes_array[0]. I looked at that for hours and didn't see it until a few minutes after posting this message. LOL

Ok, since I am new to javascript programming, I'm still having trouble assigning values to radio objects when there are 3 or more of them. So if anyone can help with the second part, I would greatly appreciate it...

To restate the problem, have a an object with 3 radio buttons assigned 0,1,2, representing yes, no, and maybe. I can set the radio check to 1 (yes), but I can't get it assigned to 0 (no) or 2 (maybe). I tried using both the following with no success. This is what I've attempted so far, without success:


relocate[2].checked;
relocate[2].checked = true;
relocate[2].checked = 2;
relocate.checked = 2;
relocate.value = 2;
I can successfully assign the following though:


relocate.checked = 1;
Thanks again in advance...

http://img398.imageshack.us/img398/3663/dontknow6sf.gif

jscheuer1
11-04-2007, 05:44 AM
checked is boolean. It can only be true or false. This can also be represented as 0 or 1. Those are the only choices. In a group of radio buttons in a single form, all of the same name, only one may be checked. The value attribute of a radio button may be anything you like.

So the strategy is generally to determine which one is checked and to read its value:


<script type="text/javascript">
function which(but){
for (var i = 0, b=document.getElementsByName(but.name); i < b.length; i++)
if(b[i].checked)
alert('The answer is '+b[i].value);
}
</script>



<form action="#">
<div>
<input onclick="which(this);" type="radio" name="choice" value="yes"> Yes<br>
<input onclick="which(this);" type="radio" name="choice" value="no"> No<br>
<input onclick="which(this);" type="radio" name="choice" value="maybe"> Maybe
</div>
</form>

Nightrider
11-04-2007, 06:47 AM
Thanks for the response John. I am having trouble setting the correct radio button through code though, not retrieving it. How would I go about setting one of my three choices to 0 (no) or 2 (maybe)? When I use any of the following, it breaks the code:


relocate["1"].checked = true;
or
relocate[1].checked = true;
or
relocate['1'].checked = true;
relocate is assigned to the following:


var relocate = document.getElementById("main_relocate");
This is how the radio options are defined:


<td class="row2" width="78%"> <span class="gen">
<input type="radio" name="main_relocate" tabindex="3" value="1" {RELOCATE_YES} />&nbsp;{L_YES}&nbsp; &nbsp;
<input type="radio" name="main_relocate" tabindex="4" value="0" {RELOCATE_NO} />&nbsp;{L_NO}&nbsp; &nbsp;
<input type="radio" name="main_relocate" tabindex="5" value="2" {RELOCATE_MAYBE} />&nbsp;{L_MAYBE}&nbsp; &nbsp;
</span> </td>
http://img398.imageshack.us/img398/3663/dontknow6sf.gif

jscheuer1
11-04-2007, 07:36 AM
I don't see a:


document.getElementById("main_relocate")

which would be an element with an id of 'main_relocate', anywhere in your markup. I do see three with a name of 'main_relocate'.

The red parts don't do anything I am aware of. I think that they are invalid:


<input type="radio" name="main_relocate" tabindex="3" value="1" {RELOCATE_YES} />&nbsp;{L_YES}&nbsp; &nbsp;
<input type="radio" name="main_relocate" tabindex="4" value="0" {RELOCATE_NO} />&nbsp;{L_NO}&nbsp; &nbsp;
<input type="radio" name="main_relocate" tabindex="5" value="2" {RELOCATE_MAYBE} />&nbsp;{L_MAYBE}&nbsp; &nbsp;

If I wanted to set these buttons, I would probably just do it via their order in the markup, collecting them by their name. But, since you seem to want to do it in connection with their value, here goes:


<script type="text/javascript">
function setBut(n, v){
for (var i = 0, b=document.getElementsByName(n); i < 2*b.length; i++)
if(b[i])
b[i].checked=0;
else if(b[i-b.length].value==v)
b[i-b.length].checked=1;
}
</script>


<table>
<tr>
<td class="row2" width="78%"> <span class="gen">
<input type="radio" name="main_relocate" tabindex="3" value="1">&nbsp;{L_YES}&nbsp; &nbsp;
<input type="radio" name="main_relocate" tabindex="4" value="0">&nbsp;{L_NO}&nbsp; &nbsp;
<input type="radio" name="main_relocate" tabindex="5" value="2">&nbsp;{L_MAYBE}&nbsp; &nbsp;
</span> </td>
</tr>
</table>
<div>
<input type="button" value="Yes" onclick="setBut('main_relocate', 1);"><br>
<input type="button" value="No" onclick="setBut('main_relocate', 0);"><br>
<input type="button" value="Maybe" onclick="setBut('main_relocate', 2);">
</div>

Nightrider
11-04-2007, 08:24 AM
Thanks a lot John. Your setBut function seems to be working well for me. I can see that I have a lot to learn since this basic radio assignment proved to be a such struggle...

I definitely appreciate your patience and quick response...

http://img505.imageshack.us/img505/4002/banana9sw.gif