-1

I want to pass php variable to javascript variable. When i click on error, it gives a redline under "console.log(data)" But it throws the error in Console Window -

Uncaught SyntaxError: Unexpected identifier

I am new to Javascript.

Javascript:

<script>
$('#clientName').on('change', function() {
    var clientname = $(this).val();
    var clientid = $('#clients-list option[value=' + clientname +']').attr('id');

    var data = [];
    data = <?= $client_data ?>
    console.log(data);
});
</script>

PHP Variable ( $client_data ) :

    Array
(
    [0] => Array
        (
            [id] => 1
            [client_name] => John Doe
            [client_shop_street] => Baker Street
            [client_city_town] => California
            [client_pincode] => 
            [client_desc] => hello1
            [client_contact1] => 1234567890
            [client_contact2] => 1111111111
            [client_email] => 
            [client_total_amt] => 45000
            [client_paid_amt] => 37000
            [created_at] => 
            [updated_at] => 2021-06-21 18:39:54
            [created_by] => 1
        )

[1] => Array
        (
            [id] => 2
            [client_name] =>  Tom Jerry
            [client_shop_street] => Wall Street
            [client_city_town] => New York
            [client_pincode] => 
            [client_desc] => testing demo
            [client_contact1] => 2222222222
            [client_contact2] => 3333333333
            [client_email] => 
            [client_total_amt] => 45000
            [client_paid_amt] => 37000
            [created_at] => 
            [updated_at] => 2021-06-21 23:32:12
            [created_by] => 1
        )
)
  • Does it say which line number it is? – stefan_aus_hannover Jul 07 '21 at 16:33
  • Instead of trying to render spaghetti from the server, you might make an ajax call to PHP to fetch the data. – Matt Jul 07 '21 at 16:33
  • Could you share what is generated by PHP? E.g. if this is in `index.php`, you can execute the php script from terminal or by using a web browser and viewing the page source. When PHP prints the client data, it is coming out in a format that isn't valid JavaScript – alexanderbird Jul 07 '21 at 16:33
  • @alexanderbird. This is what i am trying to achieve using php - https://stackoverflow.com/q/68284371/15360495 – user15360495 Jul 07 '21 at 16:35
  • @steveo314 It redirecting me to the line that shows like this : data = [{"id":1,"client_name": John Doe, 'client_shop_street': .........}]; – user15360495 Jul 07 '21 at 16:38
  • Does this answer your question? [How do I pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) – Ivar Jul 07 '21 at 16:38
  • @steveo314 When i click on line no. it shows Redline under console.log(data) in the file. – user15360495 Jul 07 '21 at 17:08

3 Answers3

0

To expose a PHP object in a format that makes sense in JavaScript, you'll need to encode it in a JavaScript-friendly format. The simplest is to use json, which can be interpreted by JavaScript without any special parsing.

In your PHP file:

<script>
$('#clientName').on('change', function() {
    var clientname = $(this).val();
    var clientid = $('#clients-list option[value=' + clientname +']').attr('id');

    var data = [];
    data = <?= json_encode($client_data) ?>
    console_data(data);
});
</script>

json_encode docs

alexanderbird
  • 3,847
  • 1
  • 26
  • 35
0

Two problems in the code:

  1. Use json_encode when using a PHP array as a value for a JS variable.

     data = <?json_encode($client_data) ?>
    
  2. I believe it should be console.log(data)

Charlie
  • 22,886
  • 11
  • 59
  • 90
0

[Solved] Single Quotes missed around PHP Code.

<script>
$('#clientName').on('change', function() {
    var clientname = $(this).val();
    var clientid = $('#clients-list option[value=' + clientname +']').attr('id');

    var data = [];
    data = '<?php echo json_encode($client_data) ?>'
    console.log(data);
});