0

my index.php page has something like this:

if(isset($_GET['page'])) {
        switch ($_GET['page']) {
            case 'login':
                include('login.php');
            break;
            case 'log_out':
                $_SESSION['loggedin'] = false;
                include('loggedout.html');
            break;
            case 'profile':
                include('profile.php');
            break;
            case 'contact_us':
                include('contact.html');
            break;
            default:
                include('home.html');
            break;
        }
        }  else {
            include('home.html'); 
        }

Now I can easily call on a new page when a link is pressed or form is submitted i.e:

<a href="index.php?page=home">Home</a> <a href="index.php?page=contact_us">Contact Us</a> 

but what if I had an all in one form... It would need to be able to send a request through PHP code to my index.php to show the logged in page or reshow the login.php page but I cant seem to find anything that would allow me to send a request in php script i.e:

<?php 
    if(..) {
        send request to index.php i.e index.php?page=profile
    }
?>

The reason I need this is my index.php uses divs as formatting and thus if I dont request pages through index.php they are displayed in a new html (i.e it doesnt have my navigation bars etc)

As per a comment by @HydraIO and others how would this be done using JQuery?

Here is some code:

lgoin.php:

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title>Login</title>
        <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1"
        />
        <!--
            //thanks http://decoraora.com/fototapete_115x175.php for the image http://decoraora.com/fototapete/images/664.jpg -->
        <style type="text/css">
        .table { 
            background: url("bg.jpg") no-repeat; 
            width: 600px;
            height: 250px;
        }
        </style>
    </head>

    <body>
        <?php           
            if (isset($_POST['formLogin'])) {
                    $un = $_POST['username'];
                    $pwd = $_POST['password'];
                    if($pwd!=''&&$un!='') {
                        if (md5($pwd)==='29ef52e7563626a96cea7f4b4085c124') {
                            $_SESSION['loggedin'] = true;
                            $_SESSION['login_error'] = 0;
                            $_SESSION['username'] = $un;
                            $_SESSION['password'] = $pwd;
                            //include('profile.php');
                            //header("Location: index.php/page=profile");
                        //header("Location: http://localhost/index.php?index.php?page=profile");

                        } else {
                            $_SESSION['login_error'] = 1;
                            echo 'this will dipslay in its own page';
                            //include('login.php');
                        }
                    }else {
                        $_SESSION['login_error'] = 2;
                            echo 'display in its own page';
                        //include('login.php');

        ?>
        <form action="login.php" method="post">
            <table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
                <tr>
                    <td colspan="2" align="center">Login for CC Chat</td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="text" name="username" maxlength="50"/></td>
                    <td><input type="password" name="password" maxlength="50"/></td>
                </tr>
                <tr>
                    <td><a href="index.php?page=contact_us">Forgot your password?</a></td>
                    <td><input type="submit" name="formLogin" value="Login"/></td>
                </tr>
                <?php 
                    if(isset($_SESSION['login_error'])) {
                        switch ($_SESSION['login_error']) {
                            case 1:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password does not match</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            case 2:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password and/or Username cannot be empty</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            default:
                            break;
                        }
                    }
                    ?>
                <tr>
                    <td colspan="2" align="center">&copy; CopyLeft David Kroukamp  11039662 2013 - 2015</td>
                </tr>
            </table>
        </form>
        <?php 
        }
        }else {
        ?>
        <form action="login.php" method="post">
            <table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
                <tr>
                    <td colspan="2" align="center">Login for CC Chat</td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="text" name="username" maxlength="50"/></td>
                    <td><input type="password" name="password" maxlength="50"/></td>
                </tr>
                <tr>
                    <td><a href="index.php?page=contact_us">Forgot your password?</a></td>
                    <td><input type="submit" name="formLogin" value="Login"/></td>
                </tr>
                <?php 
                    if(isset($_SESSION['login_error'])) {
                        switch ($_SESSION['login_error']) {
                            case 1:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password does not match</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            case 2:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password and/or Username cannot be empty</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            default:
                            break;
                        }
                    }
                    ?>
                <tr>
                    <td colspan="2" align="center">&copy; CopyLeft David Kroukamp  11039662 2013 - 2015</td>
                </tr>
            </table>
        </form>
        <?php
        }
        ?>
    </body>

</html>

index.php:

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>CC Chat</title>
        <meta 
        http-equiv="content-type" content="text/html; charset=iso-8859-1" 
        />
        <!-- 
        Thanks too http://stackoverflow.com/a/7851347/1133011 for the help
        on layout which acts more like frames but avoids them and using divs. As frames have 
        known draw backs see here http://stackoverflow.com/questions/4600648/frames-with-php we
        should thus rather use include in php
        !-->
        <style type="text/css" media="screen">
        /* Generic pane rules */
            body { margin: 0 }
            .row, .col { overflow: hidden; position: absolute; }
            .row { left: 0; right: 0; }
            .col { top: 0; bottom: 0; }
            .scroll-x { overflow-x: auto; }
            .scroll-y { overflow-y: auto; }

            .header.row { height: 75px; top: 0; background-color: #E5E5E5; }
            .menu.row { height: 75px; top: 75px; background-color: #EDEDED;  }
            .body.row { top: 150px; bottom: 50px; background-color: #F5F5F5; }
            .footer.row { height: 75px; bottom: 0; background-color: #EBEBEB; }

            A:link {text-decoration: none}
            A:visited {text-decoration: none}
            A:active {text-decoration: none}
            A:hover {font-size:24; font-weight:bold; color: red;}
        </style>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    </head>
    <body>
        <div class="header row">
            <?php 
                include("header.html"); 
            ?>
        </div> 
        <div class="body row scroll-y">
            <?php
        if(isset($_GET['page'])) {
                        switch ($_GET['page']) {
                            case 'login':
                                include('login.php');
                            break;
                            case 'log_out':
                                $_SESSION['loggedin'] = false;
                                include('loggedout.html');
                            break;
                            case 'profile':
                                include('profile.php');
                            break;
                            case 'contact_us':
                                include('contact.html');
                            break;
                            default:
                                include('home.html');
                            break;
                        }
                }  else {
                    include('home.html'); 
                }
            ?>
        </div> 
        <div class="menu row">
            <?php 
                include("nav.php"); 
            ?>
        </div> 
        <div class="footer row">
            <?php 
                include("footer.php"); 
            ?>
        </div>
    </body>
</html>

UPDATED with JQuery attempt in if statement which checks password hash:

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <title>Login</title>
        <meta http-equiv="content-type"
        content="text/html; charset=iso-8859-1"
        />
        <!--
            //thanks http://decoraora.com/fototapete_115x175.php for the image http://decoraora.com/fototapete/images/664.jpg -->
        <style type="text/css">
        .table { 
            background: url("bg.jpg") no-repeat; 
            width: 600px;
            height: 250px;
        }
        </style>    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

    </head>

    <body>
        <?php           
            if (isset($_POST['formLogin'])) {
                    $un = $_POST['username'];
                    $pwd = $_POST['password'];
                    if($pwd!=''&&$un!='') {
                        if (md5($pwd)==='29ef52e7563626a96cea7f4b4085c124') {
                            $_SESSION['loggedin'] = true;
                            $_SESSION['login_error'] = 0;
                            $_SESSION['username'] = $un;
                            $_SESSION['password'] = $pwd;
                            //include('profile.php');
                            //header("Location: index.php/page=profile");
                            //header("Location: http://localhost/index.php?index.php?page=profile");
                            //exit();
        echo "<script type='text/javascript'>";
        echo "$.get('index.php?page=profile', function(data) {
  $('.body.row').html(data);
  alert('Load was performed.');
});";  
echo "</script>";
                        } else {
                            $_SESSION['login_error'] = 1;
                            echo 'this will dipslay in its own page';
                            //include('login.php');
                        }
                    }else {
                        $_SESSION['login_error'] = 2;
                            echo 'display in its own page';
                        //include('login.php');

        ?>
        <form action="login.php" method="post">
            <table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
                <tr>
                    <td colspan="2" align="center">Login for CC Chat</td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="text" name="username" maxlength="50"/></td>
                    <td><input type="password" name="password" maxlength="50"/></td>
                </tr>
                <tr>
                    <td><a href="index.php?page=contact_us">Forgot your password?</a></td>
                    <td><input type="submit" name="formLogin" value="Login"/></td>
                </tr>
                <?php 
                    if(isset($_SESSION['login_error'])) {
                        switch ($_SESSION['login_error']) {
                            case 1:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password does not match</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            case 2:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password and/or Username cannot be empty</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            default:
                            break;
                        }
                    }
                    ?>
                <tr>
                    <td colspan="2" align="center">&copy; CopyLeft David Kroukamp  11039662 2013 - 2015</td>
                </tr>
            </table>
        </form>
        <?php 
        }
        }else {
        ?>
        <form action="login.php" method="post">
            <table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
                <tr>
                    <td colspan="2" align="center">Login for CC Chat</td>
                </tr>
                <tr>
                    <td>Username:</td>
                    <td>Password:</td>
                </tr>
                <tr>
                    <td><input type="text" name="username" maxlength="50"/></td>
                    <td><input type="password" name="password" maxlength="50"/></td>
                </tr>
                <tr>
                    <td><a href="index.php?page=contact_us">Forgot your password?</a></td>
                    <td><input type="submit" name="formLogin" value="Login"/></td>
                </tr>
                <?php 
                    if(isset($_SESSION['login_error'])) {
                        switch ($_SESSION['login_error']) {
                            case 1:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password does not match</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            case 2:
                                echo '<tr>';
                                echo '<td>';
                                echo '<font color="red">Password and/or Username cannot be empty</font>';
                                echo '</td>';
                                echo '</tr>';
                                $_SESSION['login_error']=0;
                            break;
                            default:
                            break;
                        }
                    }
                    ?>
                <tr>
                    <td colspan="2" align="center">&copy; CopyLeft David Kroukamp  11039662 2013 - 2015</td>
                </tr>
            </table>
        </form>
        <?php
        }
        ?>
    </body>

</html>

I see the alert but am redirected to a blank html like before (even than before at least the form was reloaded

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 3
    Php is server side and thus cannot do what you are trying to do. try jquery .ajax() – Hydra IO Apr 11 '13 at 17:52
  • 2
    You want to use jQuery for this. http://api.jquery.com – Jordan Apr 11 '13 at 17:53
  • @HydraIO +1 thanks updated question... How would I achieve this via JQuery – David Kroukamp Apr 11 '13 at 17:53
  • 1
    http://api.jquery.com/jQuery.get/ > Load data from the server using a HTTP GET request. – Hydra IO Apr 11 '13 at 17:56
  • .get() will actually send the GET request right off the bat. – Hydra IO Apr 11 '13 at 17:56
  • @HydraIO so `$('.result')` in the example `$.get('ajax/test.html', function(data) { $('.result').html(data); alert('Load was performed.'); });` I would adapt to insert my my index.php div id? – David Kroukamp Apr 11 '13 at 18:00
  • @DavidKroukamp that's correct! – Hydra IO Apr 11 '13 at 18:11
  • @HydraIO ahh that was the answer just had to change .body.row to the body of current html. Please write as answer and I will upvote and accept – David Kroukamp Apr 11 '13 at 18:39
  • I would not rely on javascript for anything that you site cannot live without. I know it sounds odd, but there are millions of users with scripting turned off. Unless you know your target audience very well (let's say you are programming a game, where people know they won't be able to play it unless they allow scripts) or don't intend for this to ever become big, make sure your site makes sense without javascript, then add it to make it look cool. – DudeOnRock Apr 11 '13 at 18:40
  • @DudeOnRock I see your very valid pint but at this stage Im still a n00b so you know dont want to go in over my head :) – David Kroukamp Apr 11 '13 at 18:41

2 Answers2

0

It sounds like you might want to evaluate all the data inside your index page first and do a page redirect by setting a header inside php to the same index page with the get query updated to whatever you want it to be.

header("Location: http://www.example.com/index.php?page=profile");
exit();

make sure you don't output any html with echo or any other way before you do the redirect. If your script heavily uses echo before that you can look into outputbuffering your echos.

DudeOnRock
  • 3,685
  • 3
  • 27
  • 58
  • Thanks looking into this updated code example with commented header... which gave me error but i think thats because of the echo thing – David Kroukamp Apr 11 '13 at 18:04
  • @DavidKroukamp: check out output buffering if you are using a lot of echos before you do the redirect – DudeOnRock Apr 11 '13 at 18:07
  • Please see my login.php code along with the commented header in the if statement... it gives error *Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\login.php:22)* – David Kroukamp Apr 11 '13 at 18:08
  • @DavidKroukamp: after line 1 you turn off php and output html. Turn output buffering on before that. Then display what is in the buffer at the end of your script. – DudeOnRock Apr 11 '13 at 18:10
  • @DavidKroukamp: To understand headers I would start with a blank php page and just set a header-redirect and see if that is the desired outcome. If that is the case, play around with output-buffering. – DudeOnRock Apr 11 '13 at 18:15
-1

PHP is a server-side language, and not dynamic. You want to use javascript (jQuery in my example).

$_GET = <?php echo json_encode($_GET); ?>;
if($_GET['val'] == some_value){
  $.get(index.php?getvar=getval, {
    complete: function(){
      $('#div').val("YOUR OUTPUT"); // *
    }
  });
}

*This can be any code you feel like, you could replace the div with the entire php file if you chose to

TheMonarch
  • 577
  • 1
  • 5
  • 19
  • what exactly would I put in `val("YOUR OUTPUT");`? As really I would just want index.php to include my login.php or profile.php no outputting the login.php will check a session error variable for any needed output. Also would this be correct `$('.body.row').val(..)`? – David Kroukamp Apr 11 '13 at 18:11