I want the contents of a file to be put in a textbox without refreshing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shankari07
    New Member
    • Sep 2007
    • 16

    I want the contents of a file to be put in a textbox without refreshing

    hi guys,
    I have a form in html which has the city drop down. when clicking the drop down a javascript is called and a file(test.txt) is created and the city is written into the file.

    Through php i want to open the same file(test.txt), fetch the values and display it in a text box without refreshing or reloading the form. Is it possible.

    After refreshing it works but i want without refreshing the form.

    here is the code.



    Code:
    Code: ( text )
    (javascript)
    function addToList_city(city) 
    {
    var c =document.assess_form.cit.value;
     
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var s = fso.CreateTextFile("C://test.txt", true);
    s.write(c)
     
    }


    Code: ( php )
    [PHP]$filename = "c://test.txt";
    if (file_exists($f ilename)) {

    $fp = fopen($filename ,'r');
    $citfile= fread($fp, filesize($filen ame));
    }
    @unlink($filena me);[/PHP]



    )


    [HTML]<html>
    <form name = assess_form.php >
    <select name="cit" onchange="addTo List_city(this. form)">
    <option value="h"> Haryana </option>
    <option value = "c">Chennai </option>
    </select>

    <input type="text" name="city" value="<? echo $citfile ?>">
    </form>
    </html>[/HTML]
  • pshm
    New Member
    • Mar 2008
    • 20

    #2
    hi,
    you need to go for AJAX... I think this will help you.

    [code=html]
    <script type="text/javascript">
    <!--
    var xmlHttpObj;

    function getXMLHttpObjec t()
    {
    var xmlHttp=null;

    try{

    xmlHttp=new XMLHttpRequest( );
    }catch (e){

    try{
    xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" );
    }catch (e){
    try{
    xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP");
    }catch (e){
    alert("Your browser does not support AJAX!");
    return false;
    }
    }
    }
    return xmlHttp;
    }

    function getTextFromPHP( ){

    try{

    xmlHttpObj=getX MLHttpObject();

    if(xmlHttpObj == null){
    alert('Initiali zation/ObjectCreation faild');
    return false;
    }


    xmlHttpObj.onre adystatechange= changeState;

    }catch(err){
    alert('Error :' + err.description );
    }

    var strURL="textLoa d.php"; // url for the .php file here


    xmlHttpObj.open ("GET",strURL,t rue);
    xmlHttpObj.send (null);

    return false;
    }

    function changeState(){
    if(xmlHttpObj == null){
    alert('Initiali zation faild');
    return false;
    }

    switch(xmlHttpO bj.readyState){
    case 0:
    JavaScript:wind ow.status='Curr ent Status :The request is not initialized';
    break;

    case 1:
    JavaScript:wind ow.status='Curr ent Status :The request has been set up';
    break;

    case 2:
    JavaScript:wind ow.status='Curr ent Status :The request has been sent';
    break;

    case 3:
    JavaScript:wind ow.status='Curr ent Status :The request is in process';
    break;

    case 4:
    JavaScript:wind ow.status='Curr ent Status :The request is complete';
    document.getEle mentById('city' ).value = xmlHttpObj.resp onseText;

    break;
    }
    return true;
    }
    //-->
    </script>

    <body>
    <form name = "assess_form.ph p" action="#">
    <select name="cit" onchange="getTe xtFromPHP();">
    <option value="h"> Haryana </option>
    <option value = "c">Chennai </option>
    </select>
    <input type="text" name="city" id="city" />
    </form>
    </body>
    [/code]

    a small change in php code

    [code=php]
    // file name : textLoad.php
    $filename = "c://test.txt";
    if (file_exists($f ilename)) {

    $fp = fopen($filename ,'r');
    $citfile= fread($fp, filesize($filen ame));
    echo $citfile
    }
    @unlink($filena me);
    [/code]

    regards,
    Psh:)

    Comment

    Working...