Welcome to WebmasterWorld Guest from 54.144.79.200

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Help with dynamic form

How to use php include with onChange?

     

bluemi

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

10+ Year Member



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...

smatts9

5:37 pm on Nov 28, 2009 (gmt 0)

5+ Year Member



I am almost certain you can accomplish this with AJAX.

bluemi

6:17 pm on Nov 28, 2009 (gmt 0)

10+ Year Member



Yes, that's what I also came accross. Any idea where I can find examples?

Tommybs

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

5+ Year Member



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.

bluemi

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

10+ Year Member



Thanks, Tommy, I will definitely have a look at jQuery even though I'm not really goood with that stuff...

NomikOS

6:34 pm on Dec 2, 2009 (gmt 0)