Reputation: 307
In the code below , the else part is working fine , but if the email is not there in database , why the javascript alert --- ('This email id doesnt exist in our records') not working ? . If the else part is working why the num_rows=0 not working . I tried a lot . A help will be appreciated.
<?php
include 'db.php';
$em = $_POST['email'];
$qry = mysql_query("select * from user where email_id='$em' ")or die(mysql_error());
$num = mysql_num_rows($qry);
if(is_null($num))
{
?>
<script type="text/javascript">
alert('This email id doesn't exist in our records');
</script>
<?php
}
else
{
while ($row = mysql_fetch_array($qry))
{
$email_id = $row['email_id'];
$name = $row['user_login_id'];
$password= $row['password'];
}
if($email_id)
{
$to= $email_id;
$subject=" Password Recovery Mail";
$message="Let US REMIND YOU!! Your username - ".$name. " and Password - ".$password;
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header.="From:<[email protected]>"."\r\n";
$n=mail($to,$subject,$message,$header);
?> <script type="text/javascript">
alert('Check your Mail, <?php echo $email_id; ?>, for your username and password');
window.location="index.php";
</script>
<?php
}
}
?>
<div class="container">
<div class="row">
<div class="col-md-8"><br/>
<div class="col-md-12" style="border:1px solid #ddd;"><br/>
<form class="form-horizontal" action="" method="POST">
<h2>Hope you remember your registered email id...</h2>
<div class="form-group">
<label class="col-sm-5 control-label">Enter the registered email id</label>
<div class="col-sm-2">
<input type="email" name="email" autocomplete="off" id="" placeholder="Enter the email id " required autofocus>
<input type="submit" id="sub" name="forgotpass" value="Continue">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Upvotes: 1
Views: 75
Reputation:
try this
<?php
include 'db.php';
$em = $_POST['email'];
$qry = mysql_query("select * from user where email_id='$em' ")or die(mysql_error());
if( mysql_num_rows($qry) < 0)
{
?>
<script type="text/javascript">
alert('This email id doesn't exist in our records');
</script>
<?php
}
else
{
while ($row = mysql_fetch_array($qry))
{
$email_id = $row['email_id'];
$name = $row['user_login_id'];
$password= $row['password'];
}
if($email_id)
{
$to= $email_id;
$subject=" Password Recovery Mail";
$message="Let US REMIND YOU!! Your username - ".$name. " and Password - ".$password;
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header.="From:<[email protected]>"."\r\n";
$n=mail($to,$subject,$message,$header);
?> <script type="text/javascript">
alert('Check your Mail, <?php echo $email_id; ?>, for your username and password');
window.location="index.php";
</script>
<?php
}
}
?>
<div class="container">
<div class="row">
<div class="col-md-8"><br/>
<div class="col-md-12" style="border:1px solid #ddd;"><br/>
<form class="form-horizontal" action="" method="POST">
<h2>Hope you remember your registered email id...</h2>
<div class="form-group">
<label class="col-sm-5 control-label">Enter the registered email id</label>
<div class="col-sm-2">
<input type="email" name="email" autocomplete="off" id="" placeholder="Enter the email id " required autofocus>
<input type="submit" id="sub" name="forgotpass" value="Continue">
</div>
</div>
</form>
</div>
</div>
</div>
Upvotes: 0
Reputation: 343
First of all to introduce Javascript alert box inside PHP code you need to call a function. Try the below code
<script type="text/javascript">
function show_alertMsg() {
var msg = "This email id doesn't exist in our records.";
alert(msg);
}
</script>
if(is_null($num))
{
echo '<script type="text/javascript"> show_alertMsg(); </script>';
}
else {
// your code
}
Upvotes: 0
Reputation: 357
I would fist check the console messages since I think your alert function is not working at all even though it comes on it cause of the ' that you used in text.
Change this
alert('This email id doesn't exist in our records');
to
alert("This email id doesn't exist in our records"); // See the "
Cheers
Upvotes: 1
Reputation: 6002
$num = mysql_num_rows($qry);
gives you the number of count of no of rows retrieved else FALSE
, indicating 0 records.
So when you do a check if(is_null($num))
it will never enter the if clause, because the mysql_num_rows($qry);
will never return NULL
.
you need to change that to
<? if($num == FALSE)
{
?>
<script type="text/javascript">
alert('This email id doesn't exist in our records');
</script>
Upvotes: 0
Reputation: 1958
Compare your variable $num against 0 and FALSE. Since mysql_num_rows function returns in integer on success and return FALSE on failure.
if( $num ==0 && $num!=FALSE)
AND Check $email agains empty or isset
if(isset($email) && $email!="")
Upvotes: 0
Reputation: 38584
change this
if(is_null($num))
to this
if(empty($num))
And
change this
if($email_id)
to this
if(isset($email_id))
Upvotes: 2