Calling java script in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    Calling java script in PHP

    I have this code.


    Code:
    <html>
    <body>
    <?php
    
    $res = `php myfile.php`;
    
    switch($res)
    {
    	case 1: "<scriptlanguage=javascript> 
    		alert('Blah Blah')
    		</script>";
    		break;
    	case 2: echo "Choice 2";
    		break;
    	case 3: echo "Choice 3";
    		break;
    	default:echo "No choice";
    		break;
    }
    
    ?>
    </body>
    </html>
    Technically it should pop up a window of alert.

    But it wont.
    Code:
    echo $res;
    this will give value 1.

    Whats wrong with it.?

    Regards
    Dheeraj Joshi
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by dheerajjoshim
    Technically it should pop up a window of alert.
    No, it should give "No choice". if you do var_dump($res); you’ll see why. besides, even if $res would be a string it still would match the default chioce.

    Originally posted by dheerajjoshim
    this will give value 1.
    how do you come to that conclusion?

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      $res is integer by the way.

      So isnt there a technique to pop up a window in PHP code?

      Regards
      Dheeraj Joshi

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by dheerajjoshim
        Code:
        $res = `php myfile.php`;
        how is that an integer?

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          Its a string...

          I am sorry..

          So can i use java script in PHP?

          Regards
          Dheeraj Joshi

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by dheerajjoshim
            Its a string...
            it ain’t even a string, it’s NULL because you assign a set of invalid characters. (strings are wrapped in ' (single quotation mark or apostrophe) or " (double quotation mark) not backticks (these are used in SQL).

            but of course you can use PHP to print JS code…

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              Yeah they are backticks

              Code:
              $res=`php myfile.php`
              it executes file named myfile.php and gives the result back.

              Regards
              Dheeraj Joshi

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by dheerajjoshim
                Code:
                $res=`php myfile.php`
                it executes file named myfile.php and gives the result back.
                actually… no. if you want to ‘execute’ a file you need to load/include it. and that doesn’t return a value…
                Code:
                // 4 different ways to include a file
                include "file.php";
                include_once "file.php";
                require "file.php";
                require_once "file.php";
                and backticks are not valid string delimiters.

                the only constructs able to return a value are variables, constants, functions, properties and methods (the latter 2 being special cases from to objects)

                Comment

                • Dheeraj Joshi
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1129

                  #9
                  You mean,

                  Code:
                  include("myfile.php");
                  
                  $res=myfile::myfun();
                  So this basically must hold the return value of function myfun() in myfile.

                  Regards
                  Dheeraj Joshi

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    Originally posted by dheerajjoshim
                    So this basically must hold the return value of function myfun() in myfile.
                    let’s say that myfun() must return a value.

                    so the code now looks sensible.

                    Comment

                    • Dheeraj Joshi
                      Recognized Expert Top Contributor
                      • Jul 2009
                      • 1129

                      #11
                      Ok.. Thanks..

                      Back to my java script problem?

                      How i have embedded the java script, is it correct?

                      Regards
                      Dheeraj Joshi

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        if you don’t need a condition, just echo it out.

                        Comment

                        • Markus
                          Recognized Expert Expert
                          • Jun 2007
                          • 6092

                          #13
                          Originally posted by dheerajjoshim
                          Ok.. Thanks..

                          Back to my java script problem?

                          How i have embedded the java script, is it correct?

                          Regards
                          Dheeraj Joshi
                          You're missing the echo/print call in the case.

                          Comment

                          • Jezternz
                            New Member
                            • Jan 2008
                            • 145

                            #14
                            I have either missed something obvious, or the javascript tag is completly wrong.
                            Code:
                            "<scriptlanguage=javascript> 
                                    alert('Blah Blah')
                            </script>";
                            Should be:
                            Code:
                            echo "<script type=\"text/javascript\">
                                    alert('Blah Blah')
                            </script>";

                            Comment

                            • Dheeraj Joshi
                              Recognized Expert Top Contributor
                              • Jul 2009
                              • 1129

                              #15
                              yeah Mark..

                              I a gave that echo still it do not give anything.

                              Regards
                              Dheeraj Joshi

                              Comment

                              Working...