passing retirned value from Javascript to PHP

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

    #1

    passing retirned value from Javascript to PHP

    Hi,
    how can I pass the returned value from Javascript to PHP?
    I have:



    ------------------------------------------------------------------------
    ------

    if ( x>y) {
    echo '<script language="JavaS cript">';
    echo 'confirm(\''.$i temname.' requires '.$optionname.' Do you want
    to add '.$optionname.' ?\')</script>';

    <I want to return the true/false value returned when user presses
    OK/Cancel, to PHP code>

    }

    ------------------------------------------------------------------------
    ------

    Thanx


  • Andy Hassall

    #2
    Re: passing retirned value from Javascript to PHP

    On Thu, 11 Mar 2004 14:04:42 GMT, "Xerxes" <ashkaan57@hotm ail.com> wrote:
    [color=blue]
    >how can I pass the returned value from Javascript to PHP?
    >I have:
    >
    >------------------------------------------------------------------------
    >------
    >
    > if ( x>y) {
    > echo '<script language="JavaS cript">';
    > echo 'confirm(\''.$i temname.' requires '.$optionname.' Do you want
    >to add '.$optionname.' ?\')</script>';
    >
    > <I want to return the true/false value returned when user presses
    >OK/Cancel, to PHP code>
    >
    > }
    >
    >------------------------------------------------------------------------
    >------[/color]

    Is this triggered by submitting a form? If so, use Javascript to modify the
    form to introduce an <input type="hidden"> element, or set the value of an
    existing <input> element, so it's available to the PHP script it's submitted
    to.

    Since the only way to get something run by a client is to post it to another
    PHP script, it can't be done 'inline', so it's off-topic here, try a Javascript
    group. Followups set to comp.lang.javas cript.

    --
    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
    <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

    Comment

    • david

      #3
      Re: passing retirned value from Javascript to PHP

      > >how can I pass the returned value from Javascript to PHP?[color=blue][color=green]
      > >I have:
      > >[/color]
      > Since the only way to get something run by a client is to post it to[/color]
      another[color=blue]
      > PHP script, it can't be done 'inline', so it's off-topic here, try a[/color]
      Javascript[color=blue]
      > group. Followups set to comp.lang.javas cript.[/color]

      Actually, if you are determined to do this, it CAN be done in PHP, and so it
      is on topic. It is just rather convolulted.

      Here is the outline. Assume that you have a form (or something). When you
      change the value in something, like a drop down box, you would like to
      change the values of another drop down box, BUT, you want to run server side
      code to accomplish this. I use this technique, a lot, to perform dynamic
      drop down box generation, ON THE SAME screen, fom the server, WITHOUT a
      refresh.

      On the page you want this to happen:
      Include a form with the drop down box that the user can select from. This
      drop down box has an "onChange" event attached to it. One I use looks like
      this:
      function doDB () {
      document.input. tbl.disabled = false;
      if (needClear) {
      removeAll(docum ent.input.tbl);
      document.input. tbl.disabled = true;
      }
      var theElement = document.input. db;
      var theIndex = theElement.sele ctedIndex;
      var theValue = theElement.opti ons[theIndex].value;
      removeAll(docum ent.input.tbl);
      addOption (tbl', 'Select', 'NONE');
      top.hidden.docu ment.location =
      "../include/loaders/gettables.php?t hedb=" + theValue;
      }

      Here is the other routine that are used, just for your reference:
      function removeAll(theEl ement) {
      var selectLen = theElement.leng th;
      for (var i = 0; selectLen > i; i++) {
      removeLast(theE lement);
      }
      }

      I use a boolean called needClear that determines if I should erase the
      contents (based on other form elements that the user might check)

      (the routine gettables.php is the one that does all the server side stuff.
      You will need to write your own that does what you want, so I will not
      include it. However, you also need this routine to return the values: this
      is the strip of code that does that. I assume that you are capable of
      putting something into $tables <grin>:
      echo "<script>\n ";
      foreach ($tables as $key => $value) {
      echo "parent.addOpti on ('tbl', '" . $value . "', '" . $key . "');";
      }
      echo "</script>\n";

      Include another drop down box (preferably inactive for the user's sake)
      where the dynamic values will go.
      Include a blank inline frame, hidden, somewhere on the page. The one I use
      looks like this:
      echo "<div id=\"iframe\" style=\"display :none;\">\n";
      echo "<iframe src=\"../blank.php\" name=\"hidden\" ></iframe>\n";

      The source for blank.php looks like this:
      <htm>
      <head>
      <title></title>
      </head>
      <body>
      </body>
      </html>
      (spiffy, eh?)

      That should get your going. Dynamic population of drop down list boxes,
      WITHOUT a refresh, and using client-side data. I lifted these routines and
      adapated them from various places across the internet. I just don't like the
      word "impossible " <grin>

      Enjoy!
      david



      Comment

      Working...