0

I’m trying to transform an array from PHP to JavaScript but it doesn’t seem to work. Here’s my PHP code query.php:

$query = 'SELECT coupon FROM '.$disc;
$coupdb = array();
$results = $newdb->get_results($query);
foreach( $results as $result )
$coupdb[] = $result->coupon;

echo $coupdb[0]; //This shows perfectly the content but I want to send this array to a Javascript file.

I've tried too with JSON:

$coupdb_js = json_encode($coupdb);

Here's my JavaScript file discount.js:

var coupdb = <?php echo $coupdb ?>;
alert(coupdb[0]);

And with JSON:

var coupdb = <?php echo $coupdb_js ?>;
alert(coupdb[0]);
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
user3321425
  • 201
  • 1
  • 2
  • 17
  • `var coupdb = = json_encode($coupdb_js) ?>;`. Did you look at the generated JS? It should be obvious that `var coupdb = Array;` is not correct. – Jon May 12 '14 at 23:46
  • yeah, you're not actually json encoding anything. It just tries to turn it into a string, which doesn't work. And you don't try to json decode it either – markasoftware May 12 '14 at 23:47
  • Sorry, I was editing the question. @Markasoftware $newdb It's my Wordpress external database connection. The query is working, I can show the array with: echo $coupdb[0]; or echo $coupdb[1]; – user3321425 May 12 '14 at 23:52
  • What is the result of the `alert( coupdb );`? Do you get an alert dialog? What does it say? Also, are you testing this with a web server? Are you sure it runs your `.js`-file as a PHP script? – fredrikhl May 12 '14 at 23:58
  • This is a possible duplicate of [this post](http://stackoverflow.com/questions/5618925/convert-php-array-to-javascript/5619038#5619038). – lschlessinger May 12 '14 at 23:59
  • How does the PHP connect to the JavaScript? This seems 100% incorrect? `var coupdb = ; alert(coupdb[0]);` – Giacomo1968 May 12 '14 at 23:59
  • @Jon shows me an "Unexpected token <"... It takes the as unknown token. – user3321425 May 12 '14 at 23:59
  • @JakeGould I'm calling the .js via form and a button on it. Everything It's working but I can't pass the PHP array to Javascript. – user3321425 May 13 '14 at 00:02
  • @lschessinger yeah, tried it before post but didn't work. – user3321425 May 13 '14 at 00:05
  • @fredrikhl you're right, I think my server It's not running my .js as a PHP script, because It's not able to recognize – user3321425 May 13 '14 at 00:08
  • Why is there no `JSON.parse()` anywhere? It's not going to work without that – markasoftware May 13 '14 at 00:20

2 Answers2

0

@Jon shows me an "Unexpected token <"... It takes the <? as unknown token.

So your PHP isn't being processed. If this is in a .js file, it won't work unless the server is set up to process those files (bad idea)

Instead, this should probably be in a <script> tag in your HTML, or perhaps better as an AJAX call.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • You're totally right, this is in a .js file and can't recognize php tags. – user3321425 May 13 '14 at 00:10
  • @user3321425 Your code & examples are a mess to debug from our side. You realize you load the PHP file as a URL via a web server & then use something like ParseJSON in jQuery to decode? http://api.jquery.com/jquery.parsejson/ – Giacomo1968 May 13 '14 at 00:12
  • I'm sorry @JakeGould it's hard to figure out because there's a lot of code to paste. But the resume is that I've been proving to pass $coupdb[] to a .js file with and without json with bad results. Seems like there's a problem with the php tags interpretation.. and if I use the – user3321425 May 13 '14 at 00:22
0

Finally I've found a way, It's an easy way but maybe not the best one :)

From my php I've sent the coupdb variable catched from bbdd to javascript file:

<?php
$DB_USER="dbuser";
$DB_PASSWORD="passdb";
$DB_NAME="dbname";
$DB_HOST="dbhost";

$newdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST);
$disc="discounts";


$query = 'SELECT coupon FROM '.$disc;
        $coupdb = array();
        $results = $newdb->get_results($query);
        foreach( $results as $result )
        $coupdb[] = $result->coupon;

?>
<script type="text/javascript">
    var coupdb = <?php echo json_encode($coupdb); ?>;
</script>

And in js file I used the variable normally:

alert (coupdb[0]);
user3321425
  • 201
  • 1
  • 2
  • 17