Jump to content

PHP/jQuery Form Error


FastPHP

Recommended Posts

I have absolutely no idea why this isn't working. Here's the PHP file:

//Check if there is a get variable
if(isset($_GET['username']))
{
$username = $_GET['username'];
//Get string length
$length = strlen($username);

//Check length
if ($length < 8 && $length > 3) print true;
else print false;
}

 

Here's the jQuery:

$(document).ready(function(){

// Declare click variable
var u_click = false;

//If username focused, funciton:
$("#username").focus(function() {
	//If not clicked yet, function:
	if (u_click == false) 
	{
		//Set clicked to true and clear field
		u_click = true;
		$("#username").val("");	
	}
});

//Username check for length function
$("#username").keyup(function() {
	//Send variable
	$.get("../php/checkuser.php", {username: $("#username").val()}, function (data) {
		if (data == true) {
			$("#usernameMessage").innerHTML = "Right";
		} else {
			$("#usernameMessage").innerHTML = "Wrong";
		}
	});
});


});

 

This first jQuery function meant to clear the form works 100%, but the other doesn't return anything at all in the span with ID "usernameMessage". Why is this...any help? Thanks!  :D

 

Oh, and hi! XD

Link to comment
https://forums.phpfreaks.com/topic/200612-phpjquery-form-error/
Share on other sites

Yes, I have checked multiple times on the path. Thanks for the suggestion though. And what do you mean??

 

Edit: modified and still didn't work.

 

New PHP:

//Check length
if ($length < 8 && $length > 3) print "true";
else print "false";

 

New jQuery:

if (data == "true")

Gah, no. It's local at the moment, and good thing, too. But here's the HTML, jQuery, and PHP (cropped).

 

HTML:

<label>Desired Username:</label> <input id="username" type="text" name="username" />
<span id="usernameMessage"></span>
<br />
<div id="spacer"></div>

 

jQuery:

$(document).ready(function(){

// Declare click variable
var u_click = false;

//If username focused, funciton:
$("#username").focus(function() {
	//If not clicked yet, function:
	if (u_click == false) 
	{
		//Set clicked to true and clear field
		u_click = true;
		$("#username").val("");	
	}
});

//Username check for length function
$("#username").keyup(function() {
	//Send variable
	$.get("../php/checkuser.php", {username: $("#username").val()}, function (data) {
		if (data == "true") {
			$("#usernameMessage").innerHTML = "Right";
		} else {
			$("#usernameMessage").innerHTML = "Wrong";
		}
	});
});


});

 

<?php

//Check if there is a get variable
if(isset($_GET['username']))
{
$username = $_GET['username'];
//Get string length
$length = strlen($username);

//Check length
if ($length < 8 && $length > 3) print "true";
else print "false";
}

?>

 

I think it might be

$("#usernameMessage").innerHTML = "Right";

because I've had problems with innerHTML before. Not sure...

#usernameMessage, not #username. Typo. Did you not catch that?

 

Yeah, I did. Here's my new code.

 

jQuery:

$(document).ready(function(){

// Declare click variable
var u_click = false;

//If username focused, funciton:
$("#username").focus(function() {
	//If not clicked yet, function:
	if (u_click == false) 
	{
		//Set clicked to true and clear field
		u_click = true;
		$("#username").val("");	
	}
});

//Username check for length function
$("#username").keyup(function() {
	//Send variable
	$.get("../php/checkuser.php", {username: $("#username").val()}, function (data) {
		if (data == true) {
			$("#usernameMessage").html("Right");
		} else {
			$("#usernameMessage").html("Wrong");
		}
	});
});


});

 

PHP:

<?php

//Check if there is a get variable
if(isset($_GET['username']))
{
$username = $_GET['username'];
//Get string length
$length = strlen($username);

//Check length
if ($length < 8 && $length > 3) echo true;
else echo false;
}

?>

 

Note: I tried with both boolean and string.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.