PHP to JavaScript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ken

    PHP to JavaScript

    How do I send a variable, $validity from php and use it in JavaScript?

    Seem like PHP is written first, then html and JavaScript.

    Is it possible?

    Ken


  • Ian.H

    #2
    Re: PHP to JavaScript

    On Sat, 4 Sep 2004 21:16:41 -0500, "Ken" <kkrolski@wi.rr .com> wrote:
    [color=blue]
    >How do I send a variable, $validity from php and use it in JavaScript?
    >
    >Seem like PHP is written first, then html and JavaScript.
    >
    >Is it possible?
    >
    >Ken
    >[/color]


    They're 2 separate technologies. PHP is server-side and Javascript,
    client-side. One option maybe to write the JS with PHP:


    <?php
    $phpFoo = 'bar';

    echo '<script type="text/javascript">';
    echo " alert('$phpFoo' );";
    echo '</script>';
    ?>


    Haven't tried this personally, but I don't know of any other method that
    might work.


    HTH =)



    Regards,

    Ian

    --
    Ian.H
    digiServ Network
    London, UK

    Comment

    • steve

      #3
      Re: PHP to JavaScript

      "Ken" wrote:[color=blue]
      > How do I send a variable, $validity from php and use it in[/color]
      JavaScript?[color=blue]
      >
      > Seem like PHP is written first, then html and JavaScript.
      >
      > Is it possible?
      >
      > Ken[/color]

      Basically php is creating an html/javascript page, so you just use php
      to write out the javascript code based on the logic of your php code.
      Hope that answers.

      --
      http://www.dbForumz.com/ This article was posted by author's request
      Articles individually checked for conformance to usenet standards
      Topic URL: http://www.dbForumz.com/PHP-JavaScri...ict146575.html
      Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=490656

      Comment

      • kingofkolt

        #4
        Re: PHP to JavaScript

        "Ken" <kkrolski@wi.rr .com> wrote in message
        news:wFu_c.1032 4$nA6.5358@twis ter.rdc-kc.rr.com...[color=blue]
        > How do I send a variable, $validity from php and use it in JavaScript?
        >
        > Seem like PHP is written first, then html and JavaScript.
        >
        > Is it possible?
        >
        > Ken
        >
        >[/color]

        I guess that would depend on how you want to use it in JS. One way to send
        it:

        <?php
        $validity = 1;
        print <<<BLOCK
        <html><head>
        <script language="javas cript">
        function foo(validity) {
        if (validity) {
        // do something
        }
        }
        </script></head>

        <body onLoad="foo($va lidity)"></body></html>
        BLOCK;
        ?>

        A common way of sending a variable from JS back to PHP is through URL's. Ex:

        <script language="javas cript">
        document.write( "<img src='logstats.p hp?referer=" + referer + "'>");
        </script>

        HTH

        - JP


        Comment

        • Ken

          #5
          Re: PHP to JavaScript

          This is what I am trying to do. There may be an easier way.

          1. Check to see picture is an acceptable image.
          a. Check image, picture1, for extension jpg. There are actually 10
          images and 5 extensions. But I just listed 1.
          b. If the extension in not jpg, change background color to yellow.
          c. This works well in
          JavaScript --document.getEle mentById('pt1') .style.backgrou ndColor='#FFF9B 9';
          ..
          d. To test this , try test.php with a *.gif and *.jpg

          2. If the file name is acceptable, check to see if the file size is less
          than FILE_SIZE_MAX. I noticed, if the file size is greater than
          FILE_SIZE_MAX, the program changes the file size to 0.
          a. Create an error variable
          b. Transfer the error variable to JavaScript
          c. Use JavaScript to change the background to yellow.
          d. Rewrite the original file name in the value for the input statement.

          I created the following two files to experiment with the concept.

          I cannot get the file > max to turn the background to yellow.

          What am I doing wrong? I am somewhat new at PHP.

          Thanks for the suggestions.

          Ken

          test.php

          <?php
          //error_reporting (E_ALL);
          session_start() ;
          ?>
          <html><head><ti tle></title>
          <script language="javas cript">
          function validate_pictur es() {
          document.getEle mentById('pt1') .style.backgrou ndColor='FFFFFF ';
          var validity1 = "true";
          var file_name = document.pictur e.picture1.valu e;
          extension = file_name.split (".");
          var ext1 = extension[1];
          if (ext1 == 'jpg') {
          //alert("jpg");
          }
          else {
          document.getEle mentById('pt1') .style.backgrou ndColor='#FFF9B 9';
          validity1 = "false";
          }
          if (validity1 == "true") {//alert("submit") ;
          picture.submit( );
          }
          else {
          //alert("correct error");
          window.location .href="#top-picture";
          } }

          if ($error == "false") {alert("error-php");}

          </script>
          </head>
          <body>
          <a name="top-picture" </a>
          <form enctype="multip art/form-data" name="picture" method="post"
          action="test2.p hp">
          <input type=file size=65 name="picture1" value="<?PHP
          $_SESSION['picture_name1']; ?>" Id='pt1'>

          <table align="center" cellspacing="5" cellpadding="1" border="0"
          width="100">
          <tr><td><inpu t type="button" value="Review Ad"
          onClick="valida te_pictures()"> </td></tr>
          </table>
          </form></body>
          </html>
          ---------------------------------------
          test2.php

          <?PHP
          error_reporting (E_ALL);
          session_start() ;
          //echo "start";
          $_SESSION['picture_size1'] = $_FILES['picture1']['size'];
          $_SESSION['picture_name1'] = $_FILES['picture1']['name'];
          // echo $_SESSION['picture_name1'];
          $_SESSION['valid_size1'] = "true";
          if (($_SESSION['picture_size1'] == 0) && ($_SESSION['picture_name1'] !=
          "")) {
          $_SESSION['valid_size1'] = "false";
          $error = $_SESSION['valid_size1'];
          echo "File greater than max";
          // header("Locatio n: test.php");
          exit();
          }
          ?>


          Comment

          • Geoff Berrow

            #6
            Re: PHP to JavaScript

            I noticed that Message-ID: <qxx_c.10339$nA 6.9230@twister. rdc-kc.rr.com>
            from Ken contained the following:
            [color=blue]
            >1. Check to see picture is an acceptable image.
            > a. Check image, picture1, for extension jpg. There are actually 10
            >images and 5 extensions. But I just listed 1.
            > b. If the extension in not jpg, change background color to yellow.
            > c. This works well in
            >JavaScript --document.getEle mentById('pt1') .style.backgrou ndColor='#FFF9B 9';[/color]

            There is no need to do this.

            Because the PHP is processed by the server you can simply change the
            background colour using CSS. Simply use a PHP snippet to write a
            background style in the body tag. No need for JS at all.

            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            Working...