how to use xmlhttprequest object's post method for sending data - javascript

I want to send data that is in a java script variable to the server.the variable is in a method that is executing when I click a button on the web site.here is the code written in that method for sending data.
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.open("POST","new_map.php",true);
xmlhttp.send(cur_entry_string);
and here is code written in new_map.php file for getting data.here cur_entry_string is the variable that is holding that data.
$massage = $_POST[cur_entry_string];
but this is not working..:(...I am using eclipse.

Maybe you should have a look at an AJAX tutorial http://www.w3schools.com/ajax/default.asp and if that is not what you want to do you can also look into JSON

You need to actually generate a valid query string. A query string in POST looks the same as a GET string.
Something like this should work:
xmlhttp.send('cur_entry_string=' + cur_entry_string);
I would recommend using a library such as jQuery for using Ajax, as it simplifies the process a lot so you don't need to do error prone things like these query string things yourself.
ps. note that you should enclose array string indices in quotes when using PHP:
$_POST['cur_entry_string']

Related

Can't get xmlhttp.responseText value

I am trying to get the xmlhttp.responseText value, but I have no response from the xmlhttp. I was wondering if there is something wrong with my code:
SCRIPT
var xmlhttp;
function show(){
loadXMLDoc("includes/edit.php",function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
alert(xmlhttp.responseText);
}
});
}
function loadXMLDoc(url,cfunc){
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=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
HTML
< a href="#" style="text-align: right; font-size: 10px;" onclick="show()">SHOW</a>
EDIT.PHP
print "1";
I don't see anything wrong with your code per se, but there are a lot of interactions that are opaque from this code snippet.
I can offer some troubleshooting tips, however:
Add a console log in your callback function (the one that you are running alert() in). Just "console.log('function is running');" on the first line. Then see if you get that far. I would put one in the opening line of show() as well -- maybe your click event isn't doing what you think it is.
Have your PHP server-side file write something to a log so you can be sure that the request is being received where you think it is.
Open your browser developer console and watch the network panel. You should be able to see the entire XHR transaction there. Is it being sent at all? If so, is it returning the content you expect?
If all this doesn't help, setup a test in jsfiddle and post the link here. We'll be able to help in a lot more detail that way.
Good luck.

Tracking visitor with javascript

I have one master domain : www.domain-A.com
I have some domains that call a javascript file on this principal domain.
For example, www.domain-B.com use the file www.domain-A.com/file.js
In this file.js script, I'm trying to put cookie (that's ok) and send some data to www.domain-A.com like the query string, the referrer and the user agent.
I've use that code in my 'file.js' (found on W3School website) :
//Envoi des données
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 json = xmlhttp.responseText;
}
}
xmlhttp.open("POST","http://www.domaine-A.com/script.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(urlParams + "&" + infParams + "&" + u);
It's working very well on FF and Chrome. But, not on IE because I have this error :
This page is accessing information not under its control
So, I've look a little how analytics send data to Google and i seem's use a GIF or something like that but i don't know how to do it in the good way.
My goal is send data from domain-x.com to domain-a.com with javascript.
What version of GA are you using? If you are on analytics.js (Universal Analtyics) You can use ajax and the measurement protocol to call page views on the other site. The measurement protocol lets you make HTTP requests and send raw data right into to GA.
I have used it before to track interaction with interactive infographic that I had on another site.
For your case you can use the following paramaters
www.google-analytics.com/collect
v=1 // Version.
&tid=UA-XXXX-Y // Tracking ID / Web property / Property ID.
&cid=555 // Anonymous Client ID.
&t=pageview // Pageview hit type.
&dh=mydemo.com // Document hostname.
&dp=/home // Page.
&dt=homepage // Title.

Javascript is working on Safari, but fails on others

There is some problem that I can't find out why the code worked on Safari, but failed on other browsers.
There is the html part
and the main javascript part.
The main problem that I find is:
While executing the function downloadurl(url, function), cannot find the XML tags "Info", and the markers array length is 0 on many browsers. However, it's ok on Safari. The part of javascript code is like:
downloadUrl("http://travel-taoyuan.tycg.gov.tw/content/travel/xmlcontentlist1.aspx", function(doc) {
var xml = xmlParse(doc);
var markers = xml.documentElement.getElementsByTagName("Info");
......
To alert markers, it will return "0".
And actually it should be "174"(Safari's result).
Thanks for answering my question.
hmmm, seems to be a syntax error. Different browsers will use a different syntax, and so it won't execute (or at best, not properly if your lucky).
Try something along the lines of....
<script type="text/javascript">
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://travel-taoyuan.tycg.gov.tw/content/travel/xmlcontentlist1.aspx",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("theMainTagName"); //TODO: this is the main tag ^(see note)
for (i=0;i<x.length;i++)
{
//TODO: code to handle each xml element
//this is the code to get the value from a particular tag: x[i].getElementsByTagName("theTagName")[0].childNodes[0].nodeValue
}
</script>
NOTE: ^the 'main' tag is, for example, in the case of this xml document, the 'main' tag is 'CATALOG'
Hope this helps. Sorry if it's not very clear, been a while since I worked with xml/javascript. Comment if you want further explanation

Cant get node value in XML, how can I get it?

Im currently using a simple looped XML script from on w3cschools and it works great:
if (p1p2total == 5)
{
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","xml/SearchRequest-12437.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("item");
for (i=0;i<x.length;i++)
{
document.write("<tr><td class='c3c1'>");
document.write(x[i].getElementsByTagName("key")[0].childNodes[0].nodeValue);
document.write("</td><td class='c3c2b'>");
document.write(x[i].getElementsByTagName("priority")[0].childNodes[0].nodeValue);
document.write("</td><td class='c3c3b'>");
document.write(x[i].getElementsByTagName("customfields")[0].getElementsByTagName("customfield")
[0].getAttributeNode("id").textContent);
document.write("</td></tr>");
}
}
However on my third node value query, customfields, im trying to obtain the textContent of a node with the following path in my xml:
item -> customfields -> customfield id=10080 -> customfieldvalue
How can I get the textContent value of customfieldvalue? The parent node has to have an attribute of id=10080 because different items have a varying amount of customfield tags that have different childnode ids.
Please help!!!
I only need this to work in FireFox...
---EDIT---
Ok so I figured out that the xpath I will need to use is:
./customfields/customfield[#id='customfield_10080']/customfieldvalues/customfieldvalue/text()
So how can I replace the line
document.write(x[i].getElementsByTagName("customfields")[0].getElementsByTagName("customfield")[0].getAttributeNode("id").textContent);
to pull the textcontents of the Xpath I provided while remaining in the javascript loop? I need this to pull this Xpath for each
As far as I'm concerned you only have to call
document.write(x[i].getElementsByTagName("customfields")[0].getElementsByTagName("customfield").text;
Give it a try. There are several ways of reading and parsing XML from javascript. I wouldn't recommend w3schools for learning. Go to the roots, try MDN for the next time ;)
I hope it helps

Load html file into a div with javascript?

I have a div which I want to populate with different html files from my server. Is there a simple way to do this? All I ever find are jquery samples and I don't want to use a library.
I have tried this:
document.getElementById('main').innerHTML = 'menu.html';
But that obviously just loaded text!
Some simple Ajax will do the trick for you. This is untested, but should give you the right idea:
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("main").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","menu.html",true);
xmlhttp.send();
Simplest way is to use a library, which the entire point of, they wrote the code for u. right?
Use the jQuery example. It is as good as it gets.
Is there a simple way? No, not without a library. That being said, you can do it on your own if you choose - that might be all that's necessary for you to come crawling back into the arms of jQuery ;)
Resource: XHR on Mozilla Developer Network

Categories

Resources