0

I am not sure if what I am trying to do is possible but here it is.

I have a header, inside that header is a php include for "login.php"

On "login.php" is a link that takes the user to "forgot.php".

What I would like to do is, instead of the user being taken to "forgot.php" is to just refresh the page with "login.php" include replaced with "forgot.php" or do some sort of content switch out, sort of like using an Iframe.

I dont want to bring the user to a new page, I just want to switch out the content displayed inside my header.

Thanks for any help you can provide, code samples appreciated.

4 Answers 4

7

If you are trying to accomplish this without reloading the page you will need to use AJAX.

If you want to just keep the login.php you can perhaps do something like:

<a href="login.php?p=forgot">link</a>

with php something like

<?
if ( isset($_GET['p']) && $_GET['p']=="forgot") {
    include('forgot.php');
} else {
    include('login.php');
}
Sign up to request clarification or add additional context in comments.

4 Comments

Zach L ... You rock :) Just had to change the small bit of code but its now doing exactly what i wanted...props! Thank you everyone for all your help, I wish I could accept all answers that where along the same lines :/
Quick question, im adding a link back, what would the a href tag look like to clear the ?p=forgot
Right now I have it going to <a href="?p=login" because per the statement above it will auto replace with login.php so its a work-around for now
with this code you can just use <a href="login.php"> without the query string
1

PHP is parsed in it's entirety before the page is displayed in a user's browser, therefore, things such as onclick() or onsubmit() that are featured in JavaScript (a client-side language) are not available in PHP.

There would be a few solutions possible:

1) Use AJAX to submit a query to the server and replace the HTML content on the page with the result.

2) As you mentioned, use iFrames.

3) Have a hidden <div> on your login.php page that contains the HTML for forgot.php, and use a simple JavaScript onclick() method to swap the page contents. In this case, both "pages" would actually all be in the same file of login.php.

1 Comment

Maybe there is another way of presenting the "forgot.php" using JS onclick, and hiding/showing a div with the content inside
0

I can think of two things:

What I would do, assuming that the differences between your login.php and forgot.php aren't too different because you don't to change the page, is to put the html for the forgot.php just below the html for the login.php and hide the login.php html and show the forgot.php content.

example:

<div id = "login">
    <!-- Login HTML -->
</div>
<div id = "forgot" style = "display:none" >
    <!-- forgot password HTML -->
</div>
<input type="button" value="Forgot Password?" onclick="forgot()" />

Javascript:

function forgot(){
    document.getElementById('login').style.display='none';
    document.getElementById('forgot').style.display='block';
}

Otherwise, you could use an ajax call to the page and insert the necessary elements. This would create a noticeable pause.

1 Comment

Thats what I was thinking about, if you see my comment on answer 1. Thanks for the code snippit :)
0

You can't change the include statement from javascript because the script was already executed by the time you see your page in the browser.

Use ajax to dinamically change the content of the page without refreshing. If you're using jquery the code would be pretty simple:

<script type="text/javascript">
$(document).ready(function() {
    $('#link').click(function() {
        $.get('script.php', function(data) {
            $('#divOfContainer').html(data);
        });
    });
});
</script>
<div id="divOfContainer"><!-- the content to be fetched with ajax will be put here --></div>
<a href="#" id="link">Link</a>

4 Comments

lucky me I do have access to Jquery min
Sadly I know less about AJAX then I do php lol. Im muddling my way thoguht this learning project.
So it would look something like <script type="text/javascript"> $('#link').click(function() { $.get('forgot.php', function(data) { $('#headerdiv').html(data); }); }); </script> <a href="#" id="link">forgot</a>
it sounds more complicated then it is, trust me, i've been there. google up some tutorials and you'll be on the right track soon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.