Help needed with php and javascript

Hi,

I am very new to coding in php and javascript, so please forgive me if this is a daft question…

I am basically trying to get a recurring date set up on a website, based on an initial date given by a user. This is to show bin collections which happen fortnightly. I have successfully brought the data in from the database of when the user has said the first bin collection happens, but I am having troubles getting the variable that holds this information to then feed into the javascript.

I know that the line ‘var firstDayOfCycle = new Date(2011, 5, 10);’ will work, so my idea was to use php to gather the same date string and then insert it, however this isn’t working. I know that the line ‘echo $NewTimeofNextBinCollection;’ is returning ‘2016, 8, 26’ which is close to what I want it to feed into the javascript. (I know that the month needs reduced by 1 though). The current output is that the javascript seems to think that the current day is the day the bin collection happens, so it’s not picking up the variable of $NewTimeofNextBinCollection.

Does anyone know how to resolve this? It’s driving me slowly mad trying to figure out what to do!

Here’s the code:

[php]<?php session_start(); ?>

<?php include 'functions.php'; if ($stmt = $mysqli->prepare("SELECT BinCollectionDate FROM building WHERE Buildingid=?")) { // opening prepare statement (2) $stmt->bind_param("s", $BuildingID); $BuildingID = $_SESSION["usersBuildingID"]; $stmt->execute(); //echo $_SESSION["usersBuildingID"]; $stmt->store_result(); $stmt->bind_result($BinCollectionDate); if ($stmt->num_rows > 0) { //opening rows (3) $stmt->fetch(); } // closing rows (3) echo $BinCollectionDate; if ($BinCollectionDate == 0) { //if no data, show button to add data echo("

No Bin Collection information set up yet.

Set up

"); } else { //opening else (6) echo "

"; //echo $BinCollectionDate; $time = strtotime(stripslashes($BinCollectionDate)); //echo $time; $time = ($time . '-1 month'); $NewTimeofNextBinCollection = (date("Y, n, j",$time)); echo $NewTimeofNextBinCollection; echo "

Change Date

"; //echo date("y, m, d",$time); } //closing else (6) $stmt->close(); } //closing prepared statement (2) $mysqli->close(); //$DateOfNextBinCollection = date("y-n-j",$time); //$DateOfNextBinCollection = date("y-m-d",$time); //echo $DateOfNextBinCollection; //$DateOfNextBinCollectionUNIXCode = strtotime($DateOfNextBinCollection . ' - 1 month'); //$DateOfNextBinCollectionUNIXCode = strtotime($DateOfNextBinCollection); //echo $DateOfNextBinCollectionUNIXCode; ?>

[/php]

First a question. Is this for a class project, or actual production environment?

Hi,

This is for a class project at uni, hand in is tomorrow so I’m in a slight last minute panic!

Much appreciate any help

Emily

Any output, html/css/js, should come after the php code executes.

You shouldn’t be using document.write. You can change the text/html using javascript without the antiquated function.

Is $time not giving the proper values? What should be in the BinCollection/building table? Are you checking when the last collection took place?

Thank you for your reply, when I move the Javascript to the bottom, nothing is displayed in the content div. As if it’s not picking up on the javascript at all.

What do you use as an alternative to document.write to display the variable ‘message’? I read online some information about using DOM manipulation but I’m not sure how that would translate for my code.

$BinCollectionDate is returning: ‘26 August, 2016’ which it collects from the Database
$time is displaying the UNIX time stamp of: 1472235360

then I have tried to get the month to reduce by 1 as the Javascript code requires months to start by 0 - Jan, 1 - Feb etc. This hasn’t worked though because…

$NewTimeofNextBinCollection is returning: ‘2016, 8, 26’.

I want the javascript to recognise that this date is the first date of the fortnightly cycle and then to display the variable message.

Thank you so much for your help!

Sponsor our Newsletter | Privacy Policy | Terms of Service