Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Variables in Javascript URL

Posted on 2012-12-23
7
449 Views
Last Modified: 2012-12-25
Hi, I'm new to Javascript so bear with me please.

I have a series of slides in Adobe Captivate. Each slide has a button on it. When the user clicks the button it should take them to a URL which is partially made up of today's date, e.g:

http://10.10.10.10/abcd/web/day.php?year=2012&month=11&day=01&area=12

I played with setting the variables and using window.open but cannot get it to work. As I said I'm new to Javascipt so any help would be appreciated, and merry Xmas!

Thanks
0
Comment
Question by:jonlake
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:wadehults
ID: 38717293
The problem you are probably having is that you are attempting to assign values from PHP variables to a JavaScript variable. JavaScript cannot retrieve PHP variables directly from the server. We are talking about both server-side and client-side scripting here. PHP operates on the server and JavaScript operates on the local machine.

Based on the URL example you provided, you are using a _GET method to submit form data to the next page using PHP.

What you will need to do is embed a PHP script within your JavaScript to dynamically generate the values needed for the JavaScript to work. See example below:
<?php
    $year = "2012";
    $month = "11";
    $day = "01";
    $area =  "12";
?>
...
<script language="javascript" type="text/javascript">
    var year = "<?php echo $year  ?>.";
    var month = "<?php echo $month ?>";
    var day = "<?php echo $day ?>;
    var area = "<?php echo $area ?>;
    
    
</script>
...

Open in new window


At this point you can use the variables generated by PHP within your JavaScript construct using the JavaScript variables. Notice the difference between the PHP variable and JavaScript syntax. PHP uses the $ symbol to denote a variable, but JavaScript does not require any prefix.
0
 

Author Comment

by:jonlake
ID: 38717299
I may not have explained myself very well. I need to create the three variables, year, month and day based upon whichever day it happens to be. Something like this:

var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();

Those variables then need to populate into the URL. Once I have the URL created isn't it a smple onclick event?
0
 

Author Comment

by:jonlake
ID: 38717347
I found this code which nicely opens a browser window. My though process is that there my be a way to pass the date variables into the URL:

var simmy = window.open("http://www.google.com", "simWindow","status=0,menuBar=0,location=0,toolBar=0,directories=0,resizable=0,scrollbars=0,height=620,width=980");
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:wadehults
ID: 38717390
You can develop a JavaScript function that will dynamically insert the variables and variable names into a URL. Example follows:

var url = "http://www.google.com?year="+year+"&month="+month+"&day="+day+"&area="+area;

The final markup would look something like this:
<script type="text/javascript">
var month = "12";
var day = "12";
var year = "2012";
var area = "02";	
var url = "http://www.google.com?year="+year+"&month="+month+"&day="+day+"&area="+area;

document.write("<a href="+url+">this link");
</script>

Open in new window


Does this help you move in the right direction?

Just in case you want to use PHP to generate your variable data, you can use
<?php
$sysdate = date("d,m,Y");
?>

Open in new window

This will return a variable value of DDMMYYYY format and it can be altered to include special characters in the return value.
0
 

Author Comment

by:jonlake
ID: 38720086
This has worked:

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
if(month<10){month='0'+month};
var day = currentTime.getDate("dd");
if(day<10){day='0'+day};

var year = currentTime.getFullYear();
var area = "02";


var simmy = window.open("http:"+"/"+"/"+"10.10.10.10"+"/"+"mrbs"+"/"+"web"+"/"+"day.php"+"%3F"+"year="+year+"%26"+"month="+month+"%26" +"day="+day+"%26"+"area="+area, "simWindow","status=0,menuBar=0,location=0,toolBar=0,directories=0,resizable=0,scrollbars=0,height=620,width=980");
0
 
LVL 3

Accepted Solution

by:
wadehults earned 500 total points
ID: 38720145
I am glad I could assist you in getting to the solution.
0
 

Author Closing Comment

by:jonlake
ID: 38720202
Thanks, you steered me in the right direction.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
key press alert 2 32
How to add a stringified JSON string to an IP address? 11 59
ASP.NET Content Page 3 28
Javascript 2 22
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question