Welcome to WebmasterWorld Guest from 54.224.230.193

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Help with dynamic form

How to use php include with onChange?

     
3:34 pm on Nov 28, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 8, 2002
posts: 179
votes: 0


Not having much of a clue about Javascript and PHP I'm trying to build a somewhat dynamic contact form. First I have the "normal" entries like name, email address, country. Then comes a dropdown with three options, let's call them A, B and C. Depending on what is chosen in this dropdown the rest of the form has different form fields. Can this be done with a php include? So let's say if option A is chosen a different file with the "rest" of the form is included than for option B? And acordingly for C?
Would be greatful for some hints...
5:37 pm on Nov 28, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 25, 2005
posts:111
votes: 0


I am almost certain you can accomplish this with AJAX.
6:17 pm on Nov 28, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 8, 2002
posts: 179
votes: 0


Yes, that's what I also came accross. Any idea where I can find examples?
9:34 pm on Nov 28, 2009 (gmt 0)

Full Member

5+ Year Member

joined:June 21, 2007
posts: 316
votes: 0


I'm quite a fan of jQuery for javascript bits like this. Especially if you're not very comfortable with native javascript. There are a few examples of what you want to accompolish. Just a quick idea of the code (if you do go the jQuery way)

say you have a div that holds where the includes should go called #includes you could include the following in a script block


$(function(){
$('select #dropdown').change(function(){
var val = $(this).val();
if(val == 1){
$('#includes').load('include1.php');
}
if(val == 2){
$('#includes').load('include2.php');
}
});
});

This is a very simple example and you will have to do some more work to get it working how you want it. One thing you should consider is what if a user doesn't have js enabled. Unless you know for certain that they will always have js on, you could look at doing something with the querystring. You could also use this to drive the ajax style functionality you are after.

8:11 pm on Nov 29, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:May 8, 2002
posts:179
votes: 0


Thanks, Tommy, I will definitely have a look at jQuery even though I'm not really goood with that stuff...
6:34 pm on Dec 2, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 22, 2005
posts:185
votes: 0