473,288 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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

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 2356
Atli
5,058 Expert 4TB
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
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
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
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
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
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
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
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
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
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.