How to pass input file name to xmlhttp.open as variable - javascript

I want to pass name of selected file. It works if i put "filename" instead url. Here is my test code. Please help me to get this working.
<input id="upload" type="file" />
<script>
document.getElementById('upload').onchange = uploadOnChange;
function uploadOnChange() {
var filename = this.value;
var lastIndex = filename.lastIndexOf("\\");
if (lastIndex >= 0) {
filename = filename.substring(lastIndex + 1);
}
var url='"'+filename+'"';
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",url,false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var tag=xmlDoc.getElementsByTagName("tag");
}
</script>

Related

document.getElementById innerHTML Not working in loop

Hy ! I am trying to put the result of an XMLHttpRequest inside a div using document.getElementById innerHTML without success.
In the head :
XMLHttpRequest is fine (I tried document.write and it show my results).
Here is my code :
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","http://www.mywebsite.com/my.php",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
for(var i=0; i < xmlDoc.getElementsByTagName("place").length; i++) {
name = xmlDoc.getElementsByTagName("name")[i].childNodes[0].nodeValue;
document.getElementById('name').innerHTML += name +"<br>";
}
In the body :
<div id="name"></div>
NB :
my.php generate xml file and replacing the line document.getElementById... by document.write(name + "br tag"); is working fine.
Thanks in advance for help.
Ok, I found my mistakes and here is the working code :
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) {
xmlDoc=xmlhttp.responseXML;
for(var i=0; i < xmlDoc.getElementsByTagName("place").length; i++) {
var xmlName = xmlDoc.getElementsByTagName("name")[i].childNodes[0].nodeValue;
document.getElementById('name').innerHTML += xmlName +"<br>"
}
}
}
xmlhttp.open("GET","http://mywebsite.com/xmlcreate.php",true);
xmlhttp.send();
Thanks for helps !

Ajax call to get Data

Dear I want to get Variable Value form PHP Value, and I want to write an ajax call in Java Script, tags,
How it is possible,
I want to get Value from get_result.php file,
and I have following code in get_result.php:
<?php
echo $val="abc";
?>
and I wrote follwing code in another file named ajax.php:
<script>
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var url="<?php echo "get_result.php"; ?>";
xmlhttp.open("GET", url, false);
xmlhttp.send(null);
var ok xmlhttp.responseText;
alert(ok);
</script>
is there any bug and Where?
I want to get value from get_result.php file and show this value in an Alert;
You can use jQuery. but for your code, you didn't assign the response
<script>
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var url="get_result.php";
xmlhttp.open("GET", url, false);
xmlhttp.send(null);
var ok = xmlhttp.responseText;
alert(ok);
The below code will help you in returning the value from the php page
<script>
function myfun()
{
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)
{
var demo=xmlhttp.responseText;
alert(demo);
}
}
xmlhttp.open("GET","result.php",true);
xmlhttp.send();
}
</script>
body
<body>
<input type="button" onclick="myfun();"/>
</body>

How to add a file extension to a user input field with javascript

Im trying to retrieve a txt doc from server with ajax request. The name of the txt doc depends on a text input on html doc. Basically I want to append .txt to the end of the input field following an onclick event
// JavaScript Document
function getData(){
var xmlhttp;
var user=document.getElementById("nameDetails").value;
var userText = user + ".txt"; //**not the solution
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("userSubmit").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","userText",true);
xmlhttp.send();
}
If you want to append .txt to the input field itself, you could try this:
document.getElementById("nameDetails").value = document.getElementById("nameDetails").value + ".txt";
or the short form:
document.getElementById("nameDetails").value += ".txt";

Automatically pass the retrieved value from a javascript function to another javascript function?

I have two inline java script functions, one intended to get a token value using xhr and second one will use that value to get the full response from server. But when i uploaded the same on server nothing happened.
Here goes the sample code:
function getUser(user)
{
var xmlhttp;
var result,x,i;
var uservalue=user;
var url="http://abc.xyz.com:8090/uauth/" +uservalue;
///here starts the getToken method..
var tokensave=function getToken(uservalue)
{
var xmlhttp;
var token,a,b;
var url="http://abc.xyz.com:8090/uauth/" +uservalue;
var data="op=login&pass=" +uservalue;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST",url, true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseXML;
token="";
a=xmlDoc.getElementsByTagName("token");
token=token + a.childNodes.nodeValue;
document.getElementById("myDiv").innerHTML=token;
}
};
xmlhttp.setRequestHeader("Content-Type", "text/xml");
xmlhttp.send(data);
return token;
}
var header="Xyz-Authorization: "+tokensave;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",url, true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
xmlDoc=xmlhttp.responseXML;
result="";
x=xmlDoc.getElementsByTagName("response");
for (i=0;i<x.length;i++)
{
result=result + x[i].childNodes[0].nodeValue + "<br />";
}
document.getElementById("myNewDiv").innerHTML=result;
}
};
xmlhttp.setRequestHeader("Xyz-Authorization: ", +tokensave);
xmlhttp.send();
}
HTML CODE:
<div id="myDiv"></div>
<br />
<br />
<div id="myNewDiv"></div >
<br />
<form>
<input name="textbox" id="textbox" type="text" />
<input name="buttonExecute" onclick="getUser(document.getElementById('textbox').value)" type="button" value="Get User" />
</form>
What's wrong with this apparoach?
Thanks in advance for your valuable help...
mrana
You need to use a return token in the first function and use it like this getUser(getToken(user))
Just create a global variable, and you set change the value inside de function but it's still accesible from a global scope for all the functions you want.
var data = false;
function getData() {
// some ajax...
data = {cool:true};
}
function foo () {
if(data.cool) {
alert (data.cool)
}
}

Dynamically populate dynamically created select list

I'm having a bit of difficulty here; I am using js to dynamically create select boxes, but I need Ajax to fill them with options. So far, my code is returning undefined, and I frankly don't know what to do here. My php returns the info fine, but the js isn't parsing it. Another set of eyes, or another brain full of knowledge would be appreciated here;
function getSkilllist(group) {
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) {
return xmlhttp.responseText;
}
}
xmlhttp.open("GET","skl_lst_gen2.php?group=" + group + "&t=" + Math.random(),true);
xmlhttp.send();
}
function addInput(divName,group) {
var skillst = getSkilllist(group);
var newdiv = document.createElement('div');
newdiv.innerHTML = '<select name="ski[]">' + skillst + '</select> .....
The rest of the function is fine, but the var skillst is returning undefined as stated, and I can't figure out why. I assume it has something to do with strings, but I can't figure out what needs to be done.
Your function does not return anything, which is why it isn't working. Try this:
function getSkilllist(group) {
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) {
var newdiv = document.createElement('div');
newdiv.innerHTML = '<select name="ski[]">' + xmlhttp.responseText + '</select> .....
//place in DOM here
}
}
xmlhttp.open("GET","skl_lst_gen2.php?group=" + group + "&t=" + Math.random(),true);
xmlhttp.send();
}
function addInput(divName,group) {
getSkilllist(group);

Categories