accessing PHP in JS code

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

    accessing PHP in JS code

    i have some php code pulling some data from a database and i need to access
    some of hte variables in java script. i've never done this before. how can
    i do it.

    eg. i'm loading a combo box from the database (this works fine). but, i'd
    like to send some description data to a text area when user selects
    something, from the combo using, JS. when they click a button they would go
    to a website (this works fine for me as well)

    any help would be appreciated,

    tim


    <?
    require_once("l ogin.php");
    login();


    mysql_select_db ("krra");
    $options = "select linkname,linkad dress, linkdesc from links order by
    linknum";
    $optres = mysql_query($op tions);
    $opt_results = mysql_num_rows( $optres);

    $selsizequery = "select * from links";
    $selsize = mysql_query($se lsizequery);
    $selectsize = mysql_num_rows( $selsize);
    echo "$selectsiz e links in this list: ";

    echo "<select name = example size = $selectsize onChange=showte xt()>";

    for ($i=0; $i <$opt_results ; $i++)
    {
    $optrow = mysql_fetch_arr ay($optres);
    $optionval = stripslashes($o ptrow["linkname"]);
    $optiongoto = stripslashes($o ptrow["linkaddres s"]);
    echo "<option value=$optiongo to>$optionval</option>";
    echo "<br>";

    }
    echo "</select>";

    ?>
    </p>
    <p align="left">
    &nbsp;<scrip t language="javas cript">


    <!--

    var shortcut=docume nt.combowithtex t
    var descriptions=ne w Array()

    //extend this list if neccessary to accomodate more selections

    <?
    require_once("l ogin.php");
    login();

    mysql_select_db ("krra");
    $options = "select linkdesc from links order by linknum";
    $optres = mysql_query($op tions);
    $opt_results = mysql_num_rows( $optres);

    for ($i=0; $i <$opt_results ; $i++)
    {
    $optrow = mysql_fetch_arr ay($optres);
    $descript = stripslashes($o ptrow["linkdesc"]);

    }
    ?>

    descriptions[0]="descriptio n of data from combo selection 1"
    descriptions[1]="descriptio n of data from combo selection 2."
    descriptions[2]="descriptio n of data from combo selection"
    descriptions[3]="descriptio n of data from combo selection ."


    shortcut.text.v alue=descriptio ns[shortcut.exampl e.selectedIndex]
    function gothere(){
    location=shortc ut.example.opti ons[shortcut.exampl e.selectedIndex].value
    }

    function showtext(){
    shortcut.text.v alue=descriptio ns[shortcut.exampl e.selectedIndex]
    }
    //-->
    </script></p>

    </form>
    <p><input type="button" value="Go!" onClick="gother e()" style="float:
    left"></p>

    </body>
    </html>


  • Terence

    #2
    Re: accessing PHP in JS code

    Tim Blackwell wrote:
    [color=blue]
    > i have some php code pulling some data from a database and i need to access
    > some of hte variables in java script. i've never done this before. how can
    > i do it.
    >
    > eg. i'm loading a combo box from the database (this works fine). but, i'd
    > like to send some description data to a text area when user selects
    > something, from the combo using, JS. when they click a button they would go
    > to a website (this works fine for me as well)
    >
    > any help would be appreciated,
    >
    > tim
    >
    >
    > <?
    > require_once("l ogin.php");
    > login();
    >
    >
    > mysql_select_db ("krra");
    > $options = "select linkname,linkad dress, linkdesc from links order by
    > linknum";
    > $optres = mysql_query($op tions);
    > $opt_results = mysql_num_rows( $optres);
    >
    > $selsizequery = "select * from links";
    > $selsize = mysql_query($se lsizequery);
    > $selectsize = mysql_num_rows( $selsize);
    > echo "$selectsiz e links in this list: ";
    >
    > echo "<select name = example size = $selectsize onChange=showte xt()>";
    >
    > for ($i=0; $i <$opt_results ; $i++)
    > {
    > $optrow = mysql_fetch_arr ay($optres);
    > $optionval = stripslashes($o ptrow["linkname"]);
    > $optiongoto = stripslashes($o ptrow["linkaddres s"]);
    > echo "<option value=$optiongo to>$optionval</option>";
    > echo "<br>";
    >
    > }
    > echo "</select>";
    >
    > ?>
    > </p>
    > <p align="left">
    > &nbsp;<scrip t language="javas cript">
    >
    >
    > <!--
    >
    > var shortcut=docume nt.combowithtex t
    > var descriptions=ne w Array()
    >
    > //extend this list if neccessary to accomodate more selections
    >
    > <?
    > require_once("l ogin.php");
    > login();
    >
    > mysql_select_db ("krra");
    > $options = "select linkdesc from links order by linknum";
    > $optres = mysql_query($op tions);
    > $opt_results = mysql_num_rows( $optres);
    >
    > for ($i=0; $i <$opt_results ; $i++)
    > {
    > $optrow = mysql_fetch_arr ay($optres);
    > $descript = stripslashes($o ptrow["linkdesc"]);
    >
    > }
    > ?>
    >
    > descriptions[0]="descriptio n of data from combo selection 1"
    > descriptions[1]="descriptio n of data from combo selection 2."
    > descriptions[2]="descriptio n of data from combo selection"
    > descriptions[3]="descriptio n of data from combo selection ."
    >
    >
    > shortcut.text.v alue=descriptio ns[shortcut.exampl e.selectedIndex]
    > function gothere(){
    > location=shortc ut.example.opti ons[shortcut.exampl e.selectedIndex].value
    > }
    >
    > function showtext(){
    > shortcut.text.v alue=descriptio ns[shortcut.exampl e.selectedIndex]
    > }
    > //-->
    > </script></p>
    >
    > </form>
    > <p><input type="button" value="Go!" onClick="gother e()" style="float:
    > left"></p>
    >
    > </body>
    > </html>
    >
    >[/color]

    what's wrong with

    <?php
    // get "items" from MySQL database
    // javascript source code
    print "var arrayFoo = {'item2','item3 ','item4','item 5','item6'};";
    ?>

    You could put all the data in a separate javascript file if you want to
    and call it from the HTML page like so

    <script language="JavaS cript" src="js_vars.ph p"
    type="text/javascript"></script>



    Even easier, use the "Heredoc" syntax


    $str = <<<EOD

    var arrayFoo = {'item2','item3 ','item4','item 5','item6'};
    // more javascript goes here

    EOD;

    Comment

    • Chung Leong

      #3
      Re: accessing PHP in JS code

      A simple way is to dump the text into a bunch of hidden fields.
      Inserting the variable into the Javascript is a little harder, since
      you need to correctly escape slashes, quotes, and line-breaks.

      <?

      $js_descr_array = array();
      for ($i=0; $i <$opt_results ; $i++)
      {
      $optrow = mysql_fetch_arr ay($optres);
      $descript = stripslashes($o ptrow["linkdesc"]);
      $js_descr_array[i] = '"' . addcslashes($de script, "\r\n\"\\") .
      '"';
      }


      ?>

      <script>

      var descriptions = [<?php implode(',' $js_descr_array ); >];

      </script>


      "Tim Blackwell" <timblackwell@h otmail.com> wrote in message news:<bptuko$qv 4$1@knot.queens u.ca>...[color=blue]
      > i have some php code pulling some data from a database and i need to access
      > some of hte variables in java script. i've never done this before. how can
      > i do it.
      >
      > eg. i'm loading a combo box from the database (this works fine). but, i'd
      > like to send some description data to a text area when user selects
      > something, from the combo using, JS. when they click a button they would go
      > to a website (this works fine for me as well)
      >
      > any help would be appreciated,
      >
      > tim
      >
      >
      > <?
      > require_once("l ogin.php");
      > login();
      >
      >
      > mysql_select_db ("krra");
      > $options = "select linkname,linkad dress, linkdesc from links order by
      > linknum";
      > $optres = mysql_query($op tions);
      > $opt_results = mysql_num_rows( $optres);
      >
      > $selsizequery = "select * from links";
      > $selsize = mysql_query($se lsizequery);
      > $selectsize = mysql_num_rows( $selsize);
      > echo "$selectsiz e links in this list: ";
      >
      > echo "<select name = example size = $selectsize onChange=showte xt()>";
      >
      > for ($i=0; $i <$opt_results ; $i++)
      > {
      > $optrow = mysql_fetch_arr ay($optres);
      > $optionval = stripslashes($o ptrow["linkname"]);
      > $optiongoto = stripslashes($o ptrow["linkaddres s"]);
      > echo "<option value=$optiongo to>$optionval</option>";
      > echo "<br>";
      >
      > }
      > echo "</select>";
      >
      > ?>
      > </p>
      > <p align="left">
      > &nbsp;<scrip t language="javas cript">
      >
      >
      > <!--
      >
      > var shortcut=docume nt.combowithtex t
      > var descriptions=ne w Array()
      >
      > //extend this list if neccessary to accomodate more selections
      >
      > <?
      > require_once("l ogin.php");
      > login();
      >
      > mysql_select_db ("krra");
      > $options = "select linkdesc from links order by linknum";
      > $optres = mysql_query($op tions);
      > $opt_results = mysql_num_rows( $optres);
      >
      > for ($i=0; $i <$opt_results ; $i++)
      > {
      > $optrow = mysql_fetch_arr ay($optres);
      > $descript = stripslashes($o ptrow["linkdesc"]);
      >
      > }
      > ?>
      >
      > descriptions[0]="descriptio n of data from combo selection 1"
      > descriptions[1]="descriptio n of data from combo selection 2."
      > descriptions[2]="descriptio n of data from combo selection"
      > descriptions[3]="descriptio n of data from combo selection ."
      >
      >
      > shortcut.text.v alue=descriptio ns[shortcut.exampl e.selectedIndex]
      > function gothere(){
      > location=shortc ut.example.opti ons[shortcut.exampl e.selectedIndex].value
      > }
      >
      > function showtext(){
      > shortcut.text.v alue=descriptio ns[shortcut.exampl e.selectedIndex]
      > }
      > //-->
      > </script></p>
      >
      > </form>
      > <p><input type="button" value="Go!" onClick="gother e()" style="float:
      > left"></p>
      >
      > </body>
      > </html>[/color]

      Comment

      Working...