Help with showing a form if drop-down value = "Other"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikeashfield
Forum Contributor
Posts: 159
Joined: Sat Oct 22, 2011 10:50 am

Help with showing a form if drop-down value = "Other"

Post by mikeashfield »

Hey, I'm current creating a system in PHP that I want to be able to add orders to the system, and the person processing it will select the customer from a drop down, if the customer does not already exist I would love for there to be an "Other..." option in the drop-down (at the bottom) if the customer isn't in the clients table in the database that will show a hidden form that will allow a client to be added at the same time as order. I would also like a second hidden from to be shown at the same time to allow the person processing the order to add the clients product (for repair) as it will not have a product for the client if the client does not exist. But if they do select a client, then it should search the database table products client_id field for a list of their products and display this in a drop down.

How can I do this? Thanks.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Help with showing a form if drop-down value = "Other"

Post by social_experiment »

AJAX is a good option (not sure about any other technologies). I'll explain at the hand of ajax example i use; Pass a variable using $_GET which is retrieved by the page it is passed to and based on the result, you display / create your other content.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Help with showing a form if drop-down value = "Other"

Post by twinedev »

For the "selecting other issue":

on the <select> add a onclick="toggleOther(this)"

On the text input, start it out with a style of display: none
Also, the second form (will needs to be the same form, but going with your wording), which will probably be just another <fieldset> that is also defaulted to a style of display=none;

now in the function toggleOther(elem) you would do something like the following:

Code: Select all

if  elem.value == "(your value for <option>Other)" {
    document.elementGetById("id on the Other text input").style.display = "block";
    document.elementGetById("id on the fieldset for 'second' form").style.display = "block";
}
else {
    document.elementGetById("id on the Other text input").style.display = "none";
    document.elementGetById("id on the fieldset for 'second' form").style.display = "none";
}
Ok,. since it is in the PHP code second, a reminder here that when processing the form, if they don't select "Other", skip over all the inputs that you are toggling

And the actual JS code, need to test to make sure I typed it correctly off the top of my head... But that should be enough that you can get started and things to look up if you don't understand them (or ask here).
Post Reply