Creating a dynamic textbox on a form

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
rpamballa
Forum Newbie
Posts: 2
Joined: Tue Aug 12, 2008 1:58 pm

Creating a dynamic textbox on a form

Post by rpamballa »

Hi,

I very php development, and I am developing an html form. I have field called "dry ice weight" which has to appear only when the user requires to enter.
I am writing a pseudo code for what I want, can someone suggest me how to code it php? Any help is greatly appreciated.

if(dryice== yes)
{
create the text box to take the dry ice weight;
}else{
do nothing
}
EducatedFool
Forum Newbie
Posts: 3
Joined: Tue Aug 12, 2008 1:00 pm

Re: Creating a dynamic textbox on a form

Post by EducatedFool »

something like this?

<?php
if($dryice) {
echo '<input type="text" name="dryiceweight" value="">';
}
?>
rpamballa
Forum Newbie
Posts: 2
Joined: Tue Aug 12, 2008 1:58 pm

Re: Creating a dynamic textbox on a form

Post by rpamballa »

I have an html form where I have to include this textbox based on the selection made form the list consisting of options "yes" or "No" on the same form.

For instance if I select to yes from the list the text for dryice weight should appear else if I select No the box should not appear.
pkbruker
Forum Commoner
Posts: 32
Joined: Sun Aug 03, 2008 9:36 am
Location: Oslo, Norway

Re: Creating a dynamic textbox on a form

Post by pkbruker »

If you wanna do it without reloading the form (which would be the best idea), you'll have to use Javascript, not PHP. What you can do is something like this:

1. Place the entire form field within a div tag, give the tag the id "dry_ice" and make it invisible. Like this:

Code: Select all

<div id="dry_ice" style="display: none;">Here goes the form field</div>
2. Add

Code: Select all

onchange="javascript&#058;showDryIce()"
Within the form field which should cause the form field which should trigger the dry ice field. If there are other conditions than simply "onchange", put these in the showDryIce() function. (&#058; = :).

3. Function showDryIce() should look like this:

Code: Select all

 
function showDryIce() {
  // Makes div "dry_ice" visible.
  document.getElementById('dry_ice').style.display='inline';
}
 
Post Reply