Correct method on passing PHP variables that contains date and time to javascript -


i'm bit confuse on passing php variables contains date on onclick attribute of button, example.. (for reason, put button inside echo)

<?php $date = date("f-d-y", "2015-07-24"); //to convert string 2015-07-24 date specific format echo "<button onclick = 'gotothisjsmethod(".$date.")'> pass </button>"; ?> 

on javascript part wherein gotothisjsmethod written.

function gotothisjsmethod(daterec){  alert (daterec); }  

the syntax above results nothing , alert box didn't appear

i tried changing above code, alter parameter passed on php, instead of using $date variable, used exact string of date parameter , change javascript this:

function gotothisjsmethod(daterec){     var date = new date(daterec);     alert(date.getmonth()+1 " " + date.getdate() + " " + date.getfullyear()); } 

yes there's result again, returns default date of 1/1/1970. should regarding problem?

the biggest problem second argument date() has timestamp, need use strtotime. you'll "january-01-1970" because you're passing string needs timestamp.

you need quotes around string, below. check rendered html in browser see.

<?php $date = date("f-d-y", strtotime("2015-07-24"));  echo "<button onclick=\"gotothisjsmethod('{$date}')\"> pass </button>"; ?> 

i converted html use " , php use variable interpolation.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -