You first try to validate with Javascript/AJAX. However, that cannot be counted on since people can turn off JS, or even use a browser that doesn't support it at all, such as mobile browsers, so all validation must still be done with PHP as if nothing was done client side. However, if the user does have JS turned on, it will just save them some time by having immediate feedback.
Also, for dynamically writing error messages, instead of using document.write, you can put a span or div tag on the page with an ID attribute. Get a reference to the span or div and use the .innerHTML property to add whatever error message you want to add to the page.
Just a quick question to clear up my confusion.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: Just a quick question to clear up my confusion.
The indelible lord of tl;dr
Re: Just a quick question to clear up my confusion.
Well thanks for the answer i was just stopping by to write some updates on how it's going.
First off, the idea of using Div came to me a cuple of hours ago ( I stumbeled uppon a topic about it. ). I just love the idea of Divs now.
I have for now decided to use alert(), but i might change that later for now i wan't to get done with the register/loging page.
I also came to the conclusion that i could not avoid using AJAX, yes I'm a bit slow but hey atleast I'm learning and making progress.
Since i need to check if Email and Username is in use i need to use AJAX.
As for the part of PHP i was thinking of doing a extra validation on the server once JavaScript has validated it. This servers as an extra protection. Though it won't be executed if the JavaScript validation fails.
I'v stumbled uppon a problem in my JS script.
Validation.js
The error text is in Swedish but by looking att the code i guess you can se what it does.
My problem is that this line wont work:
if(form.username.value.lenght<3)
I wan't the users to write a minimum username of 3 characters but it dosent work. I have looked around the webs and i can't see the problem, maybe you guys can clear it up.
Also the code might not be the best but it's really my first real JS scripting.
NOTE: AJAX calls hasn't been inplemented yet. I want the basic to work first.
First off, the idea of using Div came to me a cuple of hours ago ( I stumbeled uppon a topic about it. ). I just love the idea of Divs now.
I have for now decided to use alert(), but i might change that later for now i wan't to get done with the register/loging page.
I also came to the conclusion that i could not avoid using AJAX, yes I'm a bit slow but hey atleast I'm learning and making progress.
Since i need to check if Email and Username is in use i need to use AJAX.
As for the part of PHP i was thinking of doing a extra validation on the server once JavaScript has validated it. This servers as an extra protection. Though it won't be executed if the JavaScript validation fails.
I'v stumbled uppon a problem in my JS script.
Validation.js
Code: Select all
<!--
function validate(form)
{
valid = true;
if ( form.username.value == "" )
{alert("Användarnamnet måste fyllas i!"); valid = false;}
else if ( form.username.value.lenght<"3")
{alert("Användarnamnet måste vara minst tre tecken!"); valid = false;}
if ( form.pass1.value == "")
{alert("Fyll i Lösenord!"); valid = false;}
if ((form.pass1.value) != (form.pass2.value))
{alert("Lösenord matchar inte!"); valid = false;}
if (form.email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
alert("Inte en valid Email Adress");
valid = false;
}
return valid;
}
My problem is that this line wont work:
if(form.username.value.lenght<3)
I wan't the users to write a minimum username of 3 characters but it dosent work. I have looked around the webs and i can't see the problem, maybe you guys can clear it up.
Also the code might not be the best but it's really my first real JS scripting.
NOTE: AJAX calls hasn't been inplemented yet. I want the basic to work first.
Here take a bad computer/programming-thingy joke:
"The best thing about UDP jokes is that I don’t care if you get them or not."
"The best thing about UDP jokes is that I don’t care if you get them or not."
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Just a quick question to clear up my confusion.
Code: Select all
else if ( form.username.value.lenght<"3")
Re: Just a quick question to clear up my confusion.
Wow, such a small mistake.... damn.
Well atleast it's solved now and can spell a new word correct.
Thank you so much xD i appriciate it
a LOT!
OFF TOPIC:
Hallsofvallhalla: You havent replyed on my PM, sent it like a month ago, why is that?
Well atleast it's solved now and can spell a new word correct.
Thank you so much xD i appriciate it

OFF TOPIC:
Hallsofvallhalla: You havent replyed on my PM, sent it like a month ago, why is that?

Here take a bad computer/programming-thingy joke:
"The best thing about UDP jokes is that I don’t care if you get them or not."
"The best thing about UDP jokes is that I don’t care if you get them or not."
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Just a quick question to clear up my confusion.
i apologize. I get many PMs between here, Youtube, Vimeo, and other forums I admin or frequent so some I do not get to or forget to reply to. Trust me it happens allot. I don't show it in my mailbox so resend and I will reply asap. Sorry about that.
Re: Just a quick question to clear up my confusion.
You need a secretary.
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: Just a quick question to clear up my confusion.
Okej so i finally started adding AJAX calls. But it went to hell so i thought "Maybe i should start with a smaller scale"
I coded some new pages just to test AJAX out and se how it works real time
I got it all to work EXCEPT one small thing, since my webpage is in swedish their are the swedish chars Å,Ä and Ö. The webpage shows them fine and the error messages from JavaScript when the user forgets to fill something in shows correct BUT when i retrive echo from PHP with AJAX the å, Ä and Ö characters is replaced with diamonds with a question mark inside them. I have tryed googling it but found no solution that worked for me.
So now I'm turning to the Indie Forum, to help me understand.
I'm using Dreamwever 8( Yes it's old but hey it's what i got ) as an editor.
HTML:
Script.js:
and finally the getuser.php:
The code works perfectly but it's the echo that returns bad Å, Ä and Ö too the homepage.
I coded some new pages just to test AJAX out and se how it works real time

I got it all to work EXCEPT one small thing, since my webpage is in swedish their are the swedish chars Å,Ä and Ö. The webpage shows them fine and the error messages from JavaScript when the user forgets to fill something in shows correct BUT when i retrive echo from PHP with AJAX the å, Ä and Ö characters is replaced with diamonds with a question mark inside them. I have tryed googling it but found no solution that worked for me.
So now I'm turning to the Indie Forum, to help me understand.
I'm using Dreamwever 8( Yes it's old but hey it's what i got ) as an editor.
HTML:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset ISO-8859-1" />
<title>AJAX - Test</title>
<script type="text/javascript" src="script.js"></script>
</head>
<div id="myDiv"> </div>
<form name="register" method="get">
Användarnamn<input type="text" size="15" maxlength="15" name="namn" />
<button type="button" onclick="formCheck()" /> Register </button>
</form>
<body>
</body>
</html>
Code: Select all
function NewXmlHttp()
{
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function formCheck()
{
var str = document.register.namn.value;
var error = "";
if(document.register.namn.value == "")
{
error += "Inget Användarnamn<br>";
}
if(error != "")
{
document.getElementById("myDiv").style.color = "#ff0000";
document.getElementById("myDiv").innerHTML = "Kallabalik, Å och Ä och Ö";
}
else
{
NewXmlHttp();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
//-->
Code: Select all
<?php
// DATA
$q = $_GET['q'];
//CONNECTION
$con = mysql_connect("localhost", "root", "");
$db = "scriptest";
mysql_select_db($db, $con);
// VALIDATION
$playercheck=mysql_fetch_array(mysql_query("SELECT * from users where name='$q'"));
if($playercheck)
{
echo "Användarnamnet är redan registerat av en annan spelare.";
}
else // If no multiple
{
echo "Användarnamnet är Ledigt";
}
// Close the DB
mysql_close($con);
?>
Here take a bad computer/programming-thingy joke:
"The best thing about UDP jokes is that I don’t care if you get them or not."
"The best thing about UDP jokes is that I don’t care if you get them or not."