declaring variable in onchange to use in php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • R.G. Vervoort

    #1

    declaring variable in onchange to use in php

    is it possible to declare a variable in the onchange event of a <select> and
    use it later in php

    I would like to put the value of the selected option (in <select>) in a
    variable and later when i activate a php function use this variable

    Looks inpossible but who knows maybe it is possible


    roy


  • Michael Winter

    #2
    Re: declaring variable in onchange to use in php

    On Thu, 15 Apr 2004 23:45:42 +0200, R.G. Vervoort
    <roy.vervoort@r oyvervoort.nl> wrote:
    [color=blue]
    > is it possible to declare a variable in the onchange event of a <select>
    > and use it later in php
    >
    > I would like to put the value of the selected option (in <select>) in a
    > variable and later when i activate a php function use this variable
    >
    > Looks inpossible but who knows maybe it is possible[/color]

    You'll have to send it to the server using either the search (query) part
    of the URI, or POSTed in a form. Server-side languages cannot obtain the
    values of script variables directly as that data only exists on the client.

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Grant Wagner

      #3
      Re: declaring variable in onchange to use in php

      "R.G. Vervoort" wrote:
      [color=blue]
      > is it possible to declare a variable in the onchange event of a <select> and
      > use it later in php
      >
      > I would like to put the value of the selected option (in <select>) in a
      > variable and later when i activate a php function use this variable
      >
      > Looks inpossible but who knows maybe it is possible
      >
      > roy[/color]

      <select onchange="sendT oServer(this);" >

      <script type="text/javascript">
      function sendToServer(se l) {
      (new Image()).src = 'path/to/your/file.php?theVal ue=' +
      sel.options[sel.selectedInd ex].value;
      }
      </script>

      You have no way of knowing whether or not the call succeeded using this method.
      You can achieve some degree of error checking by having the file.php retrieve
      and send an image of specific dimensions back to the client, then you could
      interrogate those dimensions to determine whether the call succeeded or not:

      <script type="text/javascript">
      function sendToServer(se l) {
      var i = new Image();
      i.onload = imgOnLoad;
      i.onerror = imgOnError;
      i.src = 'path/to/your/file.php?theVal ue=' +
      sel.options[sel.selectedInd ex].value;
      // file.php would do whatever with the value passed and then
      // return an image that is 10 pixels wide if everything succeeded
      // and an image that is 20 pixels wide if anything failed
      }
      function imgOnLoad() {
      if (this.width == 10) {
      alert('Success' );
      } else if (this.width == 20) {
      alert('Failure' );
      }
      }
      function imgOnError() {
      alert('Call to set theValue on the server failed');
      }
      </script>

      --
      | Grant Wagner <gwagner@agrico reunited.com>

      * Client-side Javascript and Netscape 4 DOM Reference available at:
      *


      * Internet Explorer DOM Reference available at:
      *
      http://msdn.microsoft.com/workshop/a...ence_entry.asp

      * Netscape 6/7 DOM Reference available at:
      * http://www.mozilla.org/docs/dom/domref/
      * Tips for upgrading JavaScript for Netscape 7 / Mozilla
      * http://www.mozilla.org/docs/web-deve...upgrade_2.html


      Comment

      Working...