Pippa Rose Smith
Pippa Rose Smith

Reputation: 1416

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:

<?php
//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;

    $id_array;
    $name_array;
    $arrival_array;
    $departure_array;

    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']);
    }
}
else
{
    //Error
}
?>

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; ?>;
</script>

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!

Upvotes: 1

Views: 511

Answers (2)

bipen
bipen

Reputation: 36531

1) one way around is using implode in php and spliting it in javasciript using split..

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

Upvotes: 3

VolkerK
VolkerK

Reputation: 96159

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

to

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

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

Upvotes: 1

Related Questions