Calling java script in PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #16
    Originally posted by dheerajjoshim
    yeah Mark..

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

    Regards
    Dheeraj Joshi
    Post the new code (using [code] tags).

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #17
      take it out of the switch(), currently that’s preventing the print.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #18
        Originally posted by Dormilich
        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)
        Actually, back-ticks, like in other languages, do run the contents as a shell command.

        Code:
        printf("PHP is located at %s", `which php`);
        // equivalent of doing
        printf("PHP is located at %s", shell_exec('which php'));

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #19
          Originally posted by Markus
          Actually, back-ticks, like in other languages, do run the contents as a shell command.
          interesting, need to make a (mental) note of that.

          Comment

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

            #20
            Ok.. Markus... Then what does this return?

            Code:
            `php myfile.php`

            A string or ............... ..?


            Regards
            Dheeraj Joshi

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #21
              that depends on what’s inside myfile.php

              Comment

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

                #22
                You mean return type right?

                Regards
                Dheeraj Joshi

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #23
                  Originally posted by dheerajjoshim
                  You mean return type right?
                  yepp, though I don’t know if that’s return or echo. didn’t work with the CLI version of PHP so far.

                  Comment

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

                    #24
                    Generally languages have a tendency to print not to return unless we say it so.

                    Regards
                    Dheeraj Joshi

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #25
                      It'll return a string, I imagine. Do a var_dump() on your $res variable.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #26
                        Originally posted by Markus
                        Do a var_dump() on your $res variable.
                        general advice: this is one of the things to be done first.

                        Comment

                        • gopan
                          New Member
                          • Apr 2009
                          • 41

                          #27
                          Dear dheerajjoshim,
                          If you're getting $res as 1 then look at the code line#9
                          Code:
                          case 1: "<scriptlanguage=javascript>
                          and change it to

                          Code:
                          case 1: "<script language=\"javascript\">

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #28
                            Originally posted by gopan
                            Dear dheerajjoshim,
                            If you're getting $res as 1 then look at the code line#9
                            Code:
                            case 1: "<scriptlanguage=javascript>
                            and change it to

                            Code:
                            case 1: "<script language=\"javascript\">
                            As mentioned before, that won't work because you're not asking PHP to do anything with the string, it's the same as
                            Code:
                            <?php
                            "hi, my name is Mark";
                            which obviously will not output anything.

                            Comment

                            • gopan
                              New Member
                              • Apr 2009
                              • 41

                              #29
                              Originally posted by Markus
                              As mentioned before, that won't work because you're not asking PHP to do anything with the string, it's the same as
                              Code:
                              <?php
                              "hi, my name is Mark";
                              which obviously will not output anything.
                              yaa my bad Mark...

                              he need to add an echo before the quotes... I din't see that...

                              The corrected code will be...
                              Code:
                              <html>
                              <body>
                              <?php
                               
                              $res = `php myfile.php`;
                               
                              switch($res)
                              {
                                  case 1: 
                                     echo "<script language=\"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>
                              and if $res is holding value one... the javascript will alert "blah blah"

                              Comment

                              • Jezternz
                                New Member
                                • Jan 2008
                                • 145

                                #30
                                didn't I say that above :(

                                Comment

                                Working...