You can't do it with PHP. PHP is a server side technology. It does not have any active communication with the user. It takes a page, processes it into HTML, and sends it to the user. Once at the user end, the user has to do w/e on the page, send a new request to the server, which will send back new HTML.

To do this, you need Javascript, and specifically for this situation, AJAX. Google AJAX, or for simplicity, pick up a framework like jQuery, which makes doing AJAX requests easier.

Calle81

11-05-2010, 05:08 PM

Yeah, I know I need AJAX, that is what this thread is about. I just can't seem to get any AJAX coding working. Been playing around with a few tutorials but no luck so far.

kbluhm

11-05-2010, 05:56 PM

Here's a simple AJAX request using jQuery to hijack click events, process the data with `ajax/my-form.php`, and inserts the resulting HTML into a defined div.

<script>
// Hijack the form submission
$( '#my-form' ).submit( function() {
// not too sure of a better solution for handling scope within the callback
$this = $( this );
$.post(
// The URL to process the form data and spit out HTML
$this.attr( 'action' ),
// The serialized form data, treated as $_POST
$this.serialize(),
// grab the response and do with it what you will
function( response ) {
$this.children( '#form-response' ).html( response );
}
);
// Halt the default action
return false;
} );
</script>

Calle81

11-07-2010, 04:04 PM

Thanks for you suggestions, seems to work out well so far. Gonna test it a bit more to see that everything is ok.

Calle81

11-07-2010, 05:10 PM

Only problem with this code seems to be that it doesn't run the my-form.php on the first page load. So first time you enter the page is empty.

DrDOS

11-07-2010, 05:28 PM

Here's what I did for a site without a database. I made a php page that contained the onload entries for the form, just the entries you see when you first load the page. All the folders were checked by the index.php to see if they had a version of this entries.php, if not it was copied to the folder. Then when the form was submitted it changed the entries in the folder copy. So when the page reloaded after submission the form had all the user made entries. Of course the same thing could be done with a database, with some modifications.

kbluhm

11-07-2010, 05:45 PM

Only problem with this code seems to be that it doesn't run the my-form.php on the first page load. So first time you enter the page is empty.

That's not really a problem. It's really not that difficult. You have full control over whatever you'd like to display within that div: