1.11M Members

Problem with passing variable

 
0
 

Hello i am trying to make my Ajax fetch the right <input button value.
The <button is generated by PHP and will look like this.

<input type="button" value="Private" id="txtCustomerId" onclick="requestAlbumInfo()"/>
<input type="button" value="Public" id="txtCustomerId" onclick="requestAlbumInfo()"/>
<input type="button" value="Nature" id="txtCustomerId" onclick="requestAlbumInfo()"/>

My Ajax needs to fetch the value="" and send it to PHP. My ajax looks like this.

<script type="text/javascript"> 
        var url = "uimages_class.php?album="; // The server-side script 
       function handleHttpResponse() {    
        if (http.readyState == 4) { 
              if(http.status==200) { 
                  var results=http.responseText; 
              document.getElementById('divCustomerInfo').innerHTML = results; 
              } 
              } 
        } 
        
        function requestAlbumInfo() {      
            var sId = document.getElementById("txtCustomerId").value; 
            http.open("GET", url + escape(sId), true); 
            http.onreadystatechange = handleHttpResponse; 
            http.send(null); 
        } 
function getHTTPObject() { 
  var xmlhttp; 

  if(window.XMLHttpRequest){ 
    xmlhttp = new XMLHttpRequest(); 
  } 
  else if (window.ActiveXObject){ 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    if (!xmlhttp){ 
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
    } 
    
} 
  return xmlhttp; 

  
} 
var http = getHTTPObject(); // We create the HTTP Object 
</script>

It does retrieve data to the <div id="divCustomerInfo"></div> but only the first button, in this case "Private"... Now i can see myself that its because the Ajax fetches one value by the element id "txtCustomerId", which all the generated buttons has.

But how can i make the Ajax fetch a value by the button clicked?
Some sort of array for the "txtCustomerId" id? i am really new with Ajax, would love some help.

 
0
 

The real question here is how can i do it something like this?:

the buttons:

<input type="button" value="FunnyAlbum" id="FunnyAlbum" onclick="requestAlbumInfo('FunnyAlbum')"/>

then fetch the string...and use it inside getElementById...

function requestAlbumInfo(string) {      
            var sId = document.getElementById(string).value; 
            http.open("GET", url + escape(sId), true); 
            http.onreadystatechange = handleHttpResponse; 
            http.send(null); 
        }
 
0
 

Well i solved this by modifying this:

function requestAlbumInfo(str) {      
            var sId = document.getElementById(string).value; 
            http.open("GET", url + str, true); 
            http.onreadystatechange = handleHttpResponse; 
            http.send(null); 
        }


onclick="requestAlbumInfo(this.value)"
Question Self-Answered as of 2 Years Ago
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: