1

I have a php that echos html. Inside one of these echos I have a button that calls a javascript function. Inside of this call I pass a php variable. However when I see what the value of one of the elements is inside of the javascript I get undefined.

Any ideas?

Javascript

function addrow(innerid, teams){


    alert(teams[1]);}

Here is how I pass it, this is all inside of an echo

<input type = "button" id = '.$buttonid.'  value = "Agregar" onclick = "addrow(\'' . $leaguesarray[$numofleagues] . '\','.$teamsarray.')

So I call addrow(with a league value, and I also pass the array teamsarray from php

I've decided to try something else but I'm not getting it to work correctly.

Any suggestions?

echo '<script language="javascript">';


for ($size = 0; $size < sizeof($teamsarray);$size++){

    echo "var teamsarray[".$size."] = ".$teamsarray[$size].";\n";


    }

echo 'function addrow(innerid, size){


for (var i = 0;i< size; i++ ){

html = html + "<option value = " + teamsarray[i] + ">"+teamsarray[i]+"</option>";


    }

html = html +"</select>";
}</script>';

basically what I'm trying to do is echo the javascript through php. I'm trying to make a dropdown with values that I get from php. Which will be added dynamically with the addrow function.

3
  • Show us the generated markup. Commented Apr 11, 2012 at 20:07
  • The default string representation of arrays is "Array", I don't think you want that. Commented Apr 11, 2012 at 20:14
  • 1
    possible duplicate of pass php array to javascript function Commented Apr 11, 2012 at 20:14

2 Answers 2

1

It sounds like you might be looking for json_encode, so you might do something like this:

if (empty($teamsarray)) $teamsarray = array();
echo '<input type = "button" id = '.$buttonid.'  value = "Agregar" onclick = "addrow(\'' . $leaguesarray[$numofleagues] . '\','.json_encode($teamsarray).'); return false;" />';
Sign up to request clarification or add additional context in comments.

2 Comments

I used json_encode however javascript is still giving me a undefined for the value
Hmm, well undefined is strange since the code I suggested explicitly defines a value if it is undefined. Can you provide a jsfiddle so we can take a look? If so I could fix it up in a heartbeat.
0

In javascript, arrays look like: [2,4,6]. The php implode command can help here.

    $buttonid = 42;
    $leaguesarray[$numofleagues] = 66;
    $teamsarray = array(2,4,6);

    $teams = implode(',',$teamsarray);

    $input = <<<EOT
<input type="button" id="$buttonid" value="Agregar" onclick="addrow($leaguesarray[$numofleagues],[$teams])" />
EOT;
    echo $input . "\n";

Yields:

<input type="button" id="42" value="Agregar" onclick="addrow(66,[2,4,6])" />

Which I think is what you want.

EDITED:

@Dustin Grahams suggestion to use json_encode is a good one. Replace the implode with:

$teams = json_encode($teamsarray);

And drop the extra brackets from the template.

3 Comments

thanks for the response. However I used the json_encode and passed it with the addrow function. my javascript looks like addrow(something, teams) teams is my array. However I get undefined.
Okay so I tried your method I have my teamsarray which contains '5a7' '8' etc. I then did $teams = implode(',',$teamsarray); and passed it as $teams, however when I get the values the elements inside of the array spell out array.
As previously requested, post the actual html that you generated. Your posted code is very confusing and it's difficult to figure out what exactly is going on.

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.