1

I'm currently working on a project that saves details of different types of objects to a database e.g. book, webpage and journal article. To save the different attributes of these objects I am trying to get different forms to display that depend on the selection in a drop down menu.

Here's the dropdown menu:

<div class="dropdown">
        <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
            Select Reference Type...
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
            <li role="presentation"><a role="menuitem" tabindex="-1" href="book.php">Book</a></li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="journal.php">Journal</a></li>
            <li role="presentation"><a role="menuitem" tabindex="-1" href="webpage.php">Webpage</a></li>
        </ul>
    </div>

How to I get a different form to load on screen without redirecting to a different page. I've been trying to do this in php but I get the feeling that php isn't the right way of going about doing this. Also, apologies in advance as I have no previous experience in Javascript, AJAX or jQuery.

3 Answers3

0

In order to accomplish your goal, you would need the following

A menu in your HTML page like this one. Let's say that the values of the select-list will be your PHP files, just as it's shown below

<!-- HTML -->
<p>Select reference type...</p>
<!-- This is the menu -->
<select id="menu">
  <option value="book.php">Book</option>
  <option value="jurnal.php">Jurnal</option>
  <option value="webpage.php">Webpage</option>  
</select>

<!-- This is the container for your HTML content -->
<div id="content"></div> 

You'll need this snippet to make AJAX requests to your server

// JavaScript
var s = document.getElementById('menu');     // reference to the <select> menu
var c = document.getElementById('content');  // reference to the content <div> 
s.onchange = function(){                     // hook the change event on <select>
  xhr = new XMLHttpRequest();
  var url = 'your-domain/' + this.value;     // here we're passing the selected value
  xhr.open("GET", url, true);
  xhr.onreadystatechange = function () { 
    if (xhr.readyState == 4 && xhr.status == 200) {  // wait for the response
      c.innerHTML = xhr.responseText;                // here it comes; populate the <div> 
    }
  };
  xhr.send(); 
};

And these are your PHP files on the server-side; they may contain any valid PHP / HTML code

// PHP
// book.php or jurnal.php or webpage.php
echo 'anything';
hex494D49
  • 9,109
  • 3
  • 38
  • 47
  • From my understanding, it seems your way of doing this differs from the solution given by @Benjamin in that there is less functions like 'bookInclude()' to write? – TheCurlyProgrammer Aug 28 '14 at 12:06
  • 1
    @TheCurlyProgrammer Well, these are quite a different solutions. Mine doesn't require any other external library (jQuery or any other); mine doesn't require to change or inject inline javascript, mine doesn't have any other useless function included; and finally my snippet is well commented ;) All you need to learn are these ten lines of code... for any other Ajax requests ;) – hex494D49 Aug 28 '14 at 12:11
  • @TheCurlyProgrammer If you want to learn, then you should learn standards first; you shouldn't include 200 kb of some external library that you don't understand; it's better to learn ten lines of standard code (which is JavaScript in this case) than copy-pasting hunders of lines that you don't get. Think about it ... ;) – hex494D49 Aug 28 '14 at 12:16
  • 1
    That's a very good point you raise! Is there any online resources that you know of that could help better explain javascript/AJAX standards? Also, where would the AJAX request above be saved? As in within 'index.php' or in a seperate file? – TheCurlyProgrammer Aug 28 '14 at 12:25
  • @TheCurlyProgrammer This is one of the best https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest And follow the links on footer. Remember, Ajax is easy and it's cool. Folloe the content there and you'll be able to use it within a week without having to over-load your files with useless libraries. To be fair, there's just grait libraries out there but if you don't need them you shouldn't them. And finally, you should at least know what each library does; what's the aim of usinig it... – hex494D49 Aug 28 '14 at 12:31
  • @TheCurlyProgrammer Your Ajax request, the JavaScript snippet from my answer should be placed within `` section; just as any other JavaScript code. – hex494D49 Aug 28 '14 at 12:38
  • I've tried both solutions and I can't seem to get your solution to work. That's down to my own inexperience and unfortunately I don't have the time to properly get my teeth into it (looming deadlines). Thanks very much for the help though. I'll be getting into that online resource post-deadline :D – TheCurlyProgrammer Aug 28 '14 at 12:58
  • @TheCurlyProgrammer Make an HTML file using [this content here](http://jsbin.com/qeyupemaroqa/1/edit); check the `var url` for having correct path; then, make a PHP file with any valid content you want (`echo` something at the end) and call the HTML file. That's it. – hex494D49 Aug 28 '14 at 13:14
0

Okay, so without knowing the rest of your code, I would suggest that the best option would be to have the different forms in separate documents. For example

HTML

<div class="dropdown">
    <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown">
        Select Reference Type...
        <span class="caret"></span>
    </button>
    <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
        <li role="presentation"><a onclick="bookinclude()" role="menuitem" tabindex="-1" href="book.php">Book</a></li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="journal.php">Journal</a></li>
        <li role="presentation"><a role="menuitem" tabindex="-1" href="webpage.php">Webpage</a></li>
    </ul>
</div>
<div id="contentwrapper">
    Some Initial Content Here
</div>

Javascript

function bookinclude(){
  $("#contentwrapper").fadeOut(400);
  setTimeout(function(){$("#contentwrapper").load("book.php").fadeIn();}, 400); 
};

And remember to include Jquery!In the head of your HTML:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Basically, on the click of the book link, it will load the book.php into the contentwrapper div.

I think this is what you want? All you need to do is replicate the function, and the link, but replace the book with journal, and with webpage.

Hope this helps!

Benjamin
  • 347
  • 1
  • 12
  • That's what i'm looking to do! As I haven't had any experience in js before, i'm wondering is the javascript to be saved in a file such as 'scripts.js' and then included on the html doc as ``? – TheCurlyProgrammer Aug 28 '14 at 12:01
  • The JavaScript doesn't need loading separately :) You do however need to load the Google hosted jQuery (with the link I've put in) and through that, you don't need to put the script in as one of your files. However, you can if you want to, you just need to download jQuery and load it as you've specified. – Benjamin Aug 28 '14 at 12:03
  • Or do you mean the Javascript I've specified? If you mean the one I've put in, all you need to do is put it in the head like this `` and it will work :) – Benjamin Aug 28 '14 at 12:08
  • Thanks! It was where to put the function you've put in. I'm trying this now. – TheCurlyProgrammer Aug 28 '14 at 12:14
  • @TheCurlyProgrammer sorry, I can't comment on the other post yet -.-' Whilst he does raise a good point, maybe this is a good way into learning how to use jQuery, it's a nice starting point. The points hex494D49 raises are absolutely true (Touche good sir) but if you are only using a few drop downs, it's hardly the end of the world? :3 I wish I'd started using jQuery sooner, and if you're doing the project from start to finish, at some point, you'll probably require jQuery anyway to make life easier? :) – Benjamin Aug 28 '14 at 12:29
  • I guess it's just an alternative way? :) – Benjamin Aug 28 '14 at 12:30
0

you can also keep all the 3 forms on the same page and can hide or show them on the basis of the dropdown selection.

Lets say there are 3 forms: 1. book 2. webpage 3. journal

Create forms for all three in html, and by default keep all of them hidden(for that we will use a class "display-none"), after that detect change in the dropdown on basis of whom the form populates and do action as required. Please look at the following peace of code, it might help:

<style type="text/css">
    .display-none {
        display: none;
    }
</style>

<select id="dropdown" onchange="myFunction()">
    <option value="-1">-- Select --</option>
    <option value="book">Book</option>
    <option value="webpage">Webpage</option>
    <option value="journal">Journal</option>
</select>
<form id="book" class="display-none" method="post" action="where-you-want-to-post/book">
    <input type="submit" value="Submit Book" />
</form>
<form id="webpage" class="display-none" method="post" action="where-you-want-to-post/webpage">
    <input type="submit" value="Submit Webpage" />
</form>
<form id="journal" class="display-none" method="post" action="where-you-want-to-post/journal">
    <input type="submit" value="Submit Journal" />
</form>

<script type="text/javascript">

    function myFunction() {
        var allForms = document.getElementsByTagName('form');
        var dropdown = document.getElementById("dropdown");
        if (dropdown.value != "-1") {
            var form = document.getElementById(dropdown.value);
            for (var i = 0; i < allForms.length; i++) {
                allForms[i].setAttribute("class", "display-none");
            }
            form.setAttribute("class", "");
        }
    }
</script>

Demo: http://jsfiddle.net/oynjj3jn/