Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys, i have a project where i am building a website where the client will have to create an account so i have to save data from the sign up form. I am newbie to this kind of programming and i am trying about a week now to understand how to do it with json and ajax(i must not use jquery, only ajax json and javascript) unsuccesfully. Can anyone of you explain to me simply the basics?Am i supposed to save data in a txt file, in a xml?Will the data be like a string or an array? I feel so confused. Sorry for my bad english and thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 27-Jan-15 20:25pm    
What's the problem? What have you tried so far?
—SA
Member 10482066 27-Jan-15 20:39pm    
Sorry for being so general but the fact is that i am really confused. I have tried doing it with xml with no success and now i am trying with json. I dont understand if i have to save the data as a string or in an array. I also saw a lot of times this code http_request.open("GET", url, true);, but i can't understand what kind of url i have to use.
Sergey Alexandrovich Kryukov 27-Jan-15 20:48pm    
Any kind of URL, it's up to you, depends on what you want to achieve.

Where is AJAX here? Where is JSON here?
First of all, separate these unrelated issues? What's the problem with JSON? Did you read about Javascropt-native JSON object? It's methods are quite apparent; and you don't have to learn JSON rules for manual editing. Same thing about AJAX. Just learn those native objects with come with Javascript itself...

You ask a question about Javascript but are showing some "http_request", who knows what... (PHP?) How PHP could be related to your question? Don't rush, explain everything properly, and, first of all, understand yourself what you are trying to achieve.

—SA
Member 10482066 28-Jan-15 7:39am    
Ok , when i said i tried xml, i did this but it didn't work:


<script>
function callServer() {
var xmlHttp;
try{
// Opera 8.0+, Firefox, Safari
xmlHttp = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}

var firstname = document.getElementById("firstname").value;
var lastname = document.getElementById("lastname").value;
var mail = document.getElementById("mail").value;
var mail2 = document.getElementById("mail2").value;
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var password2 = document.getElementById("password2").value;

var xmlString = "<firstname>" + escape(firstname) +"" + "<lastname>" + escape(lastname) +"" +"<mail>" + escape(mail) + "" + " <mail2>" + escape(mail2) + "" + " <username>" + escape(username) + ""+ " <password>" + escape(password) + "</password>" + <password2> + escape(password2) + "</password2>";

//Build the URL to connect to
var url = "server.xml";
// Open a connection to the server
xmlHttp.open("GET", url, true);
// Tell the server you're sending it XML
xmlHttp.setRequestHeader("Content-Type", "text/xml");
// Set up a function for the server to run when it's done
// Send the request-
xmlHttp.send(xmlString);
}

function updatePage() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var response = xmlHttp.responseText;
document.getElementById("firstname").value = response;
document.getElementById("lastname").value = response;
document.getElementById("mail").value = response;
document.getElementById("mail2").value = response;
document.getElementById("username").value = response;
document.getElementById("password").value = response;
document.getElementById("password2").value = response;
}
else if (xmlHttp.status == 404) {
alert("Request URL does not exist");
}
else {
alert("Error: status code is " + xmlHttp.status);
}
}
}
</script>


and after the previous code i tried this:

function loadJSON()
{
var http_request = new XMLHttpRequest();
try{
// Opera 8.0+, Firefox, Chrome, Safari
http_request = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
http_request.onreadystatechange = function(){
if (http_request.readyState == 4 )
{
// Javascript function JSON.parse to parse JSON data
var jsonObj = JSON.parse(http_request.responseText);

// jsonObj variable now contains the data structure and can
// be accessed as jsonObj.name and jsonObj.country.
document.getElementById("username").innerHTML = jsonObj.firstname;
document.getElementById("password").innerHTML = jsonObj.lastname;
}
}
http_request.open("GET", "server.xml", true);
http_request.send();
}
</script>
Sergey Alexandrovich Kryukov 28-Jan-15 11:37am    
Please don't put code in comments, it's unreadable. Use "Improve question", format it (in "pre" HTML element).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900