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.

user541597
  • 4,247
  • 11
  • 59
  • 87

2 Answers2

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;" />';
Dustin Graham
  • 2,054
  • 1
  • 20
  • 24
  • I used json_encode however javascript is still giving me a undefined for the value – user541597 Apr 11 '12 at 21:56
  • 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. – Dustin Graham Apr 12 '12 at 00:37
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.

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • 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. – user541597 Apr 11 '12 at 22:02
  • 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. – user541597 Apr 11 '12 at 22:12
  • 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. – Cerad Apr 12 '12 at 00:07