/    Sign up×
Community /Pin to ProfileBookmark

rreload a list box inside a form?

How can I make a list box reload but not the rest of the form?
The content of the list box is a php generated list of the directory in a variably define variable.

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@yearbassNov 15.2007 — you can use folowing method (last time known as remote scripting)

  • 1. hidden frame/iframe

  • 2. AJAX


  • i think AJAX is a good idea. for ajax framework try prototype.js (http://www.prototypejs.org)
    Copy linkTweet thisAlerts:
    @yearbassNov 15.2007 — this is some example, that get list data from db

    php script [getlist.php]

    [code=php]<?php
    mysql_connect("localhost","root","");
    mysql_select_db("mydb");
    $qry = mysql_query("select id_pegawai,nama_pegawai from pegawai");

    $sResult = "";
    $sResult .= "[";
    $firstRow = true;
    while ($row = mysql_fetch_assoc($qry)) {
    if ($firstRow) $firstRow = false; else $sResult .= ",";
    $sResult .= "{id:'" .$row["id_pegawai"]. "',name:'" .$row["nama_pegawai"]. "'}";
    }
    $sResult .= "]";

    mysql_free_result($qry);
    echo $sResult;
    exit;
    ?>[/code]


    and this is your html [test.html]

    [code=php]<html>
    <head>
    <script language="javascript" src="includes/prototype.1.5.1.js"></script>
    <script type="text/javascript">
    function reloadEmployee(){
    cleanEmployee();
    new Ajax.Request(
    "getlist.php",
    {
    method:'get',
    onComplete:function(transport){
    var emp = String(transport.responseText).evalJSON();
    var f = document.frm;
    $A(emp).each(
    function(key,index){
    f.employee.options[index] = new Option(key["name"],key["id"]);
    }
    );
    }
    }
    );
    }
    function cleanEmployee(){
    var emp = document.frm.employee.options;
    $A(emp).each(
    function (key,index) {
    key[index]=null;
    }
    )
    }
    </script>
    </head>

    <body>

    <form name="frm">
    select employee: <select name="employee"></select> <a href="javascript:reloadEmployee()">Reload Employee</a>
    </form>

    </body>
    </html>[/code]


    don't forget download prototype.js library (i think is cool)
    Copy linkTweet thisAlerts:
    @PeuplarchieauthorNov 16.2007 — I'm not using a db.

    I can't on that server.
    Copy linkTweet thisAlerts:
    @yearbassNov 16.2007 — no problem you are using db or not, the data in getlist.php not always get from db, it can get from text file instead, or read files from a directory.

    but you need to decide the format for the result. in my example the format is in JSON (JavaScript Object Notation), you can use another format such as XML or another one, but JSON and XML are readable in javascript.
    ×

    Success!

    Help @Peuplarchie spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...