tags:

views:

549

answers:

8

Hi guys!

I want to do something like this:

page A has a link to page B
page B gets content from a database and processes it
the result from page B is displayed on a div in page A

please notice that I don't want to leave page A while page B processes the information of the database.

what I'm really trying to do is avoid using frames in the site, and i want to make the pages appear on a DIV.

is this even possible? :?

i'm guessing its kinda of a newbie question, but it's really bugging me and i don't even know where to start looking...

thanks for any help you might have...

+2  A: 

What you are looking for is JavaScript and AJAX.

Scott Bevington
I can't really use either... it's for a class in school and I really need to use PHP :S
Joum
I don't know that you can prevent the page load from happening with server side only.
MrChrister
+1  A: 

You want to do this client side rather than server side. I personally like JQuery

You might have to have a simple version of Page B. This is very common. Let me know if you would like some examples.

MrChrister
+10  A: 

You want AJAX!

AJAX will do that, but the steps will be a little different from what you describe

  • page A has a link that calls a javascript function
  • the javascript function makes an AJAX call to page B which gets content from a database and processes it
  • the result from page B is returned to the javascript function
  • page a displays it in a div
Tom Ritter
A: 

Short answer: no.

You will need some client side scripts or AJAX to do this.

barfoon
A: 

Check out XAJAX.

Its an AJAX library for PHP which will essentially do what you want.
It writes the Javascript required to handle the client side stuff.

You will do the following. 1. Include XAJAX libraries in your php pages. 2. Register your functions 3. Call them on client side from javascript by using xajax_phpfunctionName()

Adam Lerman
A: 

As every one else has said, this needs to be done through some kind of AJAX setup. If you're more familiar with PHP than JavaScript (or server-side scripting than client-side scripting), I'd also recommend you checkout XAJAX. [Which was posted by Adam as I was answering.]

Tim Lytle
+1  A: 

There is probably a zillion things wrong with this example, but it should get you down the right track :)

file a.php

<html>
<body>
<script type="text/javascript" src="stuff.js"></script>
Data to process please: <input type="text" name="datatoprocess" id="datatoprocess" />
<br />
<input type="button" value="Do Something" onclick="dostuff();" />
<hr />
<div id="resultarea">
</div>
</body>
</html>

file b.php

<?php

$message = '';
if ( isset( $_REQUEST['datatoprocess'] ) ) {
    $datatoprocess = $_REQUEST[ 'datatoprocess' ];
    if ( strlen( $datatoprocess ) > 0 ) {
     if ( $datatoprocess == 'foo' ) {
      $message = '<span style="color:#ffff00;background:#ff0000;">bar</span>';
     } else {
      $message = 'You sent me: &quot;'.htmlentities($datatoprocess).'&quot;';
     }
    } else {
     $message = 'That is really short data.';
    }
} else {
    $message = 'No data to process in request?';
}

echo $message;

?>

file stuff.js

// poor man quick and dirty ajax :|

function dostuff() {
    var xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
     alert ("Browser does not support HTTP Request.");
     return;
    } 
    var datatoprocess = document.getElementById('datatoprocess').value;
    var url="b.php"
        +"?datatoprocess=" + escape(datatoprocess)
    ;
    xmlHttp.onreadystatechange = function() {
     if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
      var AjaxAnswer = xmlHttp.responseText ;
      document.getElementById('resultarea').innerHTML = AjaxAnswer;
     }
    };
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}


function GetXmlHttpObject() { 
    var objXMLHttp=null;
    if (window.XMLHttpRequest) {
     objXMLHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
     objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp;
}
feihtthief
A: 

Well, what I originally wanted to do was to get PHP to dump the HTML (and PHP) contents of a page on to a DIV on another page (sounds really weird, I know). The way you do this is with this function:

ob_start();  
include($filename);  
$return_str = ob_get_contents();  
ob_end_clean();

If you do it like so, it will parse the PHP on the $filename file and return the result in a string. To make it dump AND show the content, simply remove the last line.

I'm kinda newb and this probably isn't the best way to do this, but it worked for me. Hope it helps.

Joum