12

I am trying to pass 2 values to a javascript xmlHttp request. The values are being passed to the javascript function. I was successfully passing a single value to the javscript function, but now I need to pass 2 values. The bold value is a string name I want in the javascript.

 echo "<a href='#' class='thumb'><img class='thumb-img' value = ".$row->aid." onclick='getVote(".$row->aid.", **".$row->atitle."**)' src='images/roadies/th".$row->aid.".jpg' />    </a>";

One is an int and the other is a string.

In the javascript, I am not sure how to receive these values. Earlier I used to do this:

function getVote(int)
{
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    value = int;

for a single value. But now since there are 2 values to process, I don't know how to write the function for it. I am currently (unsuccessfully) trying this:

function getVote(int, name)
{
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
    value = int;
    name= char;

Please tell me how to do it?

1
  • no i dont want to send it to the server. i just need it in the javascript file. post updated. Commented Dec 20, 2009 at 19:56

4 Answers 4

20

If I understand correctly, your question is simply: how do javascript functions receive multiple arguments?

This is easy, just separate them by a comma in your function declaration, and pass multiple values, again separated by comma in the function call:

function myFunc(one, two) {
    alert(one); alert(two);
}

myFunc(1,2);

If you don't know in advance how many arguments to pass/receive, just declare the function without arguments, and use the built-in arguments array inside the function definition:

function myFunc(){
    for (var i=0, numArgs = arguments.length; i<numArgs; i++){
        alert(arguments[i]);
    }
}

The approach above is nice if you need to pass a list of values that are all equal, but when you have to deal with multiple arguments and some of them are optional, a better approach is to pass an object literal, and add the 'arguments' as properties:

function myFunc(obj){
    if (typeof(obj.arg1)!="undefined") {
        ....
    }
    if (typeof(obj.arg2)!="undefined") {
        ....
    }
    ...more handling...
}

myFunc({
    arg1: "one"
,   arg2: "two"
,   ...more properties...
});
Sign up to request clarification or add additional context in comments.

Comments

17

You probably want to enclose the title in quotes. Let's say you have a row with aid 123 and title Hello World. You want to have onclick="getVote(123,'Hello World')"

2 Comments

i was just reading the same on daniweb. thanks a lot. stackoverflow is a great resource.
I know this is an old post/answer, but your answer calling out that text must be surrounded by single bar quotes is what saved my bacon. Short and sweet, but resolved my issue. Thanks.
1

In JavaScript, you don't need to declare variable types in parameter lists. So, your function would be:

function getVote(myInt, name) {

Make sure, though, when sending your value to the server, use myInt.toString() instead of just myInt, as you did with name.

Comments

1
function getVote(int, name)
{
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
    {
        alert ("Browser does not support HTTP Request");
        return;
    }
   alert(int);
   alert(name);
}

This variables are all ready defined in your function once you get them as parameters.
My code will alert them to show you they are deined

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.