Javascript Dynamic PHP database Query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ubccis@gmail.com

    Javascript Dynamic PHP database Query

    Hi.

    I'm wondering if you can dynamically query a database with javascript
    and php. For example, can I select something on the select menu and
    dynamically query the database and populate another select box without
    reloading the page?

  • Allan Sun

    #2
    Re: Javascript Dynamic PHP database Query

    This is how I would do it:

    1. Insert a iframe with height=0 and border=0 in your php file.
    2. In your menu, set the object to be clicked to have the
    onClick="url='t he_page_in_ifra me.php?params=s omething';frame s['your_iframe_id '].location.href= url";
    3. In your iframe php file, query the database and get the result, add
    them with escape charactor "\" for every thing Javascript needed, clean
    the \n .
    4. In your iframe php file, write the js as:
    parent.document .getElementById ('your_main_men u_object_id').i nnerHTML="<?=
    $what_you_just_ got ?>";
    5. Then you'll see it!

    I can give you more codes tommorrow when I get back to my office, if
    you feel they are useful.

    I would very much like to see how they other people are dealling with
    it, coz I even thought it's a bit complex:(

    Comment

    • ramiro

      #3
      Re: Javascript Dynamic PHP database Query

      I would like to view more codes of this if you have, because I tried
      this so much and never could make it. I'm not so good with Iframes and
      Javascript.
      Regards,
      Ramiro Varandas Jr

      Comment

      • Chung Leong

        #4
        Re: Javascript Dynamic PHP database Query

        "Allan Sun" <sunajia@gmail. com> wrote in message
        news:1106687210 .029908.183890@ c13g2000cwb.goo glegroups.com.. .[color=blue]
        > This is how I would do it:
        >
        > 1. Insert a iframe with height=0 and border=0 in your php file.
        > 2. In your menu, set the object to be clicked to have the
        >[/color]
        onClick="url='t he_page_in_ifra me.php?params=s omething';frame s['your_iframe_i
        d'].location.href= url";[color=blue]
        > 3. In your iframe php file, query the database and get the result, add
        > them with escape charactor "\" for every thing Javascript needed, clean
        > the \n .
        > 4. In your iframe php file, write the js as:
        > parent.document .getElementById ('your_main_men u_object_id').i nnerHTML="<?=
        > $what_you_just_ got ?>";
        > 5. Then you'll see it!
        >
        > I can give you more codes tommorrow when I get back to my office, if
        > you feel they are useful.
        >
        > I would very much like to see how they other people are dealling with
        > it, coz I even thought it's a bit complex:(
        >[/color]

        A while back when I was playing around with Flash and AMFPHP, I wrote a
        little class that let you retrieve data from a remote server in a manner
        similiar to Flash Remoting in a HTML page. Then I promptly forgot about it.

        The method used is the same as the one you described, except the iframe is
        created dynamically, a post done instead, and the iframe is destroyed when
        afterward. This last step turned out to be critical for otherwise the link
        to the iframe page will linger in the history, messing up the back/forward
        button navigation.

        The class let you call a server method with any number of parameters, all of
        which can be complex objects. The method can also return a complex object.
        The class traps PHP errors, sending messages back to Javascript so you can
        throw them up on an alert box. Pretty neat stuff.

        I've just put it up on my personal page at http://www.conradish.net/bobo/
        along with a little demo. The class is called Pajama (PHP -> Javascript =
        PJ, get it?).



        Comment

        • Dani CS

          #5
          Re: Javascript Dynamic PHP database Query

          ubccis@gmail.co m wrote:[color=blue]
          > Hi.
          >
          > I'm wondering if you can dynamically query a database with javascript
          > and php. For example, can I select something on the select menu and
          > dynamically query the database and populate another select box without
          > reloading the page?
          >[/color]

          XMLHttpRequest gives you the power. Google it.

          Comment

          • Allan

            #6
            Re: Javascript Dynamic PHP database Query

            Yes XMLHttpRequest is really a good idea! But sometimes I just found it
            not that good for debugging:P
            For those who would still go XMLhttpRequest, here is a piece of very
            nice and clean code

            function loadFragmentInT oElement(fragme nt_url, element_id) {
            var element = document.getEle mentById(elemen t_id);
            element.innerHT ML = '<p><em>Loadin g ...</em></p>';
            xmlhttp.open("G ET", fragment_url);
            xmlhttp.onready statechange = function() {
            if (xmlhttp.readyS tate == 4 && xmlhttp.status == 200) {
            element.innerHT ML = xmlhttp.respons eText;
            }
            }
            xmlhttp.send(nu ll);
            }

            For what I've done, it looks a bit more comlex than XMLHttpRequest,
            maybe I should say it's really worth than XMLHttpRequest, but I'll
            still put it here. Because most of the browsers support Iframe and
            object.innerHTM L now.

            HTML part:
            =============== =============== =============== =============== ======
            <iframe src="about:blan k" height="0" width="100%" frameborder="1"
            id="iframe_php " name="iframe_ph p">Sorry your browser doesn't support
            IFRAME, this programme may not work properly.</iframe>

            <input name="criterium Submit" type="button" id="criteriumSu bmit"
            value="Query" onclick="
            var url='campaignif rame.php?action =searchUsers';
            url+='&title='+ document.getEle mentById('title ').value;
            url+='&gender=' +document.getEl ementById('gend er').value;
            url+='&firstNam e='+document.ge tElementById('f irstName').valu e;
            url+='&lastName ='+document.get ElementById('la stName').value;
            url+='&minAge=' +document.getEl ementById('minA ge').value;
            url+='&maxAge=' +document.getEl ementById('maxA ge').value;
            url+='&city='+d ocument.getElem entById('city') .value;
            url+='&postcode ='+document.get ElementById('po stcode').value;
            url+='&info='+d ocument.getElem entById('info') .checked;
            url+='&mobileNu mber='+document .getElementById ('mobileNumber' ).checked;
            frames['iframe_php_res ult'].location.href= url;"/>
            PHP part
            =============== =============== =============== =============== =============== =====
            function outputJSString( $string){

            //make it Javascript compatitable
            $string=str_rep lace("\n","",$t emplate->fetch());
            $string=str_rep lace("\s","",$s tring);
            $string=str_rep lace("\t","",$s tring);
            $string=str_rep lace("\r","",$s tring);
            $string=str_rep lace("\"","\\\" ",$string);
            $string=str_rep lace("'","\'",$ string);
            // and send the results
            return trim($string);
            }

            Javascript Part
            =============== =============== =============== =============== =============== =====
            <script LANGUAGE="JavaS cript">
            parent.document .getElementById ('target').inne rHTML = null;
            parent.document .getElementById ('target').inne rHTML='<?=
            $view->outputJSString () ?>';
            </script>

            Quite a lot ham? Franckly I would go for XMLHttpRequest if I don't need
            to consider the browser campabilities for now:)

            Comment

            • R. Rajesh Jeba Anbiah

              #7
              Re: Javascript Dynamic PHP database Query

              ubccis@gmail.co m wrote:[color=blue]
              > I'm wondering if you can dynamically query a database with javascript
              > and php. For example, can I select something on the select menu and
              > dynamically query the database and populate another select box[/color]
              without[color=blue]
              > reloading the page?[/color]




              --
              <?php echo 'Just another PHP saint'; ?>
              Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

              Comment

              Working...