How do I refresh a table using ajax - html

I would like to update a table cell for a status obtained from a jsp method. The method takes some time, hence it isn't instant. Therefore I need ajax to refresh the page to get an updated status once the method completes
<script type="text/javascript">
function updateTable()
{
var xmlhttp;
var table = document.getElementById('theTable');
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('theTable').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open(???);
xmlhttp.send();
}
//Refresh every 2 seconds
setInterval( doStuff, 2000 );
</script>
<!-- The actual Table needed to updated -->
</TABLE>
<TABLE id="theTable">
<TH>status</TH>
<tr>
<td><%=status%></td>
</tr>
</table>

On the server you need a jsp page that generates the table data when it is invoked. Say this is called generatetable.jsp. You can then use that URL in the open() method to invoke the jsp page.
Your callback function will handle replacing the current table data with the new table data when it is received.

Related

AJAX how to create a new element?

i've been using an online tutorial on how to use ajax to create a new element dependent on the users input. At the moment, all it will do is replace an already made div's innerhtml however I would like to create a new div and fill it with the input designated by the user. Here is my current code:
AJAX:
<script>
function showType(str)
{
if (str=="")
{
document.getElementById("Answer").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/ASPScripts/QuestionDesign.asp?q="+str,true);
xmlhttp.send();
}
</script>
ASP
<%
response.expires=-1
dim q
q=request.querystring("q")
response.write("<input type='" & q & "'/>")
%>
Any ideas on how to do this? I'm aware its probably a very simple answer, i'm just very new to this stuff
basically, what you are asking is how to append a new element to the DOM:
var newDiv = document.createElement("div");
newDiv.innherHtml = xmlhttp.responseText;
document.getElementById("newDivParent").appendChild(newDiv);
EDIT: you can paste this code in console (f12 in chrome) right here on this page to see how it works:
var newDiv = document.createElement("div");
newDiv.setAttribute('style', 'position:fixed; top:0; left:0; right:0; bottom:0; z- index:10000000; background-color:red;' )
document.body.appendChild(newDiv);

Simple ajax error driving me crazy

I am simply trying to read a html file inside a div, now it is not working therefore I tried to read just a a simple text file named a.txt, the text file contains 3 lines "asdasdas" something like that.
It just won't work, the function is being called after pressing a paragraph tag, here is the code:
functionNew()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("divfull").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","a.txt",true);
xmlhttp.send(null);
}
any ideas why its not working?
code seem to be proper but try calling
xmlhttp.open("GET","a.txt",true);
before
xmlhttp.onreadystatechange=function()
try and let us know if it work for you

xmlhttp.open GET invalid characters

I thought that changing accents chars with
$GLOBALS['normalizeChars'] = array(
'Á'=>'Á', 'É'=>'É', 'Í'=>'Í', 'Ó'=>'Ó', 'Ú'=>'Ú', 'Ñ'=>'Ñ',
'á'=>'á', 'é'=>'é', 'í'=>'í', 'ó'=>'ó', 'ú'=>'ú', 'ñ'=>'ñ'
);
and this function ...
function MakeIt($toClean){
return strtr($toClean, $GLOBALS['normalizeChars']);
}
... will help me to replace áéíóúñ to a á etc...
But i'm still in trouble.
mainfile.php calls to a get_data.php that read mySQL content which uses MakeIt(); to replace invalid chars. If I load get_data.php into a browser, chars are replaced correctly but when it's loaded from mainfile.php it returns invalid chars.
mainfile.php
<script>
function ShowData(str)
{
if (str=="")
{
document.getElementById("deal_rcpt").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("deal_rcpt").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","get_data.php?event="+str,true);
xmlhttp.send();
}
ShowData(1)
</script>
<div id="deal_rcpt"></div>
I think xmlhttp is not compatible with áéíóúñ chars.

Refreshing list on a page using html form and ajax

The page has a textbox and a submit button. The textbox accepts an item. On adding an item, the list on the page should refresh and display new items using ajax.
The previous items should remain.
the code i've written so far:
1) 123.html:
<html>
<head>
<title>PHP using AJAX</title>
<script type="text/javascript">
var time_variable;
function getXMLObject() //XML OBJECT
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+
}
catch (e2) {
xmlHttp = false // No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers
}
return xmlHttp; // Mandatory Statement returning the ajax object created
}
var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object
function ajaxFunction() {
var getdate = new Date(); //Used to prevent caching during ajax call
if(xmlhttp) {
var txtname = document.getElementById("txtname");
xmlhttp.open("POST","321.php",true); //calling 321.php using POST method
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("txtname=" + txtname.value); //Posting txtname to PHP File
}
}
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
</script>
<body>
<form name="myForm">
<table>
<tr>
<td>Enter Name</td>
<td><input type="text" name="txtname" id="txtname" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="Submit" onclick="ajaxFunction();" /></td>
</tr>
</table>
<div id="message" name="message"></div>
</form>
</body>
</head>
</html>
2)321.php
<?php
$a = $_POST["txtname"];
echo ".$a.";
?>
I am able to get output list of items(what i enter in textbox) on the same page without refreshing.
wanted to know how i could add to the list keeping the previous items intact. i.e. update list keeping previous output
thanks.
DO you wish to write us the code ?
You can get some samples here
http://sillythingsthatmatter.com/ajax/ajax3.php

Javascript array for AJAX POST send

Here is the deal... I need to make an AJAX save script. I have a whole system built on php and every action needs a refresh... I'm trying to minimize the refresh count by using AJAX ... I can't seem to find a way how to send a WYSIWYG editor output without loss to the PHP script...
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
function save(){
xmlhttp.open('POST','action.php',true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", document.getElementById('output').value.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(document.getElementById('output').value);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status==200){
$('#ajaxresult').css('opacity', 0.1);
$('#ajaxresult').stopAll().pause(1000).fadeTo(400,1);
$('#ajaxresult').stopAll().pause(3000).fadeTo(400,0, function(){$(this).hide();});
document.getElementById('ajaxresult').innerHTML=xmlhttp.responseText;
}
}
}
While this script works fine I can't seem to find the way what kind of array to give the send option... what is the syntax or is there something I don't know?
BTW I'm a beginner in JS...
I'd look into using jQuery and it's Ajax library:
http://api.jquery.com/jQuery.ajax/
Instead of doing all that you'd simply do:
$.post({url: 'action.php',data: output,success: function() { /* do something here */ }});
create custom parameter in the javascript code like below:
var jspNameParam = "content="+escape(document.getElementById('output').value);
function myFunction() {
if (xmlhttp) {
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
/* want to accsess some data written from action.php */
}
};
xmlhttp.open("POST", "action.php", true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send(jspNameParam);
}
}
Now in action.php you will get whole content with the parameter name content.