NEED help to pass javascript variable to php

Reply

Join Date: May 2008
Posts: 17
Reputation: flashyflashy is an unknown quantity at this point 
Solved Threads: 2
flashyflashy flashyflashy is offline Offline
Newbie Poster

NEED help to pass javascript variable to php

 
0
  #1
May 8th, 2008
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=javascript_variable ?>
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.
Last edited by flashyflashy; May 8th, 2008 at 11:00 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,738
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: NEED help to pass javascript variable to php

 
0
  #2
May 8th, 2008
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.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 4
Reputation: mani8php is an unknown quantity at this point 
Solved Threads: 0
mani8php mani8php is offline Offline
Newbie Poster

Re: NEED help to pass javascript variable to php

 
0
  #3
May 8th, 2008
Originally Posted by nav33n View Post
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...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,738
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: NEED help to pass javascript variable to php

 
0
  #4
May 8th, 2008
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.
  1. <script type='text/javascript'>
  2. var xyz;
  3. xyz = 1000;
  4. <?php
  5. $value = xyz; // he wants xyz value of javascript to be assigned to php variable $value.
  6. ?>
  7. </script>
  1. //But this is possible
  2. <?php
  3. $value = 100;
  4. ?>
  5. <script type='text/javascript'>
  6. var xyz;
  7. xyz = <?php echo $value; ?>;
  8. alert (xyz);
  9. </script>
Last edited by nav33n; May 8th, 2008 at 11:40 am.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 30
Reputation: Rayhan Muktader is an unknown quantity at this point 
Solved Threads: 3
Rayhan Muktader Rayhan Muktader is offline Offline
Light Poster

Re: NEED help to pass javascript variable to php

 
0
  #5
May 8th, 2008
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:
  1. <SCRIPT LANGUAGE="JavaScript" type="text/javascript">
  2. <!-- //
  3. var number=100;
  4. document.write('<a href="another_page.php?number='+number+'">another page</a>');
  5. // -->
  6. </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?
I don't reply to private messages.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 17
Reputation: flashyflashy is an unknown quantity at this point 
Solved Threads: 2
flashyflashy flashyflashy is offline Offline
Newbie Poster

Re: NEED help to pass javascript variable to php

 
0
  #6
May 8th, 2008
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.
Last edited by flashyflashy; May 8th, 2008 at 12:33 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 17
Reputation: flashyflashy is an unknown quantity at this point 
Solved Threads: 2
flashyflashy flashyflashy is offline Offline
Newbie Poster

Re: NEED help to pass javascript variable to php

 
0
  #7
May 8th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 30
Reputation: Rayhan Muktader is an unknown quantity at this point 
Solved Threads: 3
Rayhan Muktader Rayhan Muktader is offline Offline
Light Poster

Re: NEED help to pass javascript variable to php

 
0
  #8
May 8th, 2008
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:
  1. <script type="text/javascript">
  2. var myvar= "hello world";
  3. location.href="http://www.google.com/search?q=" + myvar;
  4. </script>
  5.  
  6. OR you can do this:
  7. <script type="text/javascript">
  8. var myvar= "hello world";
  9. window.location ="http://www.google.com/search?q=" + myvar;
  10. </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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 17
Reputation: flashyflashy is an unknown quantity at this point 
Solved Threads: 2
flashyflashy flashyflashy is offline Offline
Newbie Poster

Re: NEED help to pass javascript variable to php

 
0
  #9
May 8th, 2008
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.
Last edited by flashyflashy; May 8th, 2008 at 4:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 30
Reputation: Rayhan Muktader is an unknown quantity at this point 
Solved Threads: 3
Rayhan Muktader Rayhan Muktader is offline Offline
Light Poster

Re: NEED help to pass javascript variable to php

 
0
  #10
May 8th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC