0

I need to pass a URL to a JavaScript function something like the following.

<script type="text/javascript" language="javascript">

var xmlhttp='';   
function ajax()
{
    if(window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
         xmlhttp = new ActivexObject("Microsoft.XMLHTTP");
    }
}     

function someFunction(orders_per_page, url)
{
     ajax();
     var val=document.getElementById("txt_order_amount").value; 
     xmlhttp.onreadystatechange=function()
     {
          if(xmlhttp.readyState==4 && xmlhttp.status==200)
          {                
               document.getElementById("list").innerHTML=xmlhttp.responseText;
          }
     }

     xmlhttp.open("GET",url, true);
     xmlhttp.send();        
}
</script>

<select 
    id="cmb_page" name="cmb_page" 
    onchange="someFunction(this.value, "reports/OrderAmount.php?val="+val+"&orders_per_page="+orders_per_page+"&pageNo="+getSelectedPage();">

    <option value="1">Some Value</option>
</select>

I need to pass a URL string as mentioned on the onchange even of the <select><option></option></select> element. Is it possible?

1
  • 1
    It is possible. And I can't find any error in your function at first sight. What's the problem with it? Make sure your val and orders_per_page vars are defined, as well as getSelectedPage() returning a valid value. Commented Jun 2, 2012 at 5:29

2 Answers 2

1

try this

onchange="someFunction(this.value, 'reports/OrderAmount.php?val='+val+'&orders_per_page='+orders_per_page+'&pageNo='+getSelectedPage());"

Note: I have replaced double quotes with single quotes

Sign up to request clarification or add additional context in comments.

2 Comments

Already tried that. It's not working. Internet Explorer only reports "Syntax error."
it works if those variables were set in the right place - i just tested it. Check my comments below
1

You can also break your code a little bit to prevent as much tag soup.

I assume your val var is defined and in the global scope:

onchange="someFunction(this.value)"

function someFunction(orders_per_page) {
    var url = "reports/OrderAmount.php?val="+val+"&orders_per_page="+orders_per_page+"&pageNo="+getSelectedPage();
    ajax(); // ... rest of your function

Make sure getSelectedPage() is returning a string, or a number which can be parsed as string.

6 Comments

No I need to pass this URL from this function only onchange="someFunction(this.value)" as one more parameter. What you mentioned is working fine.
In my code above, I'm defining the url inside the function itself. I have no idea where the val var or getSelectedPage() function are or what values they have or return, but my example above will do basically the same as your url defined inside the HTML. Give it a try and check if there will be a syntax error still :)
Or do you mean, you have another function call passing a completely different url? I can review the code if that's case, but if @tunmise fasipe's answer didn't solve your problem, there's more likely something else breaking your code.
I just tested the code now and it worked in chrome and IE9. May be "val" and "orders_per_page" and "getSelectedPage()" function were not declared. I declared them and they worked. For val and orders_per_page to work, you have to declare them global
Well, I alternatively changed the code to pass only the page name that I need to change through this function onchange="someFunction(this.value, 'OrderAmount.php')" and concatenated it to the URL. Not the approach I was looking for but somehow it works.
|

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.