4 Replies - 6849 Views - Last Post: 10 September 2011 - 12:54 PM

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Ajax call to PHP file not working!

Posted 10 September 2011 - 11:50 AM

Hi, this code is supposed to generate a HTML page with username and password inputs. If the user types in guest and jquery respectively in these fields, the page should display a personalized message, and if not, then "sorry, you are not authorized".

<html>
<head>
<title>Ajax</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function () {
var name = $('.uname').val();
var pwd = $('.passwd').val();
var data='uname='+name+'&password='+pwd;
$.ajax({
type:"GET",
url:"logincheck.php",
data:data,
success:function(html) {
$("#message").html(html);
}
});
return false;
});
});
</script>
</head>
<body>
<form>
<label>Enter your Name</label>
<input type="text" name="uname" class="uname"/> <br/>
<label>Enter your Password</label>
<input type="password" name="password" class="passwd"/> <br/>
<input type="submit" id="submit"/>
</form>
<div id="message"></div>
</body>
</html>



The PHP file logincheck.php kept in the same folder as this one, runs as follows:

<?php
$name = trim($_GET['uname']);
$pswd = trim($_GET['password']);
if (($name=="guest") && ($pswd=="jquery"))
echo "Welcome ". $name;
else
echo "Sorry you are not authorized";
?>



I cannot find anything wrong with the code, but when I enter name and password and click the button, nothing happens. No message is displayed, whether the username and password are correct or not. Any help?

Is This A Good Question/Topic? 0
  • +

Replies To: Ajax call to PHP file not working!

#2 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Ajax call to PHP file not working!

Posted 10 September 2011 - 12:00 PM

Slight modification, when I upload these files to WAMP, and type in localhost/ajax.html in the address bar to open the page, then the code works as expected. This is similar to testing Ajax codes written with vanilla Javascript, where the files won't work from desktop or any other folder. But with other jQuery Ajax functions like $.getJSON() or $.get(), it worked either way, both from desktop and from WAMP, so I expected this one would do so too. Apparently it doesn't. Can anyone tell me why?
Was This Post Helpful? 0
  • +
  • -

#3 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Ajax call to PHP file not working!

Posted 10 September 2011 - 12:22 PM

Hi,

perhaps adding an error handler to your AJAX call could help find the problem:
$(document).ready(function() {
	$('#submit').click(function () {
		var name = $('.uname').val();
		var pwd = $('.passwd').val();
		var data='uname='+name+'&password='+pwd;
		$.ajax({
			type:"GET",
			url:"logincheck.php",
			data:data,
			success:function(html) {
				$("#message").html(html);
			},
			error:function(jqXHR, textStatus, errorThrown){
				alert("Error type" + textStatus + "occured, with value " + errorThrown);
			}
		});
	return false;
	});
});



As far as your code not working from the desktop I'm not sure why that would be. Naturally your PHP wouldn't run unless you were accessing it through a web server but you should still get something back from the call.

If you're not already using it I suggest getting Firebug or a similar web dev tool. You can view AJAX posts and responses in the console and it can be very useful when debugging AJAX issues.
Was This Post Helpful? 1
  • +
  • -

#4 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Ajax call to PHP file not working!

Posted 10 September 2011 - 12:38 PM

Thanks. Another question, using $.ajaxSetup(), if I enter any value for a particular key inside the data object literal, and then again I use a different value for the same key indide the data object lteral of $.ajax() call, the last one overrides the first one, as expected. So if the code runs something like this:

# $.ajaxSetup({
data: {
first: "cool"
}
});

//other code

$.ajax({
type: "POST", 
url: "somefile.php"
data: {first: "fool" }
});



what the php file in the server gets is the value fool for the key first. Now, the article at http://www.bennadel....AX-Requests.htm says that JSON doesn't post undefined values. But keeping in mind the overriding feature, if I run something like this:

# $.ajaxSetup({
data: {
first: a
}
});

//other code

$.ajax({
type: "POST", 
url: "somefile.php"
data: {first: "fool" }
});



the server should get the value fool for the key first, because even though first is assigned an undefined variable a inside ajaxSetup(), it is assigned a string inside the .ajax() method, so that should override the undefined-ness. As it turns out, it doesn't, and JSON doesn't post the key-value for the first key. So why isn't the overriding feature working here?

This post has been edited by cupidvogel: 10 September 2011 - 12:39 PM

Was This Post Helpful? 0
  • +
  • -

#5 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Ajax call to PHP file not working!

Posted 10 September 2011 - 12:54 PM

Hmmm, on another look, I found that the article actually told that on giving a key a value of undefined does JSON not post it. I took it otherwise, I thought that an undefined variable is same as the word undefined. Apparently it is not, and on using undefined, the overriding feature works ok.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1