0

The following is one file index.php GetWidth and GetHeight functions work. (They are not final functions so not important.) The important part is marked by /* IMPORTANT HERE */ What I am trying to accomplish is pass the variable I get using jQuery Window Portal Height / Width and pass it to a PHP script here In other words when I get value from GetWidth() it should go to w=120 or replace 120 value with whatever the width is at the moment and the same with height. If there is any syntax errors please ignore.

I only want to figure out how to pass value from jQuery to PHP scrip. Also in the future I want add on resize method so values will be dynamic when window is resized.

<html>
    <head>
        <title></title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script>
            $(window).resize(function () {
      $('#msg').text('Browser (Width : ' 
                         + $(window).width() 
         + ' , Height :' + $(window).height() + ' )');
        });

        $(document).ready(function() {
       // DOCUMENT READY

       function GetWidth() {
            if (self.innerWidth) {
                    return self.innerWidth;
                }
                else if (document.documentElement && 
                             document.documentElement.clientHeight) {
                    return document.documentElement.clientWidth;
                }
                else if (document.body) {
                    return 0;
                }
                return x;
            }

            function GetHeight() {
                if (self.innerHeight) {
                    y = self.innerHeight;
                }
                else if (document.documentElement && 
                           document.documentElement.clientHeight) {
                    return
                                document.documentElement.clientHeight;
                }
                else if (document.body) {
                    return 0;
                }
                return y;
            }





// This is for when it first loads.                                
$('#TEXT').text('W: ' + $(window).width() + ' , H:' + $(window).height() + y);

// This is for when window gets resized.
$(window).resize(function () {
    $('#TEXT').text('W: ' + $(window).width() + ' , H:' + $(window).height());
});

  // DOCUMENT READY    
  });
 </script>
 </head>

 <body>

Info Area:<br>
<div id="TEXT"></div>

/* IMPORTANT HERE */
<!-- img src="timthumb.php?src=URL...image.jpg&h=180&w=120" -->
/* IMPORTANT HERE */

</body>
</html>
Nick
  • 1,417
  • 1
  • 14
  • 21
  • This has been answered a million times. Try searching in the upper right corner or check out the "related" section to the right of this questions. – jeremyharris Jan 08 '13 at 18:57

3 Answers3

0

You can not pass the jQuery or JS variables to the PHP script. Basically the PHP scripts get executed at server level and only HTML, JS and CSS will be returned to the browser. Then browser will render it. So when browser executing your JS method, the PHP scripts are already executed.

you can use Ajax.

Sree
  • 921
  • 2
  • 12
  • 31
0

You can not pass values from jQuery to PHP becuase PHP is a server-side language and jQuery is a client-side language. But there is a way to pass data from client-side to server-side which is AJAX

https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming

Why PHP script is not workig in a web browser?

http://www.scriptingok.com/tutorial/Server-side-vs-Client-side

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243
0

wellcome to stackoverflow!
the problem is that the php is compiling in server and browser receive a html and javascript file from server
then your browser compiling your javascript and html code
so javascript unable to change phpcode
you can make your html whit javascript like this:

image = new Image(); 
image.src = "timthumb.php?src=URL...image.jpg&h="+ hgt + "&w=" + wth;

image.onLoad=function(){ $("img").attr({src: image.src}); }
mrash
  • 883
  • 7
  • 18