JavaScript and PHP var problem

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

    JavaScript and PHP var problem

    Hi

    I'm trying to get a small script working I need to get the user to
    input some info from a prompt and then use that info in
    a PHP script, but I can't seem to assign the JavaScript var
    to a PHP var

    <?PHP

    if ($do == 'ban') {
    print "<SCRIPT language=JavaSc ript>";
    print "var reason; ";
    print 'reason=prompt( "Please enter the reason for banning this
    user:","");';
    print "document.write (\"Banning ID $id - \"+reason+\"<br >\");";

    /// problem here none of these work

    print "document.write (\"$banreason =\"+reason+\"\" );";
    print "$banreason = reason";
    ////
    print "</SCRIPT>";
    print "outside javascrpt the reason = $banreason<br>" ;
    // do PHP SQL here
    }

    ?>



  • Randy Webb

    #2
    Re: JavaScript and PHP var problem

    Brian wrote:
    [color=blue]
    > Hi
    >
    > I'm trying to get a small script working I need to get the user to
    > input some info from a prompt and then use that info in
    > a PHP script, but I can't seem to assign the JavaScript var
    > to a PHP var[/color]

    Thats because by the time the prompt has appeared, the PHP script on the
    server is finished processing. If you need to get info back to the
    server, submit a form, let PHP handle the form. JS and PHP can't
    communicate the way you are trying to do it.
    [color=blue]
    > <?PHP
    >
    > if ($do == 'ban') {
    > print "<SCRIPT language=JavaSc ript>";
    > print "var reason; ";
    > print 'reason=prompt( "Please enter the reason for banning this
    > user:","");';
    > print "document.write (\"Banning ID $id - \"+reason+\"<br >\");";
    >
    > /// problem here none of these work
    >
    > print "document.write (\"$banreason =\"+reason+\"\" );";
    > print "$banreason = reason";
    > ////
    > print "</SCRIPT>";
    > print "outside javascrpt the reason = $banreason<br>" ;
    > // do PHP SQL here
    > }
    >
    > ?>
    >
    >
    >[/color]


    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    • Grant Wagner

      #3
      Re: JavaScript and PHP var problem

      Brian wrote:
      [color=blue]
      > Hi
      >
      > I'm trying to get a small script working I need to get the user to
      > input some info from a prompt and then use that info in
      > a PHP script, but I can't seem to assign the JavaScript var
      > to a PHP var
      >
      > <?PHP
      >
      > if ($do == 'ban') {
      > print "<SCRIPT language=JavaSc ript>";
      > print "var reason; ";
      > print 'reason=prompt( "Please enter the reason for banning this
      > user:","");';
      > print "document.write (\"Banning ID $id - \"+reason+\"<br >\");";
      >
      > /// problem here none of these work
      >
      > print "document.write (\"$banreason =\"+reason+\"\" );";
      > print "$banreason = reason";[/color]

      You can not set a server-side variable from client-side JavaScript[1].
      PHP reads and parses the code above and generates HTML (which may or
      may not include client-side JavaScript). That HTML is sent to the
      client to be rendered, ignored, saved to disk, whatever. It does not
      matter, by the time the client sees _ANY_ of the client-side
      JavaScript you have written, the server has already forgotten about
      that request and moved on to other tasks. To say it another way, PHP
      is _not_ processed a line at a time with the client and server
      interacting in any way. PHP parses the entire file and sends the
      result to the client.
      [color=blue]
      > ////
      > print "</SCRIPT>";
      > print "outside javascrpt the reason = $banreason<br>" ;
      > // do PHP SQL here
      > }
      >
      > ?>[/color]

      [1] this is not completely true, there are ways of getting client-side
      values to the server, but not the way you are attempting to do, and
      not while the page is being parsed by PHP. For example, you could have
      something like:

      <script type="text/javascript">
      function saveInDatabase( someVariable) {
      var phpDispatcher = new Image();
      phpDispatcher.s rc =
      'http://someserver/saveInDatabase. php?theValue=' + someVariable;
      }
      </script>
      <a href="#" onclick="saveIn Database(prompt ('Enter a value to save in
      the database'));ret urn false;">Click here</a>

      but this concept can _not_ be used to return a value to the server for
      use in the same page it is currently parsing.

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • Brian

        #4
        Re: JavaScript and PHP var problem

        Thanks Guys

        Time for a re-think :) I feel some cookies coming on :)

        Brian
        [color=blue]
        > Hi
        >
        > I'm trying to get a small script working I need to get the user to
        > input some info from a prompt and then use that info in
        > a PHP script, but I can't seem to assign the JavaScript var
        > to a PHP var
        >
        > <?PHP
        >
        > if ($do == 'ban') {
        > print "<SCRIPT language=JavaSc ript>";
        > print "var reason; ";
        > print 'reason=prompt( "Please enter the reason for banning this
        > user:","");';
        > print "document.write (\"Banning ID $id - \"+reason+\"<br >\");";
        >
        > /// problem here none of these work
        >
        > print "document.write (\"$banreason =\"+reason+\"\" );";
        > print "$banreason = reason";
        > ////
        > print "</SCRIPT>";
        > print "outside javascrpt the reason = $banreason<br>" ;
        > // do PHP SQL here
        > }
        >
        > ?>
        >
        >
        >[/color]


        Comment

        Working...