Uncaught Syntax Error with Javascript and Php Strings


I have a php webpage that displays a calendar with specific dates displayed on it.

The dates are stored in a MySql database.

At the beginning of the webpage, I do the following:

//Load in database

$query = "SELECT ID, Full_Name, Arrival_Date, Departure_Date FROM bookings ORDER BY Arrival_Date";

if ($result = $dbc->query($query))       //Retrieve all the booking details
    $numrows = $result-> num_rows;


    for ($count = 0; $count < $numrows; $count++)
        $row = $result->fetch_assoc();

        $id_array[$count] = $row['ID'];
        $name_array[$count] = $row['Full_Name'];
        $arrival_array[$count] = $row['Arrival_Date'];
        $departure_array[$count] = strval($row['Departure_Date']);

ID is a number Full_Name is a string Arrival_Date and Departure_Date are dates (YYYY-MM-DD)

The calendar is a javascript object therefore I need to get the booking information into my javascript function to be able to use it.

This is how I am trying to do that:

<script type="text/javascript">
    var array_ID = [<?php echo implode(",",$id_array); ?>];
    var array_name = [<?php echo implode(",",$name_array); ?>];
    var array_arrival = [<?php echo implode(",",$arrival_array); ?>];                                           
    var array_departure = [<?php echo implode(",",$departure_array); ?>]; 

    var booking_numbers = <?php echo $numrows; ?>;

However, I am getting an error 'Uncaught SyntaxError: Unexpected Identifier' on the line:

var array_name = [<?php echo implode(",",$name_array); ?>];

I suspect this is because some of the names have spaces in as the array is coming up as

var array_name = [Pippa,Pippa Smith,Pippa Smith];

When the php values are put in.

So here's my question. How can I tell javascript to treat those inputs as a string.

And I'm aware this might be a terrible way of combining javascript and php, so if anyone has any better suggestions, I'll happily take them!




1) one way around is usingimplode in php and spliting it in javasciript usingsplit..

best way though is to useecho json_encode($id_array) in php so that it converts the array into javascript object




You have to mark string literals for javascript by putting them between quotes.

var array_name = ["Pippa","Pippa Smith","Pippa Smith"];

Use json_encode() to get a valid javascript object literal from your php value(s).

on a side-note: why do you change the structure of the data from

[1] x y z
[2] X Y Z


 [1]  [2]
  x    X
  y    Y
  z    Z

i.e. why do you have those separate variablesarray_ID, array_name, array_arrival, array_departure instead of one array holding all the information?

People are also looking for solutions to the problem: php - Real scheduler for wordpress plugins


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.