1

I've been searching around and nothing quite fits what I need. Here's what I'm trying to accomplish:

<a href="">TSConfig</a> <!-- When clicked, it loads tsconfig.php which contains a form and some php code into #main_content -->
<a href="">MVSN</a> <!-- When clicked, it loads mvsn.php which contains a form and some php code into #main_content -->
<a href="">Fulfillment</a> <!-- When clicked, it loads maint_fulfil.php which contains a form and some php code into #main_content -->

<div class="grid_3" id="main_content">

    <!-- PHP CONTENT TO LOAD HERE -->

</div>

I have tried different variants of JQuery examples to try and make this work, however most examples contain pulling an id from with the .php file and manipulating the url somehow, which is something I didn't want to do.

Many thanks and I hope I explained it right.

3
  • 9
    What about using $.load() ? Commented Sep 19, 2013 at 13:55
  • Uhm, this is as simple as .load Commented Sep 19, 2013 at 13:56
  • If it will help any, I have a plugin that may help with the url for .load. It's called myURL. Check it out. Withit calls are as simple as: $('#main_content').load($.myURL('index', 'directory', 'file.php'), function(response, status, xhr) { /* this is a callback function for .load */ }) Commented Sep 19, 2013 at 13:57

2 Answers 2

4

Change your html to this...

<a class="ajax-link" href="tsconfig.php">TSConfig</a> <!-- When clicked, it loads tsconfig.php which contains a form and some php code into #main_content -->
<a class="ajax-link" href="mvsn.php">MVSN</a> <!-- When clicked, it loads mvsn.php which contains a form and some php code into #main_content -->
<a class="ajax-link" href="maint_fulfil.php">Fulfillment</a> <!-- When clicked, it loads maint_fulfil.php which contains a form and some php code into #main_content -->

<div class="grid_3" id="main_content">

    <!-- PHP CONTENT TO LOAD HERE -->

</div>

and here's the script that loads the required pages for you...

$(function() {
    $("a.ajax-link").on("click", function(e) {
        e.preventDefault();
        $("#main_content").load(this.href);
    });
});

I added the class ajax-link to each of the links so you can apply the above script to them, but not any other links on the page.

Sign up to request clarification or add additional context in comments.

7 Comments

I was answering with the same answer!+1
This was fantastic for me, I much appreciate it. I did encounter another problem though, it's method is POST and it passes PHP_SELF to load the content on the same page -- once I submit my form, it loads the script and the output on a blank white page rather than inside index.php -- Any ideas what causes this?
Sorry, I don't understand. What is using the POST method, what is loading a blank page, and where does index.php come into this?
What you provided me loads the form. The form contains the following: <form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post"> which then loads tsconfig.php as a separate entity rather than contained within the div
The form submission will cause the whole page to reload, yes. If you don't want it to do that then you will need to handle the submit event for the form and make an ajax call, after constructing a data object that includes all the form elements and values, and then posts the returning data into the main content div. As you can see, that's not a small request, and it's a totally different question. I'd recommend asking a new question and showing the form markup inside the div.
|
0

jQuery.load

Last example is quite clear, too:

$( "#feeds" ).load( "feeds.php", { limit: 25 }, function() {
  alert( "The last 25 entries in the feed have been loaded" );
});

Comments

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.