Jump to content

How to do a form with "List/Menu" dynamically


lopes_andre

Recommended Posts

Hi,

 

Thanks for the reply's.

 

I will be a little bit more specific:

 

<select name="decision" id="decision">
    <option value="0" selected></option>
    <option value="a">Aprove</option>
    <option value="n">Not Aprove</option>
</select>

 

For example, If I choose the value "n" I need to show one new text box on the Form. I know that this must be done with JavaScript and using the "OnChange" on the "Select Box", but I need an example on how to do this.

 

Best Regards,

Link to comment
Share on other sites

It should like you don't need to reload the page for any reason, because AJAX would handle this.  As far as a clear example of how to do this... I would look at w3schools' AJAX examples: http://www.w3schools.com/php/php_ajax_intro.asp AJAX is actually really simple as it just passes a value to another .php file and the .php file does all of the work, spits it back to AJAX and Javascript writes the results to the output page.

 

Basically your flow will be:

 

Select option -> Call javascript function -> Pass value to .php file (using $_GET is easy) -> Receive value in .php file -> build another select element -> Echo the select -> Ajax gets it back -> JS writes it to the page...

Link to comment
Share on other sites

A lot more can go wrong using AJAX and you introduce 2 new syntaxes Javascript and XML!

 

I'd stick with a simple control flow in PHP that determines at which stage the user is as far as nagivating through the form. You can keep track by using sessions or hidden inputs <input type='hidden' name='stage' value='step1'/>

 

if (isset($_POST['stage']){

  $stage = $_POST['stage'];

}else{

  $stage = 0 // the beginning

}

 

if ($stage == 0){

    // print the first form

}elseif ($stage == 1){

// get values from database based on first form's values

// print second form using the database values

}elseif ($stage == 2){

// save all of the form to the database

}

Link to comment
Share on other sites

Well, if you wanna use javascript only, you'll have nothing on the server processing the forms. You'll need atleast PHP to process the postback and HTML to display the form on the browser.

 

If you're not familiar with HTML, PHP, Javascript, DOM, XML, etc, you can only learn so much in one day! I'd stick with a simple pure HTML & PHP  solution.

 

See my example in the previous post...

Link to comment
Share on other sites

A lot more can go wrong using AJAX and you introduce 2 new syntaxes Javascript and XML!

 

I'd stick with a simple control flow in PHP that determines at which stage the user is as far as nagivating through the form. You can keep track by using sessions or hidden inputs <input type='hidden' name='stage' value='step1'/>

 

if (isset($_POST['stage']){

  $stage = $_POST['stage'];

}else{

  $stage = 0 // the beginning

}

 

if ($stage == 0){

    // print the first form

}elseif ($stage == 1){

// get values from database based on first form's values

// print second form using the database values

}elseif ($stage == 2){

// save all of the form to the database

}

Who said anything about XML? :confused: AJAX just allows javascript and php to communicate with each other, that's it... Not a whole lot more can go wrong really... and the JS is extremely simple to implement, all of the coding is done via php.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.