8 Replies - 2409 Views - Last Post: 21 November 2010 - 07:16 PM Rate Topic: -----

#1 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

PHP Variable into JavaScript

Posted 21 November 2010 - 01:49 AM

Hope it's okay that I post this here, but I wanted to try it here first instead of the PHP forum. I am using SWFUpload as an interactive upload control for a FTP site I'm designing. I have absolutely 0 experience in java so needless to say I've been struggling the whole way. I've almost got it all figured out(the SWFUpload) except for one tiny detail, a File Upload Limit, I have the limit from a database variable in PHP. I can display it all day using PHP, but I cannot echo the variable into the javascript, this is what I have tried :

NOTE LINE 23

if($_SESSION['package']=="3"){
	$quota="2147483648";
	$new = $quota - $_SESSION['used'];
	echo " <br><br><br>NEW:" . $new . "<br><br>";
	echo  "USED:" . $_SESSION['used'] . "<br><br>";
	$maxupload=round($new/1024,1);	
	echo "MAX UPLOADX:" . $_SESSION['maxupload'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiple File Upload With Progress Bar - Web Developer Plus Demos</title>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="js/jquery.swfupload.js"></script>
<script type="text/javascript">
$(function(){
	$('#swfupload-control').swfupload({
		upload_url: "upload-file.php",
		file_post_name: 'uploadfile',
		file_size_limit : <?php $_SESSION['maxupload']; ?>,
		file_types : "*.*",
		file_types_description : "Image files",
		file_upload_limit : 5,
		flash_url : "js/swfupload/swfupload.swf",
		button_image_url : 'js/swfupload/wdp_buttons_upload_114x29.png',
		button_width : 114,
		button_height : 29,
		button_placeholder : $('#button')[0],
		debug: false
	})



And..
if($_SESSION['package']=="3"){
	$quota="2147483648";
	$new = $quota - $_SESSION['used'];
	echo " <br><br><br>NEW:" . $new . "<br><br>";
	echo  "USED:" . $_SESSION['used'] . "<br><br>";
	$maxupload=round($new/1024,1);	
	echo "MAX UPLOADX:" . $_SESSION['maxupload'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiple File Upload With Progress Bar - Web Developer Plus Demos</title>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="js/jquery.swfupload.js"></script>
<script type="text/javascript">
$(function(){
	$('#swfupload-control').swfupload({
		upload_url: "upload-file.php",
		file_post_name: 'uploadfile',
		file_size_limit : <?php session_start(); $_SESSION['maxupload']; ?>,
		file_types : "*.*",
		file_types_description : "Image files",
		file_upload_limit : 5,
		flash_url : "js/swfupload/swfupload.swf",
		button_image_url : 'js/swfupload/wdp_buttons_upload_114x29.png',
		button_width : 114,
		button_height : 29,
		button_placeholder : $('#button')[0],
		debug: false
	})



Eh, And..

[code]
if($_SESSION['package']=="3"){
	$quota="2147483648";
	$new = $quota - $_SESSION['used'];
	echo " <br><br><br>NEW:" . $new . "<br><br>";
	echo  "USED:" . $_SESSION['used'] . "<br><br>";
	$maxupload=round($new/1024,1);	
	echo "MAX UPLOADX:" . $_SESSION['maxupload'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiple File Upload With Progress Bar - Web Developer Plus Demos</title>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="js/jquery.swfupload.js"></script>
<script type="text/javascript">
$(function(){
	$('#swfupload-control').swfupload({
		upload_url: "upload-file.php",
		file_post_name: 'uploadfile',
		file_size_limit : "<?php session_start(); $_SESSION['maxupload']; ?>",
		file_types : "*.*",
		file_types_description : "Image files",
		file_upload_limit : 5,
		flash_url : "js/swfupload/swfupload.swf",
		button_image_url : 'js/swfupload/wdp_buttons_upload_114x29.png',
		button_width : 114,
		button_height : 29,
		button_placeholder : $('#button')[0],
		debug: false
	})



And lastly,


[code]
if($_SESSION['package']=="3"){
	$quota="2147483648";
	$new = $quota - $_SESSION['used'];
	echo " <br><br><br>NEW:" . $new . "<br><br>";
	echo  "USED:" . $_SESSION['used'] . "<br><br>";
	$maxupload=round($new/1024,1);	
	echo "MAX UPLOADX:" . $_SESSION['maxupload'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiple File Upload With Progress Bar - Web Developer Plus Demos</title>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="js/jquery.swfupload.js"></script>
<script type="text/javascript">
$(function(){
	$('#swfupload-control').swfupload({
		upload_url: "upload-file.php",
		file_post_name: 'uploadfile',
		file_size_limit : '<?php session_start(); $_SESSION['maxupload']; ?>',
		file_types : "*.*",
		file_types_description : "Image files",
		file_upload_limit : 5,
		flash_url : "js/swfupload/swfupload.swf",
		button_image_url : 'js/swfupload/wdp_buttons_upload_114x29.png',
		button_width : 114,
		button_height : 29,
		button_placeholder : $('#button')[0],
		debug: false
	})



I've googled this already, and found several posts with information on how to do it. But most of them described ways like the ones I have tried. What can some of you java experts recommend for such a small problem ?

This post has been edited by kinggeorge1987: 21 November 2010 - 01:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: PHP Variable into JavaScript

#2 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

Re: PHP Variable into JavaScript

Posted 21 November 2010 - 02:18 AM

SOLVED:

session_start();
if($_SESSION['package']=="3"){
	$quota="2147483648";
	$new = $quota - $_SESSION['used'];
	echo " <br><br><br>NEW:" . $new . "<br><br>";
	echo  "USED:" . $_SESSION['used'] . "<br><br>";
	$maxupload=round($new/1024,1);	
	echo "MAX UPLOADX:" . $_SESSION['maxupload'];

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiple File Upload With Progress Bar - Web Developer Plus Demos</title>
<script type="text/javascript" src="js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="js/jquery.swfupload.js"></script>
<script type="text/javascript">
<?php
session_start();
$myVar=$_SESSION['maxupload'];
echo 'var temp = '.$myVar.';';
?>
$(function(){
	$('#swfupload-control').swfupload({
		upload_url: "upload-file.php",
		file_post_name: 'uploadfile',
		file_size_limit : temp,
		file_types : "*.*",
		file_types_description : "Image files",
		file_upload_limit : 5,
		flash_url : "js/swfupload/swfupload.swf",
		button_image_url : 'js/swfupload/wdp_buttons_upload_114x29.png',
		button_width : 114,
		button_height : 29,
		button_placeholder : $('#button')[0],
		debug: false
	})


Just defined a variable with java syntax in PHP, then used the variable in Java. Pretty simple, thought Id share just in case anyone else comes up with a similar problem before this thread gets lost with millions of others.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6107
  • View blog
  • Posts: 23,659
  • Joined: 23-August 08

Re: PHP Variable into JavaScript

Posted 21 November 2010 - 06:51 AM

Dude you're talking about Javascript, not Java, right? Certainly looks that way. Those are two totally different beasts. This really belongs in PHP, so I'll move it there.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: PHP Variable into JavaScript

Posted 21 November 2010 - 07:10 AM

Sure, it works, but you've got three different languages in 40 lines of code! Throw in some mysql, along with a little in-line css and jquery and you'll have just about everything you could stuff into one file! If/when your code gets longer, say about 100 lines, you're going to find it exponentially more difficult to manage. You need to separate your languages.
Was This Post Helpful? 0
  • +
  • -

#5 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

Re: PHP Variable into JavaScript

Posted 21 November 2010 - 12:21 PM

Yea, I was talking about Javascript. Sorry, didn't know there was a difference.

@CTphpnwb

I appreciate the advice. Dont know if it's really necessary or not, but that seems handy for a lot of folks. The page really isn't that complex, and thats the only instance of mixing languages that I have, so I probably wont go through the trouble of taking it out and putting into text files and reading them in in specific places when it works just fine the way it is and I dont have to make anymore changes.

Thanks guys.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: PHP Variable into JavaScript

Posted 21 November 2010 - 12:53 PM

The question is, will you still recognize what you were trying to do in six months if you need to make a change?
Was This Post Helpful? 0
  • +
  • -

#7 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

Re: PHP Variable into JavaScript

Posted 21 November 2010 - 06:43 PM

Great point. Before I say this let me state that I am not arguing with you, you are 100% right in saying that splitting the languages is much more efficient and I will be doing it from now on, but to answer your question - I believe I would. People like you( and me but to a much smaller extent) recognize problems everyday by simply reading code we have never seen before. Id like to think that in 6 months I can go back to that page and see that I echoed a variable in java syntax using PHP, if the problem ever came up.

This post has been edited by kinggeorge1987: 21 November 2010 - 06:45 PM

Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3099
  • View blog
  • Posts: 10,887
  • Joined: 08-August 08

Re: PHP Variable into JavaScript

Posted 21 November 2010 - 07:08 PM

Well, I can recognize problems by reading code I've never seen before, but it's much easier if the code is well organized and not mixed with other languages. As you get better at this you'll see that the hardest part of writing code is keeping it well organized so that when you come back to it later you don't feel lost in it.

:^: Your code isn't bad by compared to many others you'll see posted here, so you've demonstrated some ability. I'm just suggesting that you take the next step.
Was This Post Helpful? 0
  • +
  • -

#9 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

Re: PHP Variable into JavaScript

Posted 21 November 2010 - 07:16 PM

Point well taken, and thanks for the compliment. I try and pay attention, I couldn't count the times I've seen you and some of the moderators express to the new members to organize and indent their code so it's easier to follow. So I've tried to practice that. I'll give this code separation a try and just for example sake (I guess) I'll post it in this thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1