Results 1 to 6 of 6

Thread: Losing Array Values after Text Split...

  1. #1
    Join Date
    Nov 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Losing Array Values after Text Split...

    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:

    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}

    Code:
    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:

    Code:
    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:

    Code:
    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:

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


  2. #2
    Join Date
    Nov 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:

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

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

    Last edited by Nightrider; 11-04-2007 at 05:13 AM.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <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>
    HTML Code:
    <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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Nov 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:

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

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

    Code:
    <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>

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I don't see a:

    Code:
    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:

    Code:
    <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:

    Code:
    <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>
    HTML Code:
    <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>
    Last edited by jscheuer1; 11-04-2007 at 07:58 AM. Reason: spelling
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Nov 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •