2

So I have this HTML file that tests the user's screen resolution, and plugins installed using Javascript. So when the user accesses the page it sees: (e.g.) Your current screen resolution is 1024x768 and you have the following plugins installed: Plug-in No.2- Java Deployment Toolkit 7.0.10.8 [Location: npdeployJava1.dll], Plug-in No.3- Java(TM) Platform SE 7 U1 [Location: npjp2.dll], Plug-in No.4- Microsoft Office 2003 [Location: NPOFFICE.DLL]... I also need to save this information in a file on the server. All users are having firefox or chrome. How do I do this using AJAX?

<html>
<body>
<script language="JavaScript1.2">
document.write("Your current resolution is "+screen.width+"*"+screen.height+"")
</script>
<BR><BR>
<SCRIPT LANGUAGE="JavaScript">
var num_of_plugins = navigator.plugins.length;
for (var i=0; i < num_of_plugins; i++) {
var list_number=i+1;
document.write("<font color=red>Plug-in No." + list_number + "- </font>"+navigator.plugins[i].name+" <br>[Location: " + navigator.plugins[i].filename + "]<p>");
}
</script>
</body>
</html>

Thanks

1
  • Hello alpinemobile. What did you end up doing? Can you mark the correct answer? Commented May 14, 2014 at 9:30

3 Answers 3

7

Without jQuery (raw JavaScript):

    var data = "...";// this is your data that you want to pass to the server (could be json)
    //next you would initiate a XMLHTTPRequest as following (could be more advanced):
    var url = "get_data.php";//your url to the server side file that will receive the data.
    var http = new XMLHttpRequest();
    http.open("POST", url, true);

    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", data.length);
    http.setRequestHeader("Connection", "close");

    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);//check if the data was received successfully.
        }
    }
    http.send(data);

Using jQuery:

$.ajax({
  type: 'POST',
  url: url,//url of receiver file on server
  data: data, //your data
  success: success, //callback when ajax request finishes
  dataType: dataType //text/json...
});

I hope this helps :)

More info:

Sign up to request clarification or add additional context in comments.

5 Comments

I don't know JQuery. I'll try what you have posted and report back. Anyway thank you for your help.
we still have to rely on the same old php in the backend for doing this. Node.JS can be used in the backend if the same is done ONLY in javascript.
Awesome answer!!
Could anyone explain the raw JavaScript answer to me please? I don't understand how that would write to the server file. And what would params be?
@avisk this is the "vanila" (normal js form) to send an ajax request (a request without refreshing the page) - so the server will receive a normal HTTP request and do what it pleases. Is that more understandable?
1

Do you know jQuery? It will be much easier with jQuery.

var data = "";
for (var i=0; i < num_of_plugins; i++) {
   var list_number=i+1;
   document.write("<font color=red>Plug-in No." + list_number + "- </font>"+navigator.plugins[i].name+" <br>[Location: " + navigator.plugins[i].filename + "]<p>");
   data += "<font color=red>Plug-in No." + list_number + "- </font>"+navigator.plugins[i].name+" <br>[Location: " + navigator.plugins[i].filename + "]<p>"; 
}

$.post('savedata.php', {data=data}, function(){//Save complete});

Then in savedata.php you can write something like the following:

$data = $_POST['data'];
$f = fopen('file', 'w+');
fwrite(f, $data);
fclose($f);

Comments

0

Do a request from javascript to a page which runs server side code.

Send post request with ajax http://www.javascriptkit.com/dhtmltutors/ajaxgetpost.shtml to for example an apsx page. From aspx you could save it to a text file or database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.