2 Replies - 398 Views - Last Post: 27 July 2012 - 03:33 AM

#1 nick2price  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

php inside javascript

Posted 27 July 2012 - 02:30 AM

Hi guys, trying to figure something out. On my site, I use JQuery to display a calandar (datepicker). Problem is, I want to use a drop down calandar for mobile phones. So I pretty much have my form do this
<input type="text" name="date" id="date" class="datepicker" value="Date" />


Which calls up my javascript
$('#date').datepicker({
...


Now in order to change the calandar for mobiles, I have started off by changing the above to
if (screen.width <= 768) {

}else{

	// load datepickers
	$('#date').datepicker({
        ...
        });
}


The problem is, the code I need to use in the if statement to load the dropdown calandar is php
<select name="date_day">
<?php
	$i=1;
	while($i < 32){
		echo "<option>".$i."</option>";
		$i++;
	}
?>
...


So how would I go about adding this to the javascript? Is it ok to just place it in there?

Cheers

This post has been edited by nick2price: 27 July 2012 - 02:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: php inside javascript

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2898
  • View blog
  • Posts: 7,553
  • Joined: 08-June 10

Re: php inside javascript

Posted 27 July 2012 - 02:34 AM

PHP is always executed before JS (is even loaded). if that’s OK, you can do it this way. otherwise you have to load the PHP result in the background via AJAX.

but for the given case, JS can do that as well. no PHP required.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,526
  • Joined: 23-August 08

Re: php inside javascript

Posted 27 July 2012 - 03:33 AM

Let's clear up (what appears to be) a misconception here first.

PHP (or the web page) is rendering this in the HTML:

<input type="text" name="date" id="date" class="datepicker" value="Date" />


which is sent to the browser. Assuming this call

$('#date').datepicker({
...



is in the "when DOM has finished loading" function in Javascript, Javascript looks for the element in the DOM with the id of "date" and calls the "constructor" of datepicker object, which makes the changes to the HTML to render the object as desired and add event handlers for its functionality.

What are you doing with this code, exactly?
<select name="date_day">
<?php
	$i=1;
	while($i < 32){
		echo "<option>".$i."</option>";
		$i++;
	}
?>
...


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1