| | |
NEED help to pass javascript variable to php
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2008
Posts: 17
Reputation:
Solved Threads: 2
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
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.
The code I'm working around is like
<script> var screen_width=screen.width </script>
<?php $screen_width=javascript_variable ?>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.
Last edited by flashyflashy; May 8th, 2008 at 11:00 am.
•
•
Join Date: May 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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[]).
php Syntax (Toggle Plain Text)
<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>
php Syntax (Toggle Plain Text)
//But this is possible <?php $value = 100; ?> <script type='text/javascript'> var xyz; xyz = <?php echo $value; ?>; alert (xyz); </script>
Last edited by nav33n; May 8th, 2008 at 11:40 am.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Oct 2006
Posts: 30
Reputation:
Solved Threads: 3
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:
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?
. 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: PHP Syntax (Toggle Plain Text)
<SCRIPT LANGUAGE="JavaScript" type="text/javascript"> <!-- // var number=100; document.write('<a href="another_page.php?number='+number+'">another page</a>'); // --> </SCRIPT>
I don't reply to private messages.
•
•
Join Date: May 2008
Posts: 17
Reputation:
Solved Threads: 2
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.
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.
Last edited by flashyflashy; May 8th, 2008 at 12:33 pm.
•
•
Join Date: Oct 2006
Posts: 30
Reputation:
Solved Threads: 3
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:
Normally AJAX is used in situations like these. But you should master javascript before learning AJAX. Because AJAX is based on javascript.
Here is an example:
PHP Syntax (Toggle Plain Text)
<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.
Last edited by Rayhan Muktader; May 8th, 2008 at 2:52 pm.
I don't reply to private messages.
•
•
Join Date: Oct 2006
Posts: 30
Reputation:
Solved Threads: 3
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)
I don't reply to private messages.
![]() |
Similar Threads
- pass the javascript variable to php page (PHP)
- Pass Javascript variable to PHP code (PHP)
- Pass a javascript variable to PHP in the same function (JavaScript / DHTML / AJAX)
Other Threads in the PHP Forum
- Previous Thread: passing php variables using post
- Next Thread: php pdf - converting to png/jpg/gif/tif
| Thread Tools | Search this Thread |
5.2.10 action apache api array beginner beneath binary broken cakephp checkbox class classes cms code cron curl database date destroy display dynamic echo echo$_get[x]changingitintovariable... email encode error fcc file files folder form forms function functions google header howtowriteathesis href htaccess html image images include insert ip javascript joomla limit link local login mail memberships menu mlm mod_rewrite multiple multipletables mysql mysqlquery neutrality oop open passwords paypal pdf php provider query radio random record remote rss script search server sessions sockets source space sql strip_tags syntax system table template thesishelp tutorial update upload url validator variable video voteup web window.onbeforeunload=closeme; youtube






