JavaScript cloning XML node problem - javascript

This section of code is to first get the data and display the data from the XML file. After that there is a button below named submit. When I press submit, it calls the function add() and it will copy the first node from the XML file:
<html>
<body>
<link rel="stylesheet" type="text/css" href="test.css" />
<table>
</tr class="top">
<td>ID</td>
<td>Orgin</td>
<td>Type</td>
<td>Color</td>
</tr>
<script type="text/javascript">
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","test.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("product");
for (i=0;i<x.length;i++)
{
document.write("<tr class=a><td>");
document.write(x[i].getElementsByTagName("orgin")[0].getAttribute("id"));
document.write("</td><td>");
document.write(x[i].getElementsByTagName("orgin")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("type")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("color")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
function add()
{var id=document.getElementById('idProduct').value;
var time=document.getElementById('Time').value;
var org=document.getElementById('orgin').value;
var color=document.getElementById('color').value;
var type=document.getElementById('type').value;
xmlDoc=loadXMLDoc("test.xml"); //Problem happen here, the code doesn't functioning
oldNode=xmlDoc.getElementsByTagName('product')[1];
newNode=oldNode.cloneNode(true);
xml.Doc.documentElement.appendChild(newNode);
}
</script>
<br>
Inputs:
<br>
Time: <input type="text" id="time"><br>
ID: <input type="text" id="idProduct"><br>
Orgin: <input type="text" name="orgin"><br>
Type: <input type="text" name="type"><br>
Color: <input type="text" name="color"><br>
<input type="button" value="submit" onclick="add()"></input>
</body>
</html>
This is the XML file:
<?xml version="1.0" encoding="Big5" ?>
<set>
<product time="5">
<orgin id="1">sony</orgin>
<type>comp</type>
<color>red</color>
</product>
<product time="6">
<orgin id="2">apple</orgin>
<type>others</type>
<color>blue</color>
</product>
</set>

Here is the W3Schools loadxmldoc.js file:
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}
Add that to your script block and it may start working (or get you a little further along).

Related

Populate textArea from textfile

I have a simple text file:
Text1
Text2
Text3
TextN
And would like to create an interface where each time the user presses the next button the text area will be populated with the next text. I guess I have to use an array for that but I am having problems changing the text with the next button. Moreover the code I have only works on Firefox. Does this mean that when I put the website on a server this will be interpreted by Firefox clients only?
function test() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
var contents = xmlhttp.responseText;
myArray = contents.split("\n");
}
}
xmlhttp.open("GET", "deeds.txt", true);
xmlhttp.send();
}
function nextDeed() {
document.getElementById('deed').innerHTML = myArray[0]
}
<html>
<body>
<h1 align="center" style="font-family:Arial;">RELATION TAGGING </h1>
<textarea id="deed" rows="15" cols="160"></textarea>
<input type="button" value="next" onclick="nextDeed()" /><br/>
<div id="result">Result</div>
</body>
</html>
step = 0;
$("#next").click(function(){
step = step +1;
$.ajax({
url: "./text.php?"+"step="+step,
success: function(data){
$("#deed").val(data);
}
});
});
;
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h1 align="center" style="font-family:Arial;" >RELATION TAGGING </h1>
<textarea id="deed" rows="15" cols="160">
</textarea>
<input type="button" id="next" value="next" /><br/>
<div id="result">Result</div>
</body>
</html>
PHP code : <?php $data = file_get_contents("file".$_GET["t"].".txt"); echo $data;?>

XHR not hitting 3 - 'Loading'

my XHR object isn't hitting the 'loading' state, 3. I can't figure out what the problem is, as I can't find any syntax issues. It seems to be some kind of code/syntax error, because the error that I'm getting in the console is "Failed to load resource: the server responded with a status of 500 (Internal Server Error)"
Anyway, here's what I have. The above error triggers on the line 'xhr.send(requestBody);' in 'lecturers.js'.
UPDATE
The original code for "lecturers.js" was this:
function getResults(){
xhr.open("GET", "lecturers.php?id=" + Number(new Date), true);
xhr.onreadystatechange = getData;
xhr.send(null);
}
What I want to do is change the above code to use the POST protocol.
lecturers.htm
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Course Lecturers</title>
<script type="text/javascript" src="lecturers.js"></script>
</head>
<body>
<form>
<input type = "button" id="butt1" value = "Press for Results" /><br />
</form>
<script type="text/javascript">
var btnResults = document.getElementById("butt1");
btnResults.onclick = getResults();
</script>
<span id="results" /></span>
</body>
</html>
lecturers.js
var xhr = false;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
} else if(window.ActiveXObject){
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
function getResults(){
if(xhr){
var dataSource = "lecturers.php";
var requestBody = "lecturers.php?id=" + Number(new Date);
xhr.open("POST", dataSource, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = getData;
xhr.send(requestBody);
}
}
function getData ()
{
alert(xhr.readyState);
if ((xhr.readyState == 4) &&(xhr.status == 200))
{
var object = document.getElementById("results");
object.innerHTML = xhr.responseText;
}
}
lecturers.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<Courses>
<Course>
<title>Web Development</title>
<lecturer>Jian Yu</lecturer>
<moderator>Gary Snail</moderator>
<points>25</points>
<group>SCCRL</group>
<Faculty>DCT</Faculty>
</Course>
<Course>
<title>Software Development in Java</title>
<lecturer>SpongeBob SquarePants</lecturer>
<moderator>Patrick Star</moderator>
<points>25</points>
<group>SCCRL</group>
<Faculty>DCT</Faculty>
</Course>
<Course>
<title>Financial Accounting</title>
<lecturer>Squidward Tentacles</lecturer>
<moderator>Mr Crab</moderator>
<points>10</points>
<group>KEDRI</group>
<Faculty>DCT</Faculty>
<Course>
<title>IS Project</title>
<lecturer>Plankton</lecturer>
<moderator>Sandy Cheeks</moderator>
<points>30</points>
<group>IS</group>
<Faculty>DCT</Faculty>
</Course>
</Course>
</Courses>
lecturers.xsl
<?xml version="1.0"?><!-- DWXMLSource="results.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE> Courses</TITLE>
</HEAD>
<BODY>
Courses with More than 15 Credit Points : <BR/>
<table border="1">
<tr>
<th>Title</th>
<th>Group</th>
<th>Faculty</th>
</tr>
<xsl:for-each select="Course">
<tr>
<td><xsl:value-of select="title"></xsl:value-of></td>
<td><xsl:value-of select="group"></xsl:value-of></td>
<td><xsl:value-of select="Faculty"></xsl:value-of></td>
</tr>
</xsl:for-each>
</table>
----------
<BR/>
Number of Courses Offered by CSSE :
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
lecturers.php
<?php
$xmlDoc = new DomDocument;
$xmlDoc->load("lecturers.xml");
$xslDoc = new DomDocument;
$xslDoc->load("lecturers.xsl");
$proc = new XSLTProcessor;
$proc->importStyleSheet($xslDoc);
echo $proc->transformToXML($xmlDoc);
?>

Not getting forward for the page in Firefox

I am developing a simple log in form by using XML file values. I have an XML with values last name and first name. I am accessing the file and checking the values against the given values in order to validate log in.
It works fine in chrome, IE and safari. But, In mozilla it is not getting forwarded to the next page. The page just stays in the same page and shows a loading URL icon for indefinite time.
Here is my CODE:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<link href="css/stylemp3.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form onsubmit="return myFunction()" action="action.html">
First name:<br/>
<input type="text" required="required" name="firstname" id="firstname"/>
<br>
Last name:<br>
<input type="password" required="required" name="lastname" id="lastname"/>
<br/>
<select class="dropdown" id="ddl">
<option value="haha" selected="selected">show</option>
<option value="hihi" >hide</option>
</select>
<br/>
<input type="text" name="hide" id="hidee" class="hide"/>
<br/>
<input button type="submit" value="Submit"/>
</form>
<div></div>
<script>
window.onload = function() {
document.getElementById("firstname").focus();
};
$( "#ddl" )
.change(function () {
if($( "select option:selected" ).text() == "hide")
{
$("#hidee").hide();
}
else
{
$("#hidee").show();
}
})
.change();
</script>
<script>
function myFunction() {
var lastname = document.getElementsByName('lastname')[0].value;
var firstname = document.getElementsByName('firstname')[0].value;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","login.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("login");
for (i=0;i<x.length;i++)
{
alert(x[i].getElementsByTagName("firstname")[0].childNodes[0].nodeValue);
if((x[i].getElementsByTagName("firstname")[0].childNodes[0].nodeValue) == firstname)
{
alert("Smileeee.....:-))");
if((x[i].getElementsByTagName("lastname")[0].childNodes[0].nodeValue) == lastname)
{
alert("login Successful!!!!!");
return true;
break;
}
}
}
return false;
}
</script>
</body>
</html>

updating a xml file using javascript and jsp

we are making a web UI in JSP and using a bit of Javascript .
I need to load the xml file contents in text box and allow to edit it and write the changes to xml file.
In my code i am able to edit it but the changes are not written back to the xml file. I am putting my .jsp file below..
.jsp file
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse:collapse;
}
th, td {
padding: 5px;
}
</style>
</head>
<body>
Title: <input type="text" id="title" value=""><br>
Id: <input type="text" id="id" value=""><br>
Updated: <input type="text" id="updated" value=""><br>
<button onclick="myFunction()">Save Changes</button>
<script>
//if (window.XMLHttpRequest)
//{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
//}
//else
//{// code for IE6, IE5
//xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
//}
xmlhttp.open("GET","values.xml",false);
try {
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
}
catch(err) {
alert( err.message);
}
//document.write("<table><tr><th>Title</th><th>Updated</th><th>Id</th></tr>");
var x=xmlDoc.getElementsByTagName("feed");
for (i=0;i<x.length;i++)
{
document.getElementById("title").value=x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
document.getElementById("id").value=x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
document.getElementById("updated").value=x[i].getElementsByTagName("updated")[0].childNodes[0].nodeValue;
/*document.write("<tr><td>");
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("updated")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
*/
}
//document.write("</table>");
function myFunction() {
try{
//document.getElementById("title").value="Hello";
x[0].getElementsByTagName("title")[0].childNodes[0].nodeValue = document.getElementById("title").value;
x[0].getElementsByTagName("id")[0].childNodes[0].nodeValue = document.getElementById("id").value;
x[0].getElementsByTagName("updated")[0].childNodes[0].nodeValue = document.getElementById("updated").value;
//document.write("<table><tr><th>Title</th><th>Updated</th><th>Id</th></tr>");
document.write("<p>");
document.write(x[0].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("</p><p>");
document.write(x[0].getElementsByTagName("updated")[0].childNodes[0].nodeValue);
document.write("</p><p>");
document.write(x[0].getElementsByTagName("id")[0].childNodes[0].nodeValue);
document.write("</p>");
xmlDoc.save("values.xml");
return(xmlDoc);
}
catch(err) {
alert( err.message);
}
//document.write("</table>");
}
</script>
</body>
</html>
.xml file
<?xml version="1.0" encoding="utf-8"?>
Example Feed
2003-12-13T18:30:02Z
urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6
Helo work

Textarea - missing new line

I have following code:
<form action="../p/padd.php" method="POST">
<input type="button" value="Náhľad" OnClick="javascript:nahlad()" />
<textarea tabindex="4" id="textra" name="text" ></textarea>
<input type="submit" value="Vložiť" />
</form>
<span id="nahlad"> </span>
<script>
function nahlad()
{
var textra = document.getElementById("textra").value;
alert (textra);
var xmlhttp;
if (window.XMLHttpRequest) {xmlhttp=new XMLHttpRequest();
}
else
{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('nahlad').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","ssnahlad.php?text=" + textra,true);
xmlhttp.send();
}
</script>
When I enter the following into the textarea (yes there is "new line" with enter)
asd
asd
and click the Nahlad button
ssnahlad.php contains
<?php
$new = $_GET['text'];
echo nl2br($new);
?>
so why does the span with id=nahlad contain
asdasd
instead of
asd
asd
Now that I'm at home and can test, this will work:
var newText = encodeURIComponent(textra);
using encodeURIComponent before you send it will correctly render your output, with no decoding on the server side

Categories