How do I echo different forms for different selected options

Discussion in 'PHP' started by adsegzy, May 15, 2012.

  1. #1
    Hello there,

    Am developing a site where members can add events. On the add_event.php page, the only field is Event Type (which includes Seminar, Wedding, Birthday, etc). The form for Seminar is different from the form for wedding etc. On selecting the type of event they want to add, I want the form for the selected event type to appear below the Event Type field without refreshing or redirecting the page. How do I do it? Can only php scripts do this or i'll need other languages like javascript etc?

    Thanks in advance
     
    adsegzy, May 15, 2012 IP
  2. artus.systems

    artus.systems Well-Known Member

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    103
    #2
    You need javascript
     
    artus.systems, May 16, 2012 IP
  3. 137th Gebirg

    137th Gebirg Active Member

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    ^^^ I think some more specifics are needed. Sounds like a job for AJAX - call a JavaScript function on the onChange event handler of the event type dropdown. That function will perform an AJAX call to a PHP file containing the custom form based on the selected type delivered to it in the parameter string. Remember to send a random number along with the call to the PHP file to make sure you get a fresh, un-cached page from the server. Good luck!
     
    137th Gebirg, May 16, 2012 IP
  4. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #4
    Yes, you will need to use JavaScript. I don't think any AJAX libraries are necessary like suggested above if it's only a few forms you're dealing with - the forms could be loaded when the page is generated.

    You will need to create a form with a drop-down menu (using <select> tags) with the options Seminar, Wedding, Birthday, etc.
    In your JavaScript file, you will create a function that is called whenever you select one of the options in the form above. This function will generate a form somewhere on the webpage based on your selection from the drop-down menu. It's pretty simple, do a bit of JavaScript reads.

    Remember, any change that happens in the browser without refreshing or redirecting
    ALWAYS requires JavaScript. You must refresh or redirect to access a PHP script.
     
    HostPlanz, May 16, 2012 IP
  5. 137th Gebirg

    137th Gebirg Active Member

    Messages:
    70
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #5
    Yeah, AJAX may be overkill for this. Actually, come to think of it, what might even be easier is to store pre-rendered forms in individual hidden DIV layers. Once the selection is made, make the corresponding div layer visible. Might really be the easiest way to go over all the options.
     
    137th Gebirg, May 17, 2012 IP
  6. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #6
    Or you can store the forms' HTML code in JavaScript using variables/arrays/whatever, and based on the user's selection, output the respective HTML code into the div using the innerHTML object etc.
     
    HostPlanz, May 17, 2012 IP