0

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.

5 Answers 5

6

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";
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

<script language='text/javascript'>

with

<script type='text/javascript'>

regards

Comments

0

Change

<script language='text/javascript'>

To

<script type='text/javascript'>

Comments

0

Could you try changing

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

to

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

It works like this on my pc.

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.