Jump to content

Javascript/PHP - PHP doesn't want to recognise it? Or am I wrong?


Recommended Posts

Ok so basically what i've done, is find the screen size using javascript, assigned it to a variable, and this must work because when i echo it it shows the proper size on the page.

 

I also did this with whether they have flash 8 or later, or not. should set the var to 1 if so, or 0 if not. Again, when i echo it, it shows the right number, but when i ask php if its 1, it doesnt work?

 

Heres the codes (Not full, but the codes appropriate)...

 

//$has_flash = 1 or 0
  $has_flash = '
    <script type="text/javascript">     
      if (!FlashDetect.versionAtLeast() {
        // Dont have flash 8 or later
        document.write(0);        
      } else {
        document.write(1);
      }
    </script>
  ';

 

Heres the code to echo it (works apparently)

<p>has flash: ' . $has_flash . '</p>

 

But HERES THE PROBLEM. When I do the following, it never works out.

 

if ($has_flash == 1) {
    $menu = '
      HAI U HAV FLASH
    '; 
  } else {
    $menu = 'U DONT HAV FLASH< I R SAD';
  }
  echo $menu;

It always goes for the else statement. WHY?

 

ALL help appreciated. Thanks alot :)

Because Javascript is client side and php is server side. You cannot set a server side variable (php) to the value of a client side result using javascript (not without using ajax). All you are doing is assigning a string to $has_flash containing javascript code. if you echo it out it will run the javascript in the client browser.

A way to check whether they have flash 8 or later (which evidently can be done in javascript), but then i need to store the values in a serverside variable, so I can use them in php like you saw in the first post. I'm not sure how i'd go about doing this, especially if it needs ajax, because 'm VERY new to ajax too.

Hm. I suppose it could be. Would I just do this..

 

$flashtest = '
    <script type="text/javascript">     
      if (!FlashDetect.versionAtLeast() {
        // Dont have flash 8 or later
        document.write("U need flash 8");       
      } else {
        document.write("U have flash 8");
      }
    </script>
  ';

  echo $flashtest;

 

?

I looked at the exampe and it was pretty much what I put, yet mine doesnt echo? any idea why?

 

$menu = '
    <script type="text/javascript"> 
              if (!FlashDetect.versionAtLeast() {
                // Dont have flash 8 or later
                var content = \'
                  <b><font color=#FF0000>
                  Install flash 8 or later!
                  </font></b>
                \';
                document.write(content);        
              } else {
                var content = \'
                        <p>Hi, you have flash 8 or later.</p>
                \';
                document.write(content);
              }
            </script>
  ';  

 

echo '
    <body>
      <noscript class="ERROR">Please enable JavaScript</noscript>
      <div id="main">
          ' . $menu . '	        
      </div>
    </body>
    ';

 

It doesnt echo though. Argh, please help xD

Jquery will help you more here:

 

1. download jquery

2. add the <script> tags

3. $(document).ready(function() {

      if (!FlashDetect.versionAtLeast(8)) {

        // Dont have flash 8 or later

      $('#main').html("don't have flash 8");   

      } else {

        $('#main').html("you have flash 8");   

      }

});

 

and you are done!!

try

 

$menu = '<script type="text/javascript">

              if (!FlashDetect.versionAtLeast(8)) {

                // Dont have flash 8 or later

                var content = "<b><font color=#FF0000>Install flash 8 or later!</font></b>";

                document.write(content);     

              } else {

                var content = "<p>Hi, you have flash 8 or later.</p>";

                document.write(content);

              }

            </script>';

Kk please help me out here, i'm stressin lol.

Heres my actual code, it won't fuckin' echo anything

 

$menu = '<script type="text/javascript">
              if (!FlashDetect.versionAtLeast() {
                // Dont have flash 8 or later
                var content = "<b><font color=#FF0000>HOME, TUTORIALS<br />
                  Install flash 8 or later to view the proper navigation toolbar!
                  </font></b>";
                document.write(content);       
              } else {
                var content = "<ul id=\"menu\" class=\"menu\">
                    <li>
                      <a href=\"/#\">
                        <img src=\"images/home-small.png\" class=\"small\" alt=\"HOME\"/>
    		                  <img src=\"images/home.png\" class=\"big\" alt=\"HOME\"/>
	                  <span class=\"title\">Home</span>
                      </a>
                    </li>
                    <li>
                      <a href=\"javascript:alert()\">
	                  <img src=\"images/tutorials-small.png\" class=\"small\" alt=\"TUTORIALS\"/>
	                  <img src=\"images/tutorials.png\" class=\"big\" alt=\"TUTORIALS\"/>
	                  <span class=\"title\">Tutorials</span>
                      </a>
                    </li>
                        </ul>";
                document.write(content);
              }
            </script>';

 

...

 

echo '
    <body>
      <noscript class="ERROR">Please enable JavaScript</noscript>
      <div id="main">
            ' . $menu . '	
      </div> 
    </body>
    ';

 

HELP!

Again. PHP executes on the server while Javascript executes on the client. Hence, assigning a php variable to the output of some Javascript will not work.

 

If you really need this data available to you server side you will need to use Ajax and have Javascript send this data back to the server once it has executed.

Again. PHP executes on the server while Javascript executes on the client. Hence, assigning a php variable to the output of some Javascript will not work.

 

If you really need this data available to you server side you will need to use Ajax and have Javascript send this data back to the server once it has executed.

 

Therefore my script should execute the javascript script when it echos the string that contains the script, and then javascript should echo (document.write()) the infomation i wanted. This is not working , because with my script it just doesnt echo anything, but with the script that priti posted, it echos.

 

Please explain what's wrong with my code.

 

$menu = '<script type="text/javascript">
              if (!FlashDetect.versionAtLeast() {
                // Dont have flash 8 or later
                var content = "<b><font color=#FF0000>HOME, TUTORIALS<br />
                  Install flash 8 or later to view the proper navigation toolbar!
                  </font></b>";
                document.write(content);       
              } else {
                var content = "<ul id=\"menu\" class=\"menu\">
                    <li>
                      <a href=\"/#\">
                        <img src=\"images/home-small.png\" class=\"small\" alt=\"HOME\"/>
    		                  <img src=\"images/home.png\" class=\"big\" alt=\"HOME\"/>
	                  <span class=\"title\">Home</span>
                      </a>
                    </li>
                    <li>
                      <a href=\"javascript:alert()\">
	                  <img src=\"images/tutorials-small.png\" class=\"small\" alt=\"TUTORIALS\"/>
	                  <img src=\"images/tutorials.png\" class=\"big\" alt=\"TUTORIALS\"/>
	                  <span class=\"title\">Tutorials</span>
                      </a>
                    </li>
                        </ul>";
                document.write(content);
              }
            </script>';

 

...

 

echo '
    <body>
      <noscript class="ERROR">Please enable JavaScript</noscript>
      <div id="main">
            ' . $menu . '	
      </div> 
    </body>
    ';

<div id="main">
            <script type="text/javascript">
              if (!FlashDetect.versionAtLeast() {
                // Dont have flash 8 or later
                var content = "<b><font color=#FF0000>HOME, TUTORIALS<br />
                  Install flash 8 or later to view the proper navigation toolbar!
                  </font></b>";
                document.write(content);       
              } else {
                var content = "<ul id=\"menu\" class=\"menu\">
                    <li>
                      <a href=\"/#\">
                        <img src=\"images/home-small.png\" class=\"small\" alt=\"HOME\"/>
    		                  <img src=\"images/home.png\" class=\"big\" alt=\"HOME\"/>
	                  <span class=\"title\">Home</span>
                      </a>
                    </li>
                    <li>
                      <a href=\"javascript:alert()\">
	                  <img src=\"images/tutorials-small.png\" class=\"small\" alt=\"TUTORIALS\"/>
	                  <img src=\"images/tutorials.png\" class=\"big\" alt=\"TUTORIALS\"/>
	                  <span class=\"title\">Tutorials</span>
                      </a>
                    </li>
                        </ul>";
                document.write(content);
              }
            </script>	        
    </div>

 

This would all be fine, but it still doesnt actually echo anything on the page.

Well the code posted by priti earlier works, using the same flash detection function, so I cant see how that'd make it not work.

 

And I used the function, still nothin'...

 

<script type="text/javascript">
      (function() {
              if (!FlashDetect.versionAtLeast() {
                // Dont have flash 8 or later
                var content = "<b><font color=#FF0000>HOME, TUTORIALS<br />
                  Install flash 8 or later to view the proper navigation toolbar!
                  </font></b>";
                document.write(content);       
              } else {
                var content = "<ul id=\"menu\" class=\"menu\">
                    <li>
                      <a href=\"/#\">
                        <img src=\"images/home-small.png\" class=\"small\" alt=\"HOME\"/>
    		                  <img src=\"images/home.png\" class=\"big\" alt=\"HOME\"/>
	                  <span class=\"title\">Home</span>
                      </a>
                    </li>
                    <li>
                      <a href=\"javascript:alert()\">
	                  <img src=\"images/tutorials-small.png\" class=\"small\" alt=\"TUTORIALS\"/>
	                  <img src=\"images/tutorials.png\" class=\"big\" alt=\"TUTORIALS\"/>
	                  <span class=\"title\">Tutorials</span>
                      </a>
                    </li>
                        </ul>";
                document.write(content);
              }
            }) ()
            </script>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.