473,503 Members | 4,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

99 New Member
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
1 2364
Atli
5,058 Recognized Expert Expert
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

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

Similar topics

8
8038
by: Randy Yates | last post by:
I'm using mingw/g++ 3.3.3. When I use pos = tellg(), getline(), setg(pos), then the next getline() does NOT get from the original position. I've tried doing a clear() before the seekg() to no...
0
1033
by: Grey | last post by:
I have successfully to create popup window when smartnavigation= ture. But how to set the focus on the child window instead of parent one Million thanks
5
2774
by: Nita Raju | last post by:
Hi, I have to validate a textbox for date without using the validation controls. So i had to use IsDate(). It's not working properly when i give "11//2004". When i enter the above date it...
2
3270
by: Annu | last post by:
Hi I need help on <enbed> tag. Following code(No 1) is working properly on windows but on linux code no.2 is not working Code No 1: <EMBED type='application/x-mplayer2' ...
7
3181
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
4
3234
by: Charleees | last post by:
GIFS not working properly in JavaScript PopUps Hi all, I have a button and when i click tha button it redirects to another page..... I have also added a java script for the button that...
3
1995
by: rajasree | last post by:
Hi all, am doing a project in PHP. my javascript code is working properly in ie. But its not working in firefox. Please help me my code is as follows; <script language="javascript"...
1
1342
by: HyBry | last post by:
I have a form with a subform within it. I am trying to make it dynamic for resizing but I have one problem. well two. firstly. I have this code me.form.width = me.insidewidth this works fine...
1
3012
by: Jiwei06xie | last post by:
Hi, I have a main form with 3 subforms and some combo boxes. It is working fine as an independent form. However, when I draged into a tabcontrol page, two off the combo boxes stopped working...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.