SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    array in parameter

    Greetings!

    I have here code that sends values via ajax. That values includes an array.

    Code:
    <script type = 'text/javascript'>
    function insert() {
    if(window.XMLHttpRequest){
     xmlhttp = new XMLHttpRequest();
    } else {
    xmlhtttp = new ActiveXObject('Microsoft.XMLHttp');
    }
    
    
    xmlhttp.onreadystatechange = function() {
    		if (xmlhttp.readyState ==4 && xmlhttp.status == 200) {
    		document.getElementById('message').innerHTML = xmlhttp.responseText;
    		}
    }
    
    parameters = 'keyword='+document.getElementById('keyword').value + '&accnum='+document.getElementsByName('accnum[]').value;
    /*"parameter1=" + parameter1.value + "&parameter2=" + parameter2.value*/
    
    xmlhttp.open('POST', 'Borrow_2.php', true);
    xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xmlhttp.send(parameters);
    		
    		}
    
    </script>
    <script type = 'text/javascript'>
    function addRow()
     {
         tabBody=document.getElementById("inputtable");
    	row=document.createElement("TR");
        cell1 = document.createElement("TD");
        textnode1=document.createElement("input");
        textnode1.name="accnum[]";
    	textnode1.type="text";
        cell1.appendChild(textnode1);
        row.appendChild(cell1);
        tabBody.appendChild(row);
    
    }
    </script>
     </HEAD>
     <BODY>
    <form id="test" onsubmit="return false;" autocomplete="off">
     <div class="main">
    
      
       <table>
      <tbody id="inputtable">
         	 <div id="holder"> 
    		 <p>Student ID: <input type="text" id="keyword" tabindex="0" name = 'keyword'></p>
    		  <div id="ajax_response"></div>
    		 <tr><td>SAccession No: <input type="text" id="accnum" tabindex="0" name = 'accnum[]' >
    		 <input type="button" value="Add new" onclick="addRow();" /></td></tr>		 
    				
    				<br/>
    				<br/>
    				<br/>
    		        
    				 
    		
    		
    		  <input type="submit" value="submit" onClick="insert();"/>
    My problem is I can't get the value of my accnum[].
    I think the problem is this line, and I don't know how to fix it.
    Code:
    parameters = 'keyword='+document.getElementById('keyword').value + '&accnum='+document.getElementsByName('accnum[]').value;
    When I used var_dump(); it says that 'undefined strings'. What's that mean? thank you.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The getElementsByName method returns an array-like object.

    Consider for example if you have multiple radio buttons with the same name. The getElementsByName method gives you an array elements that match the name.
    Even if you end up with one element being found, that will still be in the array-like structure, so you need to specify the index of the item you want to use, that being an index of 0.

    Code:
    ... + document.getElementsByName('accnum[]')[0].value;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Dec 2011
    Posts
    221
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much for your response.

    I'm using an array of textbox, that when the user hit the add new textbox button it will add new row of textbox.
    I tried the code below but it only shows the first value of my textbox. I understand now the index value when it is specified.
    but I don't have any idea how to do it in textbox.
    Code:
    parameters = 'keyword='+document.getElementById('keyword').value + '&accnum='+document.getElementsByName('accnum[]')[0].value;


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
  •