By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Loop (if statement) is not working properly when the condition is true

P: 99
Dear All,

I got stuck in simple problem, I have a two php file one for registration form and one for to check and insert into the table. The problem is that when I get any kind error in validiation the code is working properly but when the condition are fulfilled the code inside the if part is not executing. The following is the code for registration form UserAdd.php

[HTML]
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add user</title>
<script language="javascript">
function validate()
{
if(document.addUser.varUserId.value =="")
{
alert('Please enter the login id');
addUser.varUserId.focus();
return false;
}
if(document.addUser.varUserPwd1.value == "")
{
alert('Please enter the password');
addUser.varUserPwd1.focus();
return false;
}
if(document.addUser.varUserPwd2.value =="")
{
alert('Please the confirmed password');
addUser.varUserPwd2.focus();
return false;
}
if(document.addUser.varFirstName.value=="")
{
alert('Please enter the first name');
addUser.varFirstName.focus();
return false;
}
if(docuemtn.addUser.varLastName.value =="")
{
alert('Please enter the last name');
addUser.varLastName.focus();
return false;
}
if(document.addUser.varUserEmail.value == "")
{
alert('Please enter the email id');
addUser.varUserEmail.focus();
return false;
}
if(document.addUser.varUserCont.value =="")
{
alert('Please enter the contact no');
addUser.varUserCont.focus();
return false;
}
return true;
}
</script>
<style type="text/css">
.oddRow{
font-size:14px;
font:"Times New Roman", Times, serif;
font-weight:bold;
border-bottom:solid 2px #CC3300;
color: #0033CC;
}
.evenRow{
font-size:14px;
font:"Times New Roman", Times, serif;
font-weight:bold;
border-bottom:solid 1px #FF0099;
color:#0033CC;
}
.tdLeft{
padding-left:1.5em;
}
</style>
</head>

<body>

<div>
<fieldset style="width:600px; margin:0px; border:#FFFFFF solid 2px; margin-left:6em; text-align:center">
<legend>Add New Login</legend>
<?php
if(isset($_GET['error']))
{

$errorMsg = $_GET['error'];
echo "<div style=\"border:1px dotted white;width:400px; text-align:center;margin:1em; padding:12px;\">";
switch($errorMsg){
case "1":
echo "The user id must be greater than 5 character";
break;
case "2":
echo "Both password are not match";
break;
case "3":
echo "Please kindly select the value as Admin or User";
break;
case "4":
echo "Please enter the correct mail format";
break;
case "5":
echo "Please entered the correct no";
break;
case "6":
echo "<font color=red>".$_GET['varUserID']. "</font> is already exists";
break;
default:
echo "";
break;
}
echo "</div><br>";

?>

<div>

</div>
<form name="addUser" action="addUser.php" method="post" style="margin:0px; " >
<table width="100%" style="margin:0px;" border="0" cellpadding="0" cellspacing="1">
<tr class="oddRow">
<td align="left" width="45%" class="tdLeft">Login ID:</td>
<td align="left">
<input type="text" name="varUserId" value="<?php if($_GET['varUserID']){
echo $_GET['varUserID'];
} else {
echo "";
}?>" size="30" />
</td>
</tr>
<tr class="evenRow">
<Td align="left" class="tdLeft">Password</Td>
<td align="left">
<input type="password" name="varUserPwd1" size="30" />
</td>
</tr>
<tr class="oddRow">
<Td align="left" class="tdLeft">Confirmed Password</Td>
<td align="left">
<input type="password" name="varUserPwd2" size="30" />
</td>
</tr>
<tr class="evenRow">
<td align="left" class="tdLeft">First Name:</td>
<td align="left">
<input type="text" name="varUserFirstName" value="<?php if($_GET['varUserFirstName']){
echo $_GET['varUserFirstName'];
} else {
echo "";
}?>" size="30" />
</td>
</tr>
<tr class="oddRow">
<td align="left" class="tdLeft">Last Name</td>
<td align="left">
<input name="varUserLastName" type="text" value="<?php if($_GET['varUserLastName']){
echo $_GET['varUserLastName'];
} else {
echo "";
}?>" size="30" />
</td>
</tr>
<tr class="evenRow">
<td align="left" class="tdLeft">User Type</td>
<td align="left">
<select name="varUserType" style="width:100px">
<option selected="selected">Select</option>
<option value="Admin">Admin</option>
<option value="User">User</option>
</select>
</td>
</tr>
<tr class="oddRow">
<td align="left" class="tdLeft">Email Address:</td>
<td align="left">
<input type="text" name="varUserEmail" value="<?php if($_GET['varUserEmail']){
echo $_GET['varUserEmail'];
} else {
echo "";
}?>"size="30">
</td>
</tr>
<tr class="evenRow">
<td align="left" class="tdLeft">Contact No.</td>
<td align="left">
<input type="text" size="30" value="<?php if($_GET['varUserCont']){
echo $_GET['varUserCont'];
} else {
echo "";
}?>" name="varUserCont">
</td>
</tr>
<tr class="oddRow">
<td></td>
<td align="left">
<input type="submit" value="Submit" name="Submit" onclick="return validate()" style="width:80px;color:#FFFFFF; font-size:15px; font-weight:bold; border:#0000FF 1px solid; background-color: #3399FF">
<input type="reset" name="Reset" value="Reset" style="width:80px; color: #FFFFFF; font-size:15px; font-weight:bold;border:#0000FF 1px solid; background-color: #3399FF">
</td>
</tr>
</table>
</form>

<?php
}
else{
?>

<form name="addUser" action="addUser.php" method="post" style="margin:0px; " >
<table width="100%" style="margin:0px;" border="0" cellpadding="0" cellspacing="1">
<tr class="oddRow">
<td align="left" width="45%" class="tdLeft">Login ID:</td>
<td align="left">
<input type="text" name="varUserId" size="30" />
</td>
</tr>
<tr class="evenRow">
<Td align="left" class="tdLeft">Password</Td>
<td align="left">
<input type="password" name="varUserPwd1" size="30" />
</td>
</tr>
<tr class="oddRow">
<Td align="left" class="tdLeft">Confirmed Password</Td>
<td align="left">
<input type="password" name="varUserPwd2" size="30" />
</td>
</tr>
<tr class="evenRow">
<td align="left" class="tdLeft">First Name:</td>
<td align="left">
<input type="text" name="varUserFirstName" size="30" />
</td>
</tr>
<tr class="oddRow">
<td align="left" class="tdLeft">Last Name</td>
<td align="left">
<input name="varUserLastName" type="text" size="30" />
</td>
</tr>
<tr class="evenRow">
<td align="left" class="tdLeft">User Type</td>
<td align="left">
<select name="varUserType" style="width:100px">
<option selected="selected">Select</option>
<option value="Admin">Admin</option>
<option value="User">User</option>
</select>
</td>
</tr>
<tr class="oddRow">
<td align="left" class="tdLeft">Email Address:</td>
<td align="left">
<input type="text" name="varUserEmail" size="30">
</td>
</tr>
<tr class="evenRow">
<td align="left" class="tdLeft">Contact No.</td>
<td align="left">
<input type="text" size="30" value="+91-" name="varUserCont">
</td>
</tr>
<tr class="oddRow">
<td></td>
<td align="left">
<input type="submit" value="Submit" name="Submit" onclick="return validate()" style="width:80px;color:#FFFFFF; font-size:15px; font-weight:bold; border:#0000FF 1px solid; background-color: #3399FF">
<input type="reset" name="Reset" value="Reset" style="width:80px; color: #FFFFFF; font-size:15px; font-weight:bold;border:#0000FF 1px solid; background-color: #3399FF">
</td>
</tr>
</table>
</form>
<?php
}
?>

</fieldset>
</div>

</body>
</html>
[/HTML]

The below is the code addUser.php which doing the validation and inserting the data in database.
[HTML]
<?php
include('config.php');
include('connect.php');

$varUserID = trim($_POST['varUserId']);
$varUserPwd1 = md5($_POST['varUserPwd1']);
$varUserPwd2 = md5($_POST['varUserPwd2']);
$varUserFirstName = trim($_POST['varUserFirstName']);
$varUserLastName = trim($_POST['varUserLastName']);
$varUserType = trim($_POST['varUserType']);
$varUserEmail = $_POST['varUserEmail'];
$varUserCont = $_POST['varUserCont'];
$dUserCreateDate = date('y-m-d');
$errorMsg = "";
//echo "dd";
//echo $varUserID. $varUserPwd1.$varUserPwd2.$varUserType.$varUserEma il.$varUserCont.$dUserCreateDate;
if(isset($_POST['Submit']) && $_POST['Submit'] = 'Submit')
{
// echo "dd2";
if(strlen($varUserID)<"5")
{
$errorMsg = "1";
// echo "dd3";
}
else{
$check = "SELECT * FROM tbllogin where varUserId = '$varUserID'";
$result_check = mysql_query($check) or die(mysql_error());
$rows = mysql_num_rows($result_check) or die(mysql_error());

if($rows>0)
{
$errorMsg="6";
}
}
if($varUserPwd1 != $varUserPwd2)
{
$errorMsg = "2";
}
if(empty($varUserType))
{
$errorMsg = "3";
}
if($varUserEmail == "")
{
$errorMsg = "4";
}
if(strlen($varUserCont)>15)
{
$errorMsg = "5";
}

if(empty($errorMsg))
{
$sql = mysql_query("Insert into tbllogin(varUserID, varUserPwd, varUserFirstName,varUserLastName, varUserUserType,varUsrEmail,varUserCont) values ('$varUserID','$varUserPwd1','$varUserFirstName',' $varUserLastName','$varUserType','$varUserEmail',' $varUserCont')") or die(mysql_error());
$rows = mysql_num_rows($sql);
if($rows>0)
{
header("location:admin_home_page.php");
}
}
else
{
header("Location:admin_home_page.php?page=confirm_ add_user&error=$errorMsg&varUserID=$varUserID&varU serFirstName=$varUserFirstName&varUserLastName=$va rUserLastName&varUserType=$varUserType&varUserEmai l=$varUserEmail&varUserCont=$varUserCont");
}

break;
}


?>

In the above code, when i dont find any error, still the code inside if statement is not executing. please kindly help for the same.

Thanking you all in advance for your kind supports.

Regards,

Robindra Singha
[/HTML]
Aug 18 '08 #1
Share this Question
Share on Google+
1 Reply


Atli
Expert 5K+
P: 5,058
Hi.

The if statement in your code is looking for the existence of the submit button, which isn't necessarily sent with the rest of the data.
If the form was submitted using the enter key (at least in some browsers), or via Javascript, the button may not be sent along.

Try changing that, look for one of the other fields, or add a hidden input to check for.

And by the way, the <script language="javascript"> attribute is deprecated.
It should be <script type="text/javascript">.
Aug 18 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.