Jump to content

[SOLVED] Function Problem


OilSheikh

Recommended Posts

Hi ,

 

I m trying to validate the username field on my form so that it is > = 4 and NOT less.

 

Unfortunately, the fuction isn't working.

 

Any ideas?

 



<?php

include("SQL.php"); 

?>


<html>

<head>
<title>Please Log in to view Your Account</title>
<link rel="stylesheet" href="styler.css">
</head>


<body>

<?php 

#########	CHECKS THAT FIELDS ARE NOT EMPTY	##########

if (isset($_POST ['submit']))
{
	if (!$_POST ['fname'] | !$_POST ['sname'] | !$_POST ['address'] | !$_POST ['postcode'] | 			         !$_POST ['email'] | !$_POST ['username'] | !$_POST ['pass'] | !$_POST ['confpass'] )
	{
		die ('ERROR: Please make sure that all Information is provided');
	}
}

#########	CHECKS THAT TYPED AND CONFIRMED PASSWORDS MATCH	##########

if($_POST['pass'] != $_POST['confpass'])
{
	die ('ERROR : The Passwords did not match. Please confirm the password entered.');
}

#########	CHECKS THAT Username is at least 4 characters	##########
function checkusername ($uname)
{
if (strlen($uname) < 5)
{
    	print "Username should be at least 4 characters";
} 	

}

?>


<h2>Registration for New Users<br></h2>
<table border="0" width="101%" cellspacing="1" id="table2" bordercolorlight="#0066FF" height="33">
<tr>
	<td width="365"><font size="2" ><h3>
	<font color="#FF0000">You Must Supply all 
	Information requested in this Form.</font></h3></font></td>
</tr>
</table>


<h3>
<form method = "post" action = "registerv1.php">
<table border="0" width="101%" cellspacing="0" id="table1" bordercolorlight="#0066FF" height="482" cellpadding="0">
<tr>
	<td width="254" height="48" bgcolor="#DDDDDD">First Name</td>
	<td height="48" width="508" bgcolor="#DDDDDD"><input type = "text" name = "fname" size="34" maxlength ="25" style = "color:blue"></td>
</tr>
<tr>
	<td width="254" height="47" bgcolor="#DDDDDD">Surname</td>
	<td height="47" width="508" bgcolor="#DDDDDD"><input type = "text" name = "sname" size="34" maxlength = "25" style = "color:blue"></td>
</tr>
<tr>
	<td width="254" bgcolor="#DDDDDD" height="108">Address</td>
	<td width="508" bgcolor="#DDDDDD" height="108">
	<textarea cols = "29" rows = "6" name = "address" style = "color:blue"></textarea></td>
</tr>
<tr>
	<td width="254" bgcolor="#DDDDDD" height="44">Post Code</td>
	<td width="508" bgcolor="#DDDDDD" height="44">
	<input type = "text" name = "postcode" size="18" maxlength = "9" value = "AA00 0AA" style = "color:blue">
	</td>
</tr>
<tr>
	<td width="254"> </td>
	<td width="508"> 
	</td>
</tr>
<tr>
	<td width="254" bgcolor="#DDDDDD">E-mail Address</td>
	<td width="508" bgcolor="#DDDDDD">
	<input type = "text" name = "email" size="34" maxlength ="50" value = "someone@somesite.com" style = "color:blue"></td>
</tr>
<tr>
	<td width="254"> </td>
	<td width="508"> 
	</td>
</tr>
<tr>
	<td width="254" bgcolor="#DDDDDD">Choose a Username</td>
	<td width="508" bgcolor="#DDDDDD">
	<input type = "text" name = "username" size="34" maxlength ="7" style = "color:blue" 
	>  
	( Between 4 -7 Characters )

	<?php checkusername($username) ?></td>


</tr>
<tr>
	<td width="254" bgcolor="#DDDDDD" height="29">Choose a Password</td>
	<td width="508" bgcolor="#DDDDDD" height="29">
	<input type = "password" name = "pass" size="34" maxlength ="7" value = "1234" style = "color:blue">  
	( Between 4 -7 Characters )</td>
</tr>
<tr>
	<td width="254" bgcolor="#DDDDDD" height="35"><i>Confirm Password</i></td>
	<td width="508" bgcolor="#DDDDDD" height="35">
	<input type = "password" name = "confpass" size="34" maxlength ="7" value = "1234" style = "color:blue"> </td>
</tr>
<tr>
	<td width="254" bgcolor="#FFFFFF" height="19"> </td>
	<td width="508" bgcolor="#FFFFFF" height="19"> 
	</td>
</tr>
<tr>
	<td width="254" bgcolor="#FFFFFF"> </td>
	<td width="508" bgcolor="#FFFFFF">

	<input type = "submit" name = "submit" value = "" style="background : url(register.jpg);width:155px; height:35px; ">

</tr>
</table>
</form>
<font size="2"><br><br>Your Details will STRICTLY NOT be shared by us with any other 
Party and are governed by the Data Protection Act 2000.<br>Your E-mail Address 
will ONLY be used to correspond with you regarding your Order.</font><br>



</body>

</html>


Link to comment
Share on other sites

Where your calling it will never work. Im not even 100% sure what your trying to do but you can't use your function until after the form is submitted. Something like...

 

<?php

  if (isset($_POST['username'])) {
    checkusername($_POST['username']);
  }

?>

 

To do what it looks like your trying to do you'll need to use Javascript.

Link to comment
Share on other sites

I think that would make a minimum of 5 and not 4 lol,

function checkusername($uname){
if(trim(strlen($uname)) >= 4){
return true;
} else {
return false;
}
}

 

then somewhere below... perhaps,

if(checkusername($_POST['username']) == TRUE){
echo "Continue the checking..";
} else {
echo "Username is not of appropriate length";
}

 

 

 

Thorpe, he could use AJAX.

Link to comment
Share on other sites

Where your calling it will never work. Im not even 100% sure what your trying to do but you can't use your function until after the form is submitted. Something like...

 

<?php

  if (isset($_POST['username'])) {
    checkusername($_POST['username']);
  }

?>

 

To do what it looks like your trying to do you'll need to use Javascript.

Why when I ask for help, you tell me that we are not here

to write it for you. I just need a lil boost like you gave here.

Link to comment
Share on other sites

Thorpe, he could use AJAX.

 

Why on earth would you need Ajax to check the length of a string?

 

Perhaps he wants his page to be more advanced, and validate it as they are typing in their info?

 

"Where your calling it will never work. Im not even 100% sure what your trying to do but you can't use your function until after the form is submitted. Something like..."

 

With this, it can be validated as his form is in the process of being filled out. You mentioned JavaScript, I hinted Ajax. Lol.

 

 

 

 

 

Here's the AJAX solution I've done up for you...

 

 

 

On the page with the input field:

 

<script type='text/javascript'>
<!--
function checkusername(){
var ajaxRequest;
try{
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Browser fails to support Ajax.
			return false;
		}
	}
}
var user = document.getElementById('user').value;

ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		   var ajaxDisplay = document.getElementById('blabla');
		   ajaxDisplay.innerHTML = ajaxRequest.responseText;
               }
}

   var queryString = "?user=" + user;
   ajaxRequest.open("GET", "check.php" + queryString, true);
   ajaxRequest.send(null); 
}
//-->
</script>

 

The input field

 

<input type='text' onchange='checkusername()' name='username' value='' id='user' /> <span id='blabla'><!-- error/ok will display here --></span>

 

 

Check.php

 

<?PHP
  if(trim(strlen($_GET['user'])) >= 4){
     echo "Name is a valid length.";
  } else {
    echo "Name is too short.";
  }
?>

Link to comment
Share on other sites

Why when I ask for help, you tell me that we are not here

to write it for you. I just need a lil boost like you gave here.

 

What are you talking about?

 

Perhaps he wants his page to be more advanced, and validate it as they are typing in their info?

 

All that can be done without ever needing to go back and forth between the server. Javascript!

Link to comment
Share on other sites

Why when I ask for help, you tell me that we are not here

to write it for you. I just need a lil boost like you gave here.

 

What are you talking about?

 

Perhaps he wants his page to be more advanced, and validate it as they are typing in their info?

 

All that can be done without ever needing to go back and forth between the server. Javascript!

 

AJAX is a type of JavaScript that establishes a connection with the server without reloading the page :-\ It's just as good for what he's doing X.x

Link to comment
Share on other sites

AJAX is a type of JavaScript that establishes a connection with the server without reloading the page Undecided It's just as good for what he's doing X.x

 

Buddy, this thread isn't the place, but your way off track. Javascript is part of Ajax, Ajax is not a type of Javascript. And making unrequired requests to a server (behind the scenes or not) is just poor design. Its simply not needed, Javascript can do all that by itself without any need to ask the server if the string is long enough?

Link to comment
Share on other sites

Whoa!

 

Plenty of replies. Thanks guys.

 

I should have made myself clear. This code is for a User Registartion page for a e-shopping site. Visitor fills in all the fields and then chooses SUBMIT. Then, PHP gets into action and checks everything and alerts if there is something wrong.

 

Lemme go and try the correct function code u guys suggested.

 

Btw, sw0o0sh , I ain't gonna try AJAX. I am not supposed to be using AJAX.  :D

 

One more thing,

 

Is the function being properly called from within the HTML code in the form?  ???

 

 

Link to comment
Share on other sites

Thanks thorpe.

 

I used your code and did a bit of modding to transform the code.

 

Here it is:

 

#########	CHECKS THAT Username is at least 4 characters	##########
function checkusername ($uname)
{
if (strlen($uname) < 4)
{
    	 die ('<font face="Verdana" size="4" color = red>ERROR : Username should be at least 4 characters </font> <br><br> <input type="button" value="  Retry  " onClick="history.go(-1)"> ');

} 	
return $uname;

}

  if (isset($_POST['username'])) 
  {
    checkusername($_POST['username']);
  }

 

 

Thanks a lot guys.

 

Just one more question. Is it possible to have a JAVASCRIPT type Alert pop-up rather than going back and forth between the form and RETRY as in my modified code at the moment.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.