nunos
nunos

Reputation: 21429

Help with simple javascript php/js array

Can someone please tell me what's wrong with this code? Basically what I am doing is creating a php array for some GET variables. Then, I create a js array from that php array and pass it to a js function. The problem is that the function is not being called. I don't get to see the "hi" alert pop up.

<script type="text/javascript">
function change_values(js_array)
{
    alert("hi");
}
</script>
<?php

$first_date_month   = @$_GET["first_date_month"];
$last_date_month    = @$_GET["last_date_month"];
$resume_date_month  = @$_GET["resume_date_month"];
$pay_date_month     = @$_GET["pay_date_month"];

$first_date_day     = @$_GET["first_date_day"];
$last_date_day      = @$_GET["last_date_day"];
$resume_date_day    = @$_GET["resume_date_day"];
$pay_date_day       = @$_GET["pay_date_day"];

$pay_time_hour      = @$_GET["pay_time_hour"];
$pay_time_minutes   = @$_GET["pay_time_minutes"];

$args = array($first_date_month, $first_date_day, $last_date_month, $last_date_day, $resume_date_month, $resume_date_day, $pay_date_month, $pay_date_day, $pay_time_hour, $pay_time_minutes);

print_r($args);

echo "<script language='text/javascript'>\n";
echo "var js_array = new Array();\n";
foreach($args as $key => $value)
  echo "js_array[$key] = $value;\n";
echo "change_values(js_array)\n";
echo "</script>\n";

Some of the html source code that might be helpful to better understand what's going on.

<script type="text/javascript">
function change_values(js_array)
{
    alert("ola");
}
</script>
Array
(
    [0] => 3
    [1] => 99
    [2] => 99
    [3] => 99
    [4] => 99
    [5] => 99
    [6] => 99
    [7] => 99
    [8] => 99
    [9] => 99
)
<script language='text/javascript'>
var js_array = new Array();
js_array[0] = 3;
js_array[1] = 99;
js_array[2] = 99;
js_array[3] = 99;
js_array[4] = 99;
js_array[5] = 99;
js_array[6] = 99;
js_array[7] = 99;
js_array[8] = 99;
js_array[9] = 99;
change_values(js_array);
</script>

Thanks in advance.

Upvotes: 0

Views: 171

Answers (5)

svens
svens

Reputation: 11638

Could you try changing

echo "<script language='text/javascript'>\n";

to

echo "<script type='text/javascript'>\n";

It works like this on my pc.

Upvotes: 0

Zed
Zed

Reputation: 57678

Change

<script language='text/javascript'>

To

<script type='text/javascript'>

Upvotes: 0

Gumbo
Gumbo

Reputation: 655825

You have used the language attribute for your second script element and not type. Try it with type="text/javascript" instead. You should also use json_encode instead of your own function.

Upvotes: 1

Atmocreations
Atmocreations

Reputation: 10071

On the second part, where the array is mapped into js, try replacing

<script language='text/javascript'>

with

<script type='text/javascript'>

regards

Upvotes: 0

Luk&#225;š Lalinsk&#253;
Luk&#225;š Lalinsk&#253;

Reputation: 41326

It's <script type='text/javascript'> not <script language='text/javascript'>. Also, you can use this to serialize the array (but that's not the problem here):

echo "var js_array = " . json_encode($args) . ";\n";

Upvotes: 6

Related Questions