Joining Variables

Discussion in 'JavaScript' started by adamjblakey, Dec 4, 2010.

  1. #1
    Hi,

    I have an ajax function which sends the value to a php form, i am trying to send another value with this but when i try and join the string it will not work.

    This is the origianl:
    
    			  	  
    	      var infoStr = "?choice="+nVal;
    			  
                  AdminRequest.open("GET", "update-file.php"+infoStr, true);
    
    Code (markup):
    This is what i have done:

    
    				var infoStr;
    			  	  
    			  infoStr = "?choice="+nVal;
    			  infoStr = infoStr + "&test=";
    			  infoStr = infoStr + document.getElementById['test'].value;
    			  
                  AdminRequest.open("GET", "update-file.php"+infoStr, true);
    
    Code (markup):
    Cheers,
    Adam
     
    adamjblakey, Dec 4, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    How does your .open function look like ?
     
    tvoodoo, Dec 4, 2010 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Here is the whole script before i have done anything to it.

    
    var AdminResponse = "";
    
    	function parseResponse(field){
    
    		var nText = AdminResponse.getElementsByTagName('optionText');
    		var nVal = AdminResponse.getElementsByTagName('optionVal');
    		document.forms["register_account"].elements[field].options.length = 1;
    		
    		for (i=0; i<nText.length; i++)
    			{ 
    			 var nOption = document.createElement('option'); 
    			 var isText = document.createTextNode(nText[i].firstChild.data); 
    			 nOption.setAttribute('value',nVal[i].firstChild.data); 
    			 nOption.appendChild(isText); 
    			 document.forms["register_account"][field].appendChild(nOption); 
    			}
    	}
    
          function update(nVal, field){
    
                  var AdminRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();   
                  AdminRequest.onreadystatechange = function()
                      {
                       if (AdminRequest.readyState == 4)
                          {
                          if (AdminRequest.status == 200)
                              {
                              AdminResponse = AdminRequest.responseXML;
                              parseResponse(field);
                              }
                          else     {
                               alert('Error update-course.php File '+ AdminRequest.statusText);
                              }
                          }
                      }
    			
    			
    			var infoStr;
    			  	  
    			  infoStr = "?choice="+nVal;
    			 
                  AdminRequest.open("GET", "update-file.php"+infoStr, true);
                  AdminRequest.send(null);
              }
    
    Code (markup):
     
    adamjblakey, Dec 5, 2010 IP
  4. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #4
    
    infoStr = "?choice="+nVal+"&test="+document.getElementById('test').value;
    
    HTML:
    Actually this should work...try it with the replacement above...
     
    tvoodoo, Dec 5, 2010 IP
  5. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Thanks for the reply, i tried this but it stopped the script from working.
     
    adamjblakey, Dec 5, 2010 IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    what i tend to do with params is often hold them into an array instead.

    eg:
    
    var params = [];
    
    params.push("choice=" + nVal);
    params.push("test=" + document.getElementById("test").value);
    // ... more
    
    var url = "/update-file.php?" + params.join("&");
    
    Code (javascript):
    this gives you increased readability and also allows you to loop and clean up parmas via things like escape, encodeURI etc. a more advanced version would be to use objects with key: value into the array with a .map callback that joins them. make sure you compose the URL _after_ they click submit so it picks up the right values.
     
    dimitar christoff, Dec 6, 2010 IP