Forum Sementara Putera.com

Would you like to react to this message? Create an account in a few clicks or log in to continue.
Forum Sementara Putera.com

Bersama kita perkemaskan forum ini sementara forum asal dalam pemulihan.

Forum putera dah kembali. Masalah sudah berjaya diselesaikan. Sila lawati http://forum.putera.com/tanya


2 posters

    Sape otai AJAX, mintak tolong sket..

    neology
    neology
    Ahli Baharu
    Ahli Baharu


    Gender : Male Number of posts : 372
    Age : 38
    Registration date : 25/04/2009

    Sape otai AJAX, mintak tolong sket.. Empty Sape otai AJAX, mintak tolong sket..

    Post by neology Tue Aug 04, 2009 8:13 pm

    Salam,

    Aku baru mula berjinak-jinak dengan AJAX walaupun dah lama mengetahui kelebihan ajax ni. Aku ada basic dalam javascript,PHP,MySQL dan HTML.

    So, aku nak buat satu AJAX yang check database every 3 minutes.
    Maknanya, setiap 3 minutes, satu callback function dia call.
    Setakat yang aku tgk tutorial, response dari server dia gunekan XMLresponse dalam server side scripting.

    Code:

    <xmlresponse>
        <result><?= myfunction() ?></result>
    </xmlresponse>

    tapi dia xckp lak camne kita nak gune return value tu..kalo return value dalam array, camne lak?

    sape2 yang terer AJAX, bleh tolong x?
    johnburn
    johnburn
    Moderators
    Moderators


    Gender : Male Number of posts : 755
    Location : Terengganu
    Registration date : 07/03/2009

    Sape otai AJAX, mintak tolong sket.. Empty Re: Sape otai AJAX, mintak tolong sket..

    Post by johnburn Tue Aug 04, 2009 10:37 pm

    Contoh kod:
    Save sebagai ajax.js
    Code:

    // mula bahagian setting
    var saat = 5;
    var idDiv = "papar";
    var url = "process.php";
    //tamat bahagian setting

    //mula bahagian XMLHttpRequest
    function GetXmlHttpRequest(){
    var xmlHttp;
    try{
    xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
    }
    catch (e){
    try{
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
    }
    catch (e){
    try{
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e){
    alert("Pelayar web ini tidak menyokong AJAX.");
    return false;
    }
    }
    }
    //tamat bahagian XMLHttpRequest

    //mula bahagian code
    xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
    document.getElementById(idDiv).innerHTML=xmlHttp.responseText;
    setTimeout('GetXmlHttpRequest()',saat*1000);
    }
    }
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    }
    //tamat bahagian code

    //mula bahagian refresh
    var saat;
    window.onload = function mulaRefresh(){
    setTimeout('GetXmlHttpRequest()',saat*1000);
    }
    //tama bahagian refresh

    Contoh process.php
    Code:

    <?php
    // setting sambungan ke MySQL

    $dbname = 'namaDB';
    $user = 'root';
    $password = 'passwordDB';


    // buat sambungan ke database

    $sql = mysql_connect("localhost", $user, $password)
    or die(mysql_error());

    mysql_select_db("$dbname", $sql);

    // Fetch data

    $query = "SELECT * FROM namaTable";
    $result = mysql_query($query);

    // Return  results dari query

    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<b>Column 1:</b> {$row['column1']}" .
    "<b>Column 2:</b> {$row['column2']}" .
    }

    ?>

    So basically, kat page yang ko nk paparkan value dari query tu, just linkkn file ajax.js tu kat bhgian head:
    Code:

    <script src="ajax.js"></script>
    next, pnggl function dlm ajax.js td
    Code:

    <script type="text/javascript"><!--
    GetXmlHttpRequest();
    // --></script>
    dan kat bahagian div yg nk return result tu ltak id dia seperti yg ada dlm ajax.js
    Code:

    <div id="papar"></div>
    neology
    neology
    Ahli Baharu
    Ahli Baharu


    Gender : Male Number of posts : 372
    Age : 38
    Registration date : 25/04/2009

    Sape otai AJAX, mintak tolong sket.. Empty Re: Sape otai AJAX, mintak tolong sket..

    Post by neology Wed Aug 05, 2009 2:05 am

    tengkiu johnburn..
    memang benar2 membantu..
    tima kasih... Smile
    neology
    neology
    Ahli Baharu
    Ahli Baharu


    Gender : Male Number of posts : 372
    Age : 38
    Registration date : 25/04/2009

    Sape otai AJAX, mintak tolong sket.. Empty Re: Sape otai AJAX, mintak tolong sket..

    Post by neology Wed Sep 16, 2009 4:23 pm

    eh..nak tanya lagi..camne nk buat sementara result nk papar..kita nak kuarkan mesej : Loading..kita nk tmbh katne ek?

    kat sini ke?
    Code:

    document.getElementById(idDiv).innerHTML=xmlHttp.responseText;
    http://document.write("Loading..Please Wait");//
    setTimeout('GetXmlHttpRequest()',saat*1000);
    johnburn
    johnburn
    Moderators
    Moderators


    Gender : Male Number of posts : 755
    Location : Terengganu
    Registration date : 07/03/2009

    Sape otai AJAX, mintak tolong sket.. Empty Re: Sape otai AJAX, mintak tolong sket..

    Post by johnburn Wed Sep 16, 2009 4:43 pm

    ko nk bt untuk code yg kt atas ni gak ke?
    neology
    neology
    Ahli Baharu
    Ahli Baharu


    Gender : Male Number of posts : 372
    Age : 38
    Registration date : 25/04/2009

    Sape otai AJAX, mintak tolong sket.. Empty Re: Sape otai AJAX, mintak tolong sket..

    Post by neology Wed Sep 16, 2009 4:49 pm

    some sort la..cume nk paparkan text 'loading' sementara result dia nk kuar...cth mcm code kat atas..tapi bukan setiap 3minit..mcm bila klik button,sementara nk tunggu result..dia papar msg "Loading.."dh abih loading..baru dia paparkan result daripada httpRequest kite..
    johnburn
    johnburn
    Moderators
    Moderators


    Gender : Male Number of posts : 755
    Location : Terengganu
    Registration date : 07/03/2009

    Sape otai AJAX, mintak tolong sket.. Empty Re: Sape otai AJAX, mintak tolong sket..

    Post by johnburn Thu Sep 17, 2009 3:23 am

    kl xsilap aku, just tmbah sblom xmlHttp.send dipanggil
    ko leh tmbh2 ape2 kod html samada ko nk tmbh message yg sebut 'loading...' ke atau image loading kt div yg mna result akan dipaprkn
    neology
    neology
    Ahli Baharu
    Ahli Baharu


    Gender : Male Number of posts : 372
    Age : 38
    Registration date : 25/04/2009

    Sape otai AJAX, mintak tolong sket.. Empty Re: Sape otai AJAX, mintak tolong sket..

    Post by neology Thu Sep 17, 2009 1:48 pm

    ok..tq bro..dh berjaya..
    johnburn
    johnburn
    Moderators
    Moderators


    Gender : Male Number of posts : 755
    Location : Terengganu
    Registration date : 07/03/2009

    Sape otai AJAX, mintak tolong sket.. Empty Re: Sape otai AJAX, mintak tolong sket..

    Post by johnburn Thu Sep 17, 2009 1:59 pm

    ok no hal. sbnarnye xssh pn kl ko pham konsep dia sbb ajax ni bkn pkai bnda br pn. javascript je
    neology
    neology
    Ahli Baharu
    Ahli Baharu


    Gender : Male Number of posts : 372
    Age : 38
    Registration date : 25/04/2009

    Sape otai AJAX, mintak tolong sket.. Empty Re: Sape otai AJAX, mintak tolong sket..

    Post by neology Thu Sep 17, 2009 2:13 pm

    ok..thanks..dh nmpk clear sket dh..cuma nk faham konsep je..rasanya dh faham.. Smile

    Sponsored content


    Sape otai AJAX, mintak tolong sket.. Empty Re: Sape otai AJAX, mintak tolong sket..

    Post by Sponsored content


      Current date/time is Mon Nov 18, 2024 8:17 am