By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,745 Members | 1,576 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,745 IT Pros & Developers. It's quick & easy.

Using Ajax to create a button to save a text???

P: 8
I am very newbie in ajax, so please forgive me if my question are so dump.
I want to create a box where user can type a message and when they click "Save" button. That text will be save. I got the function writefile to work. I named it writefile.asp. But my problem is creating ajax to implement the writefile.asp when user click on "Save" button. Here is what I got so far. Please help
Here is my form.asp
<%@Language=JavaScript%>
Expand|Select|Wrap|Line Numbers
  1. <%@Language=JavaScript%>
  2. <html>
  3. <body>
  4. <%
  5. function Save(){
  6.       var xmlhttp;
  7.       try{
  8.           xmlhttp = new XMLHttpRequest();
  9.       }catch(e){
  10.           xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
  11.       }
  12.     xmlhttp.open("GET", "writefile.asp", true);
  13.       xmlhttp.onreadystatechange = function(){
  14.           if(xmlhttp.readyState == 4){
  15.               Response.Write("Message's Detail Has Been Successfully Saved");
  16.           }
  17.  
  18.       }
  19.       xmlhttp.send(null);
  20.   }
  21. %>
  22. <textarea rows="20" name="message" cols="20"></textarea>
  23. <p>
  24. <input type="button" value="Save Detail" name="saveDetail" onClick="Save();">
  25. </p>
  26. </body>
  27. </html>
  28.  
  29.  
  30.  
Jun 13 '07 #1
Share this Question
Share on Google+
5 Replies


epots9
Expert 100+
P: 1,351
what does your writefile.asp look like?
Jun 13 '07 #2

P: 8
it may sound weird but, writefile.asp is written in VBscript not javascript, since javascript does not support write to file. Here is the code. Both writefile.asp and form.asp is in the same folder
Expand|Select|Wrap|Line Numbers
  1.  
  2. <%
  3.  
  4.     dim filesys, filetxt, messageDetail
  5.     Const ForReading = 1, ForWriting = 2, ForAppending = 8
  6.     Set filesys = CreateObject("Scripting.FileSystemObject")
  7.     Set filetxt = filesys.OpenTextFile("c:\CiscoIPServices\ASP\push2phone\alert.xml", ForWriting, True)
  8.  
  9.     messageDetail = Request.Form("detail").Item
  10.     message = Request.Form("message").Item
  11.     phoneDetail = Request.Form("phones").Item;
  12.  
  13.     filetxt.WriteLine("<CiscoIPPhoneText>")
  14.     filetxt.WriteLine("<Title>Alert</Title>")
  15.     filetxt.WriteLine("<Prompt>Select an option</Prompt>")
  16.     filetxt.Write("<Text>")
  17.     filetxt.Write(messageDetail)
  18.     filetxt.WriteLine("</Text>")
  19.     filetxt.WriteLine("</CiscoIPPhoneText>")
  20.     filetxt.Close
  21.  
  22. %>
  23.  
  24.  
  25.  
  26.  
  27.  
Jun 13 '07 #3

acoder
Expert Mod 15k+
P: 16,027
You have two problems. On line 15 of your Ajax code, you have used ASP code. You need to convert that to Javascript, maybe an alert will do.

Secondly, you're just calling the file, but not passing it anything. You need to pass some info. when making a request.
Jun 14 '07 #4

P: 8
You have two problems. On line 15 of your Ajax code, you have used ASP code. You need to convert that to Javascript, maybe an alert will do.

Secondly, you're just calling the file, but not passing it anything. You need to pass some info. when making a request.

will you explain it a bit more. What do I have to pass there? I call the function Save() when there is onClick. But I dont think it ever get inside the function. What is going on here?
Jun 14 '07 #5

acoder
Expert Mod 15k+
P: 16,027
"writefile.asp" needs to have message passed to it, so that it can be saved, e.g. "writefile.asp?message=blahblah".

You may want to consider making a POST request instead where you would send message in the send method (instead of null).

Give the textarea message an id, e.g. "message" and access it using:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('message').value;
Jun 15 '07 #6

Post your reply

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