4 Replies - 683 Views - Last Post: 26 February 2013 - 01:39 PM Rate Topic: -----

#1 ziggy786  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 26-February 13

[error, problem] How to get data from PHP file using JQuery Post?

Posted 26 February 2013 - 05:56 AM

loginSend.js
$('#loginButton').on('click', function(){ //the id of the submit button = #loginButton
	
	var username = $('#userfield').val();
	var password = $('#passfield').val();
	
	if (username.length == 0 || password.length == 0 || password.length < 6) { // checks if username and pass have been entered
	
		$('#fillFields').text('*Please enter a valid username/password'); //#fillfields = the div where error message should appear in JQuery UI dialog box
	} else {
		
		//$('#formLogin').text('You have successfully logged in').dialog('close'); //Closes dialog box if logged in currently not in use
		$.post('loginProcess.php', {username: username, password: password}, funtion(data){ // sends data to loginProcess.php
		
			if (data == true){ //checks if the loginProcess has returned true (if the username exists or not)
				alert('true'); // just to check if the code works, display an alert message
			}
	}
		
	});
});


loginProcess.php
<?php

require 'config.php';

//  LOGIN FORM Process

if (user_exists('super')){ //user_exists() = a user defined function in config.php
	return true;
}
?>


config.php
function sanitize($data){
	return mysql_real_escape_string(strip_tags($data));
}

function user_exists($username){

	$username = sanitize($username);
	$query = mysql_query("SELECT COUNT(`id`) FROM `users` WHERE `Username` = '$username'");
	return (mysql_result($query, 0) == 1) ? true : false;
}


i don't understand what i've done wrong. I'm trying to create a JQuery UI dialog, login form. Normlly when the user types in a username i want it to check if the username exists. But nothing happens. Since i added JQuery Post it doesn't work. How do i get it to display if username exists or not. I think the problem is in $.post because before i added that, my code works, but after i added it it stopped working. It doesn't even display error messages? The whole code just doesn't work.

How do i fix this and get it to work as intended?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: [error, problem] How to get data from PHP file using JQuery Post?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: [error, problem] How to get data from PHP file using JQuery Post?

Posted 26 February 2013 - 06:43 AM

check the Error Console. funtion() is not the same as function().
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: [error, problem] How to get data from PHP file using JQuery Post?

Posted 26 February 2013 - 06:49 AM

besides that, if that (last) code is all there is in config.php, then it cannot work.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: [error, problem] How to get data from PHP file using JQuery Post?

Posted 26 February 2013 - 07:21 AM

Also, your PHP code is returning true, but it doesn't actually output anything. Requests for "loginProcess.php" will always return an empty response, which won't ever evaluate as true in Javascript. You need to actually print/echo something.


One more thing. Your sanitize function is a bit misleading. From the name one would assume all it does is clean the input of unwanted stuff, but you are also using mysql_real_escape_string in there, which would not fit that expectation; you are potentially adding to the input escape sequences specific to MySQL. I'd suggest renaming the function to make that point more clear.

This post has been edited by Atli: 26 February 2013 - 07:23 AM
Reason for edit:: Clarified.

Was This Post Helpful? 1
  • +
  • -

#5 ziggy786  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 9
  • Joined: 26-February 13

Re: [error, problem] How to get data from PHP file using JQuery Post?

Posted 26 February 2013 - 01:39 PM

Thanks guys, I've got it to work now :chinese:

Path name and misspelling, what a waste of 5 hours lol.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1