Page not redirected when condition is true

Hi there,
I am designing an ecommerce theme in php on my own. After an administrator successful logins, a page should redirect to success.php. So the problem is : on successful login, page is not redirected but instead full code of header.php and footer.php which is called from success.php is shown in "Javascript Console", Why this is happening:

Code in success.php is :
<?php 
	session_Start();
?>
<?php 
	include('../../../wp-load.php'); 
	include("header-adm.php"); 
?>
<section>
	<div class="fifteen columns row dsgnfullwdth">
		<div class="seven columns row">S</div>
		<div class="seven columns row">P</div>
	</div>
	<div align="center">
	  <div style="float:left; padding-left:2%; width:90%; height:40px; margin-bottom:20px"></div>
	</div>
</section>
</div>
<?php include("footer.php"); ?>

Open in new window


For your information I am attaching a snapshot of the problem
Please guide
ERROR.JPG
Shobhana UpretiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
Your console says there is an error: anything about? Can you psot a link to the live page so we can see it? And can you post the code which should redirect to the success page?
Julian HansenCommented:
Why this is happening:
You need to give us more than this. Can you post your header-adm.php file.
Shobhana UpretiAuthor Commented:
No console is not showing any error, instead it just shows the full code.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Marco GasiFreelancerCommented:
No: you see the red circle with a 1 in the middle in the top bar at the right? That's an error.
Anyway, we need more php code and possibly a live link.
Shobhana UpretiAuthor Commented:
Code of header-adm.php :

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <?php wp_enqueue_script("jQuery"); ?>
    <script src="http://localhost/website/wp-content/themes/website/js/jquery.min.js"></script>
    <script src="http://localhost/website/wp-content/themes/website/js/jquery-1.9.1.js"></script>
    <link rel="stylesheet" type="text/css" href="http://localhost/website/wp-content/themes/website/css/dsgncnv.css">
    <link rel="stylesheet" type="text/css" href="http://localhost/website/wp-content/themes/website/astyle.css">
    <link rel="stylesheet" type="text/css" href="http://localhost/website/wp-content/themes/website/amenustyle.css">		
    <link rel="stylesheet" type="text/css" href="http://localhost/website/wp-content/themes/website/skeleton.css">
  </head>
   <body>
   <div class="container-1">
     <header>
       <div class="cont1div">
         <div class="lhdr-in">
           <a href="http://localhost/website/admin/login.php"><img src="http://localhost/website/wp-content/themes/website/images/logo.jpg" alt="" style="width:100%;height:100%;" /></a>
        </div>
        <div class="rhdr-in"></div>	
       </div>
     </header>
   </div>
   <div class="container">
    <header>
      <div class="main-nav fifteen columns" id="show-nav"><a href="#">Toggle Navigation</a></div>
      <div class="fifteen columns" id="close-nav"><a href="#">X</a></div>
      <div class="fifteen columns nav-bar">
        <?php wp_nav_menu(array('menu' => 'Admin Menu', 'container_class' => 'main-nav', 'container' => 'nav', 'theme_location' => 'secondary')); ?>
      </div>
    </header>

Open in new window

Julian HansenCommented:
<a href="http://localhost/website/admin/login.php">

Open in new window

Seems the above is the important bit - what is happening in login.php?
Brian TaoSenior Business Solutions ConsultantCommented:
I think the error is in your login page.  You're using AJAX to post the login request, right?  Please also post the code for the login page, or a link to the page, so that we can have a look at it.
By looking at the console, my guess is that:
- AJAX is used to send login request to the server
- server check the login OK and redirect to success.php (so the response is the parsed HTML)
- upon receiving the response, the handler on your login page logs the response
Shobhana UpretiAuthor Commented:
This is the script i am using in "alogin.php" :
		<script type="text/javascript">
			$(document).ready(function() {
				$('#btnalogin').click(function(e) {
					e.preventDefault(); // prevent the A target from being followed
					var mail = $('#txtusername').val();
					var pwd =  $('#txtpwd').val();
					data = { txtusername:mail, txtpwd:pwd };
					
					$.ajax({
						type: 'POST',
						url: 'alogincheck.php',
						data: data,
						cache: false
					})
					
					.done(function(data){
						console.log(data)
					})
				});
			});
		</script>		

Open in new window


code in alogincheck :
$mail = htmlentities($_POST['txtusername']);
$pwd =  htmlentities($_POST['txtpwd']);

$callquery = "select a_emailid, a_pwd FROM adm_dtls where a_emailid='{$mail}' and a_pwd='{$pwd}'";
$callresult = mysqli_query($connection , $callquery);
$callarr = mysqli_fetch_array($callresult);
if (!$callarr){ 
header('location: http://localhost/website/wp-content/themes/website/alogin.php');
exit();
}
else { 
header("Location: http://localhost/website/wp-content/themes/website/adm-main.php");
exit(0);
}

Open in new window

Ray PaseurCommented:
WordPress already has plugins that do almost everything you would need to do.  It might be much easier to use one of the existing code sets than to try to write your own.
https://wordpress.org/plugins/

It looks like you may be new to PHP programming.  If you're interested in learning the language, this article can help you find dependably good learning resources and more importantly, it can help you avoid the many simply terrible examples of PHP code that litter the internet.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

But that said, don't confuse WordPress and PHP programming.  WordPress is written in PHP, but it's not the place to learn professional programming.  It's an old system, and it's getting modernized, but there are still too many design and code antipatterns in WP to make it worthy of your attention.  Use it like a black box with minimal modification and only well-established plug-ins?  You'll probably be OK.  Open it up and try to find value in the code?  That way lies madness.
Shobhana UpretiAuthor Commented:
Thanks but I am not interested in using plugins but would rather like to do on my own just to get more control over the code.

Please guide where I m wrong in the above provided code ??
Julian HansenCommented:
Here is why you are having a problem. Look at your AJAX code

$.ajax({
	type: 'POST',
	url: 'alogincheck.php',
	data: data,
        cache: false
})

Open in new window


Now look at your alogincheck.php code
header('location: http://localhost/website/wp-content/themes/website/alogin.php');

Open in new window

and
header("Location: http://localhost/website/wp-content/themes/website/adm-main.php")

Open in new window

Remember your AJAX request works exactly like a separate browser - the AJAX call goes back to the server and the response from the server goes to the AJAX success handler - not the browser hosting the main page.
So when the server script issues a redirect it effectively tells the AJAX to redirect so the AJAX handler gets the redirected page not the browser - which is what you are seeing - the console shows the returned HTML from the redirect.

What you need to do is change your alogincheck to do as follows

if (!$callarr){ 
  echo 'http://localhost/website/wp-content/themes/website/alogin.php';
}
else { 
  echo 'http://localhost/website/wp-content/themes/website/adm-main.php';
}

Open in new window

Then in your AJAX call do this
$.ajax({
        type: 'POST',
        url: 'alogincheck.php',
        data: data,
        cache: false,
        success: function(url) {
               window.location = url;
        }
})

Open in new window

Not the only way to solve the problem but probably the quickest with what you have.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
...not interested in using plugins but would rather like to do on my own...
I respect your spirit of adventure and learning.  The first step would be to learn enough about PHP to write simple programs that do something useful.  The next step would be to explore the WordPress Codex and see how millions of others have contributed to the body of WP knowledge.  This is a journey and it will take you some time (probably months), but it will be rewarding if you can be patient with yourself and work diligently at your studies and experiments.  Best of luck with your project! ~Ray
Shobhana UpretiAuthor Commented:
how to redirect a page from a non-wordpress page like "alogin.php"
Ray PaseurCommented:
PHP can accomplish redirects with the header() function.
http://php.net/manual/en/function.header.php

You may want to read this article, too.  It can help avoid some of the common errors when redirecting a browser to a new page.
http://www.experts-exchange.com/articles/4423/Warning-Cannot-modify-header-information-headers-already-sent.html
Brian TaoSenior Business Solutions ConsultantCommented:
Exactly what I have guessed ^^.  You may just use Julian Hansen's comment as the solution.  It will work with least effort on what you already have.
Julian HansenCommented:
how to redirect a page from a non-wordpress page like "alogin.php"
You can't mix header redirects and AJAX. That is where you are going wrong - it has nothing to do with Word Press.

If you are going to use AJAX the redirect has to come from the client side - not the server side.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.