Textarea - missing new line - javascript

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

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;?>

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>

Textarea is null when posting via AJAX with plain javascript

When I try to post this textarea, it sends nothing and obviously returns nothing for that field.
All other input fields are fine.
I've read and everyone says to make sure .value is added after getelementid, which I was doing anyhow, so I'm not sure what is wrong.
I'm using Bootstrap 3, in case that matters.
Update: I just did a test and hard-coded text into the textarea field. That successfully passed. But any text that I type into the form does NOT get passed. I think this might be a Bootstrap 3 bug, perhaps.
Does anyone else know?
Javascript:
var http_request = false;
function show_hint(p_hint_text, p_span) {
document.getElementById(p_span).innerHTML = p_hint_text;
}
function makePOSTRequest(url, parameters, SpanName) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function () {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById(SpanName).innerHTML = result;
document.getElementById(StatusName).innerHTML = 'Ready';
} else {
alert('There was a problem with the request.');
}
}
};
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
alert(parameters);
http_request.send(parameters);
}
function Contact(obj, SpanName, StatusName) {
var curDateTime = new Date(); //For IE
var poststr = "gid=" + encodeURI(document.getElementById("gid").value) +
"&tid=" + encodeURI(document.getElementById("tid").value) +
"&cid=" + encodeURI(document.getElementById("cid").value) +
"&recid=" + encodeURI(document.getElementById("recid").value) +
"&item_ip=" + encodeURI(document.getElementById("item_ip").value) +
"&item_userid=" + encodeURI(document.getElementById("item_userid").value) +
"&item_author=" + encodeURI(document.getElementById("item_author").value) +
"&fromajax=" + encodeURI(document.getElementById("fromajax").value) +
"&action=" + encodeURI(document.getElementById("action").value) +
"&active=" + encodeURI(document.getElementById("active").value) +
"&message=" + encodeURI(document.getElementById("message").value);
var SpanName = SpanName;
var StatusName = StatusName;
//alert (SpanName);
alert(poststr);
makePOSTRequest('http://fakedomain.com', poststr, SpanName);
}
HTML
<span id="ContactFormSpanQXBIAHOBEU25">
<span id="statusQXBIAHOBEU25">
<form role="form" action="javascript:Contact(document.getElementById('ContactForm'),'ContactFormSpanQXBIAHOBEU25','statusQXBIAHOBEU25'); show_hint('Sending Data, Please Wait...', 'statusQXBIAHOBEU25');" method="post" name="ContactForm" id="ContactForm">
<input name="gid" id="gid" type="hidden" value="SDZOAYLTDV26">
<input name="tid" id="tid" type="hidden" value="ZBSAGNSCFM55">
<input name="cid" id="cid" type="hidden" value="2">
<input name="recid" id="recid" type="hidden" value="QXBIAHOBEU25">
<input name="item_IP" id="item_ip" type="hidden" value="555.555.555.555">
<input name="item_userid" id="item_userid" type="hidden" value="YCGLNBFZPD13">
<input name="item_author" id="item_author" type="hidden" value="YCGLNBFZPD13">
<input name="fromajax" id="fromajax" type="hidden" value="1">
<input name="action" id="action" type="hidden" value="add">
<input name="active" id="active" type="hidden" value="1">
<div class="form-group">
<textarea class="form-control" name="message" id="message" rows="5"></textarea>
</div>
<input name="Submit2" type="submit" class="btn btn-success btn-flat" value="Submit" />
</form>
</span>
</span>

Function not passing value as argument

I can't figure out why the getMoreInfoResults() function doesn't pass on the value of the radio button (when one is selected) to the GET request. Anyone know why?
<form name="question_form">
<input type="radio" name="vote" value="1" onclick="getVote(this.value)" />Yes<br />
<input type="radio" name="vote" value="2" onclick="getVote(this.value)" />No<br />
<textarea rows="3" name="moreInfo" onkeyup="getMoreInfoResults(document.question_form.vote.value, this.value)" /></textarea><br />
<input type="submit" value="Submit" />
<div id="otherAnswers"></div>
</form>
This is my javascript:
function getMoreInfoResults(vote, input) {
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("otherAnswers").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","phpPoll/phpPoll_userDefined/functions/getMoreInfoResults.php?vote=" + vote + "&moreInfo=" + input,true);
xmlhttp.send();
}
Thanks.
document.question_form.vote expression will give you a NodeList object, not a Node one. Obviously, its value property is undefined.
One possible workaround is to create a function that will retrieve value of the checked radio button:
function getCheckedValue(radioNodes) {
for (var i = 0, l = radioNodes.length; i < l; i++) {
if (radioNodes[i].checked) {
return radioNodes[i].value;
}
}
}
... and use it instead of querying the value directly:
onkeyup="getMoreInfoResults(getCheckedValue(document.question_form.vote), this.value)"

JavaScript cloning XML node problem

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).

Categories