0

When I send variable using following ajax function, num and value both variable gets num variable value only. How could I pass different values. Can anyone help me?

<script>
function calculate_rate(num,value)
    {                           
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("costing").innerHTML=xmlhttp.responseText;
            }
        }            
        xmlhttp.open("GET","data.php?num="+num+"&value="+value,true);
        xmlhttp.send();
    }// showHint
</script>

<span id="costing"></span>        

<form>
    <label>Number</label>
    <span>
      <select name="num" onchange="calculate_rate(this.value)">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>                           
      </select><br />
    </span>  
    <label>Value</label>
    <span>
        <input type="text" name="value"  value="" onchange="calculate_rate(this.value)"  />     
    </span>
</form>   
2
  • 3
    you are only passing one value into the function in both onchange events. Commented Aug 16, 2013 at 13:19
  • in addition to above comment you have not defined 'xmlhttp' Commented Aug 16, 2013 at 13:21

1 Answer 1

1

You are only passing one of the two variables with each call. Try this

function calculate_rate(num,value)
        {                           
                    xmlhttp.onreadystatechange=function()
                    {
                        if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        {
                            document.getElementById("costing").innerHTML=xmlhttp.responseText;
                        }
                    }            
            xmlhttp.open("GET","data.php?num="+num+"&value="+value,true);
            xmlhttp.send();
        }// showHint
</script>




 <span id="costing"></span>        

      <form>
      <label>Number</label>
      <span>
          <select id="num" onchange="calculate_rate(this.value,document.getElementById('value').value))">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>                           
          </select><br />

        <label>Value</label>
        <span>
        <input type="text" id="value"  value="" onchange="calculate_rate(document.getElementById('num').value,this.value)"  />     
        </span>
       </form> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.