469,336 Members | 6,193 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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.



Expand|Select|Wrap|Line Numbers
  1. Code: ( text )
  2. (javascript)
  3. function addToList_city(city) 
  4. {
  5. var c =document.assess_form.cit.value;
  6.  
  7. var fso = new ActiveXObject("Scripting.FileSystemObject");
  8. var s = fso.CreateTextFile("C://test.txt", true);
  9. s.write(c)
  10.  
  11. }


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

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



)


[HTML]<html>
<form name = assess_form.php>
<select name="cit" onchange="addToList_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]
Mar 25 '08 #1
1 949
pshm
20
hi,
you need to go for AJAX... I think this will help you.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3.     var xmlHttpObj;
  4.  
  5.     function getXMLHttpObject()
  6.     {
  7.         var xmlHttp=null;
  8.  
  9.         try{
  10.  
  11.             xmlHttp=new XMLHttpRequest();
  12.         }catch (e){
  13.  
  14.             try{
  15.                 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  16.             }catch (e){
  17.                 try{
  18.                     xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  19.                 }catch (e){
  20.                     alert("Your browser does not support AJAX!");
  21.                     return false;
  22.                 }
  23.             }
  24.         }                
  25.         return xmlHttp;
  26.       }       
  27.  
  28.       function getTextFromPHP(){                                           
  29.  
  30.             try{
  31.  
  32.                 xmlHttpObj=getXMLHttpObject();                
  33.  
  34.                 if(xmlHttpObj == null){
  35.                     alert('Initialization/ObjectCreation faild');
  36.                     return false;
  37.                 } 
  38.  
  39.  
  40.                 xmlHttpObj.onreadystatechange=changeState;
  41.  
  42.             }catch(err){
  43.                 alert('Error :' + err.description);
  44.             }
  45.  
  46.         var strURL="textLoad.php";  // url for the .php file here
  47.  
  48.  
  49.             xmlHttpObj.open("GET",strURL,true);
  50.             xmlHttpObj.send(null);
  51.  
  52.             return false;
  53.       }
  54.  
  55.     function changeState(){  
  56.         if(xmlHttpObj == null){
  57.             alert('Initialization faild');
  58.             return false;
  59.         } 
  60.  
  61.         switch(xmlHttpObj.readyState){
  62.             case 0:
  63.                 javascript:window.status='Current Status :The request is not initialized';
  64.                 break;
  65.  
  66.             case 1:
  67.                 javascript:window.status='Current Status :The request has been set up';
  68.                 break;
  69.  
  70.             case 2:
  71.                 javascript:window.status='Current Status :The request has been sent';
  72.                 break;
  73.  
  74.             case 3:
  75.                 javascript:window.status='Current Status :The request is in process';
  76.                 break;
  77.  
  78.             case 4:
  79.                 javascript:window.status='Current Status :The request is complete';                
  80.                 document.getElementById('city').value = xmlHttpObj.responseText;               
  81.  
  82.                 break;
  83.         }                               
  84.         return true;
  85.     }   
  86. //-->
  87. </script>
  88.  
  89. <body> 
  90. <form name = "assess_form.php" action="#"> 
  91. <select name="cit" onchange="getTextFromPHP();"> 
  92. <option value="h"> Haryana </option> 
  93. <option value = "c">Chennai</option> 
  94. </select> 
  95. <input type="text" name="city" id="city" />
  96. </form> 
  97. </body>
  98.  
a small change in php code

Expand|Select|Wrap|Line Numbers
  1. // file name : textLoad.php
  2. $filename = "c://test.txt";
  3. if (file_exists($filename)) {
  4.  
  5. $fp = fopen($filename,'r');
  6. $citfile= fread($fp, filesize($filename));
  7. echo $citfile
  8.         }
  9. @unlink($filename);
  10.  
regards,
Psh:)
Mar 25 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Troy | last post: by
reply views Thread by Prashant | last post: by
2 posts views Thread by Schraalhans Keukenmeester | last post: by
7 posts views Thread by Arne Beruldsen | last post: by
14 posts views Thread by Professor Yonce | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.