Possible help with php/ajax call?

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

    #1

    Possible help with php/ajax call?

    Fellow php'ers;

    I am trying to make a call to a js function from within a php function based
    on the selected value of an HTML select statement (whew!).

    While the server side php program is getting called the variable being
    passed is always empty. The following is my select statement from within
    my php program which itself falls within a php function:

    print "
    <select name=\"_stcd\" id=\"_stcd\"
    onchange=\"ajax CallServer('htt p://".
    $_SERVER['SERVER_NAME']."/cnl0d2.php?_sel Name=_gameid&_s tcd=".$_stcd."' )
    >";
    The valut of $_stcd SHOULD contain the value of the select item the user
    selects. But its always blank.

    The select option lines are generated from an array of values immediately
    following this line.

    As I've been searching, researching, re-reading my php books, javascript
    books, etc for two days now, I thought it best to ask someone else.

    Any ideas as to why this value is always blank?

    Thanks all (my mind is fried!)

    Bob

  • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

    #2
    Re: Possible help with php/ajax call?

    Bob M escribió:
    I am trying to make a call to a js function from within a php function based
    on the selected value of an HTML select statement (whew!).
    You cannot. This his how all this works: PHP runs on the web server, it
    outputs a regular HTML document with possibly some bits of JavaScript
    and it delivers this document to your web browser, which then parses the
    HTML and runs the JavaScript code. Whatever PHP tutorial you read, this
    is explained in the first chapter :-P
    While the server side php program is getting called the variable being
    passed is always empty. The following is my select statement from within
    my php program which itself falls within a php function:
    >
    print "
    <select name=\"_stcd\" id=\"_stcd\"
    onchange=\"ajax CallServer('htt p://".
    $_SERVER['SERVER_NAME']."/cnl0d2.php?_sel Name=_gameid&_s tcd=".$_stcd."' )
    >";
    It's simple. This code runs in your computer, not in the server, thus
    you cannot use PHP. You must use JavaScript to fetch the current value
    of the <selectfield.


    --
    -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
    -- Mi sitio sobre programación web: http://bits.demogracia.com
    -- Mi web de humor al baño María: http://www.demogracia.com
    --

    Comment

    • Bob M

      #3
      Re: Possible help with php/ajax call?

      "Álvaro G. Vicario" wrote:
      Bob M escribió:
      >I am trying to make a call to a js function from within a php function
      >based on the selected value of an HTML select statement (whew!).
      >
      You cannot. This his how all this works: PHP runs on the web server, it
      outputs a regular HTML document with possibly some bits of JavaScript
      and it delivers this document to your web browser, which then parses the
      HTML and runs the JavaScript code. Whatever PHP tutorial you read, this
      is explained in the first chapter :-P
      >
      >While the server side php program is getting called the variable being
      >passed is always empty. The following is my select statement from within
      >my php program which itself falls within a php function:
      >>
      >print "
      ><select name=\"_stcd\" id=\"_stcd\"
      >onchange=\"aja xCallServer('ht tp://".
      >$_SERVER['SERVER_NAME']."/cnl0d2.php?_sel Name=_gameid&_s tcd=".$_stcd."' )
      >>";
      >
      It's simple. This code runs in your computer, not in the server, thus
      you cannot use PHP. You must use JavaScript to fetch the current value
      of the <selectfield.
      >
      >
      Thanks - Got It! Duh, me!

      Comment

      • Bob M

        #4
        Re: Possible help with php/ajax call?

        Thanks - Got It! Duh, me!

        So, does this NOW seem like it "should" be correct? I am getting a php
        syntax error on the unexpected character of "["

        print "
        onchange=\"ajax CallServer('htt p://"
        $_SERVER['SERVER_NAME']."/cnl0d2.php?_sel Name=_gameid&_s tcd="
        +document.getEl ementById('_stc d').options[document.getEle mentById('_stcd ').selectedInde x].value'')
        >";


        Comment

        • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

          #5
          Re: Possible help with php/ajax call?

          Bob M escribió:
          So, does this NOW seem like it "should" be correct? I am getting a php
          syntax error on the unexpected character of "["
          >
          print "
          onchange=\"ajax CallServer('htt p://"
          There's a concatenation operator missing (aka dot) and some other typos.

          It's very difficult to debug code written this way. Sometimes it's
          easier to just end PHP mode and write HTML as is:

          // ... some PHP
          ?>
          <select name="foo" onclick="alert( 'Bar')">
          <?php
          // More PHP

          $_SERVER['SERVER_NAME']."/cnl0d2.php?_sel Name=_gameid&_s tcd="
          +document.getEl ementById('_stc d').options[document.getEle mentById('_stcd ').selectedInde x].value'')
          >";
          A couple of JavaScript tips:

          * Don't write all your code in one line inside the onchange attribute:
          use functions

          * Your code is executed when printed, thus _stcd will not change. Try
          something like:

          onchange="fetch Data(this)"

          function fetchData(field ){
          var url = "/cnl0d2.php?_sel Name=_gameid&_s tcd=" +
          field.options[field.selectedI ndex].value;
          ...
          }



          --
          -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
          -- Mi sitio sobre programación web: http://bits.demogracia.com
          -- Mi web de humor al baño María: http://www.demogracia.com
          --

          Comment

          • Hugh Oxford

            #6
            Re: Possible help with php/ajax call?

            Bob M wrote:
            Fellow php'ers;
            >
            I am trying to make a call to a js function from within a php function based
            on the selected value of an HTML select statement (whew!).
            >
            Can I suggest Xajax (http://xajaxproject.org/)? You'll never have to
            think about these things again.

            Comment

            • Jerry Stuckle

              #7
              Re: Possible help with php/ajax call?

              Hugh Oxford wrote:
              Bob M wrote:
              >Fellow php'ers;
              >>
              >I am trying to make a call to a js function from within a php function
              >based
              >on the selected value of an HTML select statement (whew!).
              >>
              Can I suggest Xajax (http://xajaxproject.org/)? You'll never have to
              think about these things again.
              >
              It won't help in this case. His problem is not the Ajax call, but how
              he was expecting the PHP code to run.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              Working...