Jaiff
Jaiff

Reputation: 487

How to pass select value by javascript?

like this is my form's select box

<select name="cutid" onchange="findcustid(this.value)">
<option value="1001">cust 1</option>
<option value="1002">cust 2</option>            
<option value="1003">cust 3</option>            
</select>

now i have this ajax code which is passing and fill some select boxes

function findcustid(custid) {
    var custid = custid;
}

function Inint_AJAX() {
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {} //IE
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {} //IE
    try {
        return new XMLHttpRequest();
    } catch (e) {} //Native Javascript
    alert("XMLHttpRequest not supported");
    return null;
};

function dochange(src, val) {
    var req = Inint_AJAX();
    req.onreadystatechange = function () {
        if (req.readyState == 4) {
            if (req.status == 200) {
                document.getElementById(src).innerHTML = req.responseText; //retuen value
            }
        }
    };
    req.open("GET", "podetfill.php?data=" + src + "&val=" + val + "&custid=" + custid); //make connection
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
    req.send(null); //send value
}
window.onLoad = dochange('proid', -1); // value in first dropdown

on this line

req.open("GET", "podetfill.php?data="+src+"&val="+val+"&custid="+custid);

of above code i've added +"&custid="+custid this but its not pass value of custid on podetfill.php page and getting error

Error: custid not defined

Upvotes: 1

Views: 7097

Answers (5)

user753137
user753137

Reputation:

Your ajax request is sent when window is loaded(by window.onLoad = dochange('proid', -1);) when you have not yet made a customerid selection, thus it's either undefined or null.

Send your ajax request on your selection instead.

<select name="cutid" onchange="dochange('proid', -1)" >

and add following in the beginning of dochange function as ManiseUK said

var cus = document.getElementById('custid');
var custid = cus.options[cus.selectedIndex].value;

remove
window.onLoad = dochange('proid', -1);

Upvotes: 0

Code Spy
Code Spy

Reputation: 9964

  <select name="report_type" onchange="findcustid(this.options[this.selectedIndex].value);">
     <option value="1001">cust 1</option>
     <option value="1002">cust 2</option>            
     <option value="1003">cust 3</option>            
  </select>​​​​​​​​​​​​​​​​​​​​​​​​​​


  <script lang="javascript" type="text/javascript"  >
   function findcustid(custid) {
   var custid = custid;
   //alert(custid);
   }
  </script>

Upvotes: -1

Manse
Manse

Reputation: 38147

You cannot use your method for the following reasons :

  • the var custid is declared within a function - so its only accessible within that function
  • Even if the var custid was declared a global you still couldn't use it as the AJAX gets executed using the window.load event meaning the select.change event wouldnt have been fired yet

One solution would be to get the value of the select before using it :

var cus = document.getElementById('custid');
var custid = cus.options[cus.selectedIndex].value;
req.open("GET", "podetfill.php?data=" + src + "&val=" + val + "&custid=" + custid); //make connection

you also need give the select an id attribute to use this code :

<select id="custid" name="cutid" onchange="findcustid(this.value)">

Upvotes: 2

ymutlu
ymutlu

Reputation: 6715

this worked for me, there is fiddle link below.

<select name="report_type" onchange="alert(this.options[this.selectedIndex].value);">
<option value="1001">cust 1</option>
<option value="1002">cust 2</option>            
<option value="1003">cust 3</option>            
</select>

http://jsfiddle.net/ymutlu/JVrwL/

Upvotes: 0

Kelly
Kelly

Reputation: 3709

Move your declaration of var custid outside the function. It is not visible to the dochange function

Upvotes: 1

Related Questions