0

I am searching in this page for an answer to my question, but I dont found it.

<script type="text/JavaScript">

var jsarray=     [];
<?php 
include("conectar.php");

    if (isset($_GET['selector'])) $condicion = ' WHERE clave=' . $bd->quote($_GET['selector']);
    else $condicion = '';

    $pet = $bd->prepare("SELECT * FROM Calendario" . $condicion);
    $pet->execute();
    $filas = $pet->fetchAll();
    $arrayF=array();
    $arrayU=array();
    print '<table class="gridtable">';
    foreach ($filas as $fila) 
    {
        //print '<tr><td>'.$fila['Fecha'] . '</td><td>' . $fila['URL'] . '</td></tr>';
        array_push($arrayF,$fila['Fecha']);
        array_push($arrayU,$fila['URL']);
    }
for ($i=0 ;$i < count($arrayF); $i++){
    echo 'jsarray['.$i.'] = "'.$arrayF[$i].'";';
}
 ?> 
 window.onload=function(){
 alert(jsarray);
}

</script>

I am accesing to a database, getting some data and the, putting into arrayF and arrayU. Then, i have to do to send the array to the JavaScript to work with them. But this dont work. I dont know why, but when I execute it, dont show me nothing, but the arrays in php are with elements, I am sure of this, beacause

Anyone knows why occurs this?

0

3 Answers 3

4

You can use json_encode function for that. You can also find some simple examples here.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do it in two ways, serialize and json_encode

2 Comments

You can't do it with serialize, it doesn't return JSON.
you can then unserialize it in JS: phpjs.org/functions/unserialize
0

Your code inserts <table...> in JavaScript code which makes it uncompilable. Remove the print line.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.