I'm in a situation where I have to pass javascript variable to php without redirect (no $_get[]).

The code I'm working around is like

<script> var screen_width=screen.width </script>
<?php $screen_width=[B]javascript_variable[/B] ?>

I want to pass value of this variable to a new php variable.Please suggest me a way I could do the this.

I have tried passing javascript variable to form and thus printing it on screen,but I'm need of converting a javascript variable to a php variable.

I appreciate you for your time.

AFAIK, you can do it the other way, ie., assigning a php variable value to a javascript variable. Assigning a javascript variable value to a php variable is not possible since php is a server side scripting language.

AFAIK, you can do it the other way, ie., assigning a php variable value to a javascript variable. Assigning a javascript variable value to a php variable is not possible since php is a server side scripting language.

document.formname.action='one.php?a='+javascriptvariable;
document.formname.submit();


Otherwise use the hidden field and pass the value to it and get it from,the php using Post method...

I'm in a situation where I have to pass javascript variable to php without redirect (no $_get[]).

OP said he don't want to use $_GET[] ! I think he wants something like this.

<script type='text/javascript'>
var xyz;
xyz = 1000;
<?php 
$value = xyz; // he wants xyz value of javascript to be assigned to php variable  $value. 
?>
</script>
//But this is possible
<?php
$value = 100;
?>
<script type='text/javascript'>
var xyz;
xyz = <?php echo $value; ?>;
alert (xyz);
</script>

Not going to happen:'( . Javascript and PHP are two separate languages and they are not aware of each other's existence. However, javascript can work with php variables for the following reason. PHP code executes on the server and sends only the html to the browser. The client browser never sees any php code, all the php variables get turned into html before they reach the browser. So, as far as javascript is concerned it is only manipulating html. However, javascript code gets downloaded to the clients computer and gets executed by the client's browser. PHP has no way of knowing what javascript is doing because it stays on the on sever. The only way you can get php to do anything for you at this point would be to make another page request to the server like so:

<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
<!-- //
var number=100;
document.write('<a href="another_page.php?number='+number+'">another page</a>');
// -->
</SCRIPT>

Or you could do a javascript page redirect using the same url if you want. So the bottom line is that there is no easy way to do this. If you must have javascript pass the values to php and have result displayed on the same page you have to use AJAX. But do you really want to do that?

Thanks for replying guys.
nav33n you said it right, but I want to pass variable from javascript to php .

Rayhan could you please explain briefly How I could implement your trick. I assume you were trying to pass it to url and then use it in another page through $_get[].

I think this can be done by submitting and refreshing the page before visitors' could see it.I'm not clear how to do it with AJAx. I'm new to javascript.

How can use AJAX to do it.? I tried to pass javascript variable to php using forms by submitting the form using onload in body <body onload="document.fomname.submit() > But I had problem probably due to my insufficient knowledge abt JS

There are many ways you can do this if it is OK to display the resulting html on a different page.
Here is an example:

<script type="text/javascript">
var myvar= "hello world";
location.href="http://www.google.com/search?q=" + myvar;
</script>

OR you can do this:
<script type="text/javascript">
var myvar= "hello world";
window.location ="http://www.google.com/search?q=" + myvar;
</script>

Normally AJAX is used in situations like these. But you should master javascript before learning AJAX. Because AJAX is based on javascript.

Thanks for reply guys,but that didn't solve my problem.

1# I want js variable to be passed to php to use as a php variable (not viceversa )
2# without redirecting
3# Is there any way to do this using ajax or cookies or post method.

I thought redirection was an option. You could reload the same page with the code I posted instead of loading another page. This wouldn't be 'redirection' from the user's point of view. You should check out w3school.com's AJAX tutorial if you really want to learn AJAX. You should be able to solve this problem by the time you get the seventh or eight page of of the tutorial. But I don't see a way for me to fit all that information in a single post. (I am assuming that you don't have any prior experience with AJAX)

Yes Rayhan I too think reloading the same page before it loads completely will work. I'm workin on it.Thanks for your help.I appreciate it.

I'm doing all this circus just to present different designs to visitors with different screen resolutions .Is there any other good idea one could think of.

Do not wast your time designing for different resolutions, it will wast your time. I learned this the hard way. Solutions to resolutions, design for a standard size that most people use. 800x600 is too old fashion no one even cares for those anymore they're way to small. Design for at least the 1024 X 768 design for width and let flow from top to bottom. People don't mind scrolling down, but side ways is a wast of time and anoying.

Or you could design an expanding layout based off of the content within the body.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.