2

This is actually the first time I'm using ajax so honestly I don't know a single thing. Tried to read articles about ajax but no luck. What I'm trying to do here is quite simple really I just want to try to figure out how ajax works and what I should do to make it work. I wanted it so that when I try to click the login button and go to my php and display "login success" it works when the textboxes are empty it displays fields are empty but the ajax doesn't seem to work

Here is what I've done so far this is my index.php A.K.A login page

<!DOCTYPE html>
<html>
<head>
	<title>O-Chat</title>
	<link rel="stylesheet" type="text/css" href="index.css">
	<script type="text/javascript" src="jquery.js"></script>
	<script type="text/javascript" src="script101.js"></script>
</head>
<body>
	<div class="content">
		<div class="features">
			<div><br><br><br><br><br><br><br><br><br><br>FEATURES HERE</div>
		</div>
		<div class="loginpart">
			<div class="banner"><br><br><br>BANNER HERE</div>
			<div class="login" id="divlogin">
				<div class="login-header">Login</div>
				<form name="loginform" class="login">
					<input class="login" type="text" name="pUname" id="jUname" maxlength="35" placeholder="Username"/><br>
					<input class="login" type="Password" name="pPass" id="jPass" maxlength="40" placeholder="Password"/><br>
					<span class="error" id="jloginerror"></span>
					<div class="button" id="login"" onclick="validate();">
					</div>
				</form>
				<div class="login-footer" onclick="location.href='register.php'">Not yet a member?<br>Register Now! Click Here!</div>
			</div>
		</div>
	</div>
	<div class="footer">
		<div class="footer-container">
			<a href="index.php">Home</a> |
			<a href="aboutus.php">About O-Chat</a> |
			<a href="contactus.php">Contact Us</a>
			<div style="float: right;">Copyright © 2000 - 2017 O-Chat Unlimited (071813-S) All Rights Reserved</div>
		</div>
	</div>
</body>
</html>

Here is my javascript file script101.js

function validate() {
var uname = $("#jUname").val();
var pass = $("#jPass").val();
var data = "&uname=" + uname + "&pass=" + pass;
if(!uname.match(/\S/) || !pass.match(/\S/)) {
	document.getElementById("divlogin").style.height = '275px';
    document.getElementById("jloginerror").innerHTML = "Some fields are empty!";
}
else
{
	document.getElementById("jloginerror").innerHTML = "";
	document.getElementById("divlogin").style.height = '250px';

	$.ajax({
        type: "post",
        url: "login.php",
        data: data,
        sucess:function(data){
            $("#jloginerror").text(data);
        }
    });
}
}

<?php
	echo "very successful";
?>

3
  • Whats a $function? Do you mean $(function(){..})? Commented Feb 2, 2017 at 5:55
  • There are lots of things wrong with what you have, do you want me to correct them all? And do you want me to correct and explain or just fix? Commented Feb 2, 2017 at 5:56
  • I've corrected some of the typos above and it doesn't seem to go through the ajax code piece Commented Feb 2, 2017 at 12:12

2 Answers 2

1

Remove $ from the function declaration $function validate(). You can not use $ here.

You can use it like either

$(function(){
....
});

OR

if you wants to keep name of your function then just remove $ before the function, simply use it as:

function validate(){
....
}

and on ajax , you can use

$.ajax({
        method: "post",
        url: "login.php",
        data: data;
        sucess:function(data){
            $("#jloginerror").text(data);
        }
    });

So your full code should be like as follows:

 function validate() {
    var uname = $("#jUname").val();
    var pass = $("#jPass").val();
    var data = "&uname=" + uname + "&pass=" + pass;
    if(!uname.match(/\S/) || !pass.match(/\S/)) {
        document.getElementById("divlogin").style.height = '275px';
        document.getElementById("jloginerror").innerHTML = "Some fields are empty!";
    }
    else
    {
        document.getElementById("jloginerror").innerHTML = "";
        $.ajax({
            type: "post",
            url: "login.php",
            data: data,
            sucess:function(data){
                $("#jloginerror").text(data);
            }
        });
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

By your explanation, I understand that this is just a typo error. If so, should you not comment and mark it as off-topic as its a typo?
OP is using Ajax first time so better to explain the reason for error so I choose to answer rather than just comment.
actually I didn't know if that the function naming was a typo coz it worked just find without the ajax. I tried the function and called it and it worked as intended so I was hoping it the ajax that's wrong... I just arrived at home so I will try what you've said
It still doesn't seem to work the validation of the empty textbox is working fine but when it comes to the ajax calling the login.php which suppose to echo "very successful"; is not working :/ I edited my javascript code above to match yours, I even tried putting an alert inside the ajax just to check if it runs through it but it doesn't
check URL is accessible with this ajax script?
|
1

Remove $ from the function keyword and Jquery works with element id or name var uname = $("#jUname").val(); var pass = $("#jPass").val(); and do not use ; for separating and you should use , $.ajax({}), use $.ajax({}) instead of ajax({}) because its Jquery function not javascript function.

function validate() {
    var uname = $("#jUname").val();
    var pass = $("#jPass").val();
    var data = "&uname=" + uname + "&pass=" + pass;
    if(!uname.match(/\S/) || !pass.match(/\S/)) {
    	document.getElementById("divlogin").style.height = '275px';
        document.getElementById("jloginerror").innerHTML = "Some fields are empty!";
    }
    else
    {
    	document.getElementById("jloginerror").innerHTML = "";
    	$.ajax({
    		type: "post",
    		url: "login.php",
    		data: data,
    		sucess:function(data){
    			$("#jloginerror").text(data);
    		}
    	});
    }
}

1 Comment

What have you changed and why. Please add explanation

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.