Jump to content

[SOLVED] a few php questions


Recommended Posts

Hi.  I'm trying to do a few different things on different pages with PHP and wondering if I could get some help.

 

First, I'm using a php include script in which all other consecutive pages load into the first index page (index.php?page=x).  At the top is a small flash presentation.  When a link is clicked, the correct page is loaded, and the movie starts over, because basically it is being loaded again.  Is there any way around this?

 

Second, I am trying to make a simple login page.  On the front page, I would like to be able to allow a user to enter their username, which then would direct them to their folder (username = hello, folder is users/hello) and on that page, I will use a php password script I have.  How can I do this?

 

Thanks for any help!!!!!

 

(A)corn

Link to comment
Share on other sites

Thanks for the quick reply.

 

I tried using Iframes but it kept throwing off my layout and creating an empty white space under all my work.  I have a side bar of about 200px, a header of 250, and a menu under that of 20.  Under that is the main content.  I size the iframes for 100% (100% of the remaining space right?) but it throws everything off and with different browsers, creates 2 slidebars on the right - which is ugly.  If you have a better suggestion on how to fix it, I would love to hear it!

 

Where do I insert the header tag exactly?  How does it work?

 

Link to comment
Share on other sites

I'm not very experienced with iframes, so I can't say exactly. I'll let the other users answer that.

 

For your second question, I'm assuming you have one page that displays the form, and another page which processes the form. If my assumption is correct, put that code near the top on the processing page. So it'll look something like this:

 

form:

<form action="process.php" method="POST">
<input name="username" type="text">
</form>

 

processing page:

<?php
$username = $_POST['username'];
// If the form wasn't submitted, redirect the user to the form page
if(!isset($username)) {
   header('Location: form.php');
   exit();
}
else {
   header('Location: /' . $username . '/');
}
?>

 

Note that I haven't tested this yet. But the layout of the code should look similar to this.

Link to comment
Share on other sites

Hopefully I don't make this sound stupid.  The form will just have a username box.  Say the username is xj.  Submitting the form will bring the user to /users/xj.  That page will prompt for password. On process.php, what is form.php pointing too?  Is that the form, and it redirects back if the username is incorrect?

Link to comment
Share on other sites

Ha! This is so weird! I was just working on something like this.

I ended up using AJAX.

 

in the <head> of your website, add

 

<script language="javascript">
http = getHTTPObject();

function getHTTPObject(){
  var xmlhttp; 
  
  /*@cc_on
  
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
      try{
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(E){
      xmlhttp = false;
    }
  }
  @else
    xmlhttp = false;
  @end @*/

  if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    try {
      xmlhttp = new XMLHttpRequest();
    }catch(e){
      xmlhttp = false;
    }
  }

  return xmlhttp;
}

function loadPage(page){
  var url = page + ".php";

  http.open("GET", url, true);
  http.onreadystatechange = handleHttpResponse;

  http.send(null);
}

function handleHttpResponse(){
  if(http.readyState == 4){
    document.getElementById('content').innerHTML = http.responseText;
  }
}
</script>

 

That's basically all AJAX has to it. It's really not as hard as it sounds :)

 

Now, create a PHP script that outputs the content of what you want each page to have in plain text. Change "var url" under "function loadPage" in the script above to the location of this PHP script. (So, if, for example, the content is fetched from a sql database, have a php script that does that in a different file.) Also, change this document.getElementById('content').innerHTML = http.responseText; in the handleHttpResponse function to whatever <div> that you want the page to load in.

 

Now, on your site, have the navigation links look like this:

 

<a href="javascript:loadPage('THE PAGE YOU WANT TO LOAD MINUS THE .PHP')">The Link</a>

 

That should work :)

 

 

Example files:

 

<?php
// Script.php

echo "This is the content of the page that I want to load asynchronously so that I don't have to reload the flash header everytime.";

?>

 

 

The website index. (index.html)

<html>
<head>
<title>This is a simple page</title>
<script language="javascript">
http = getHTTPObject();

function getHTTPObject(){
  var xmlhttp; 
  
  /*@cc_on
  
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
      try{
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }catch(E){
      xmlhttp = false;
    }
  }
  @else
    xmlhttp = false;
  @end @*/

  if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    try {
      xmlhttp = new XMLHttpRequest();
    }catch(e){
      xmlhttp = false;
    }
  }

  return xmlhttp;
}

function loadPage(page){
  var url = page + ".php";

  http.open("GET", url, true);
  http.onreadystatechange = handleHttpResponse;

  http.send(null);
}

function handleHttpResponse(){
  if(http.readyState == 4){
    document.getElementById('content').innerHTML = http.responseText;
  }
}
</script>
</head>

<body>

<h1>This is my website</h1>
<div id="flash_header" style="width:500px;height:100px;padding:10px;margin:10px;">Pretend flash header.</div>
<div id="navigation"><a href="javascript:loadPage('script')">This is a link to the page I want to load asynchronously</a> into the div CONTENT.</div>
<div id="content">The content output by script.php will be put here.</div>

</body>

</html>

 

You get the idea. With a little imagination and better programming you can get some pretty good results. You can do alot more stuff than what you can do with iframes.

(plus, it's compliant unlike iframes).

Link to comment
Share on other sites

Thanks, both codes worked, but leave me with a few questions:

john010117:

The the user types in a username (which is essentially typing in a diretory)

that doesn't exist, how can I have the form recognize this and redirect the

user to a different page.

 

alecks:

The AJAX code worked great and was exactly what I was looking for!

Is it cross broser-compatible, and what exactly is:

 

/*@cc_on

 

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    }catch(e){

      try{

      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

    }catch(E){

      xmlhttp = false;

    }

  }

  @else

    xmlhttp = false;

  @end @*/

 

??

Thanks,

Acorn

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.