[thelist] Re: PHP: Adding controls on the fly

Tony Light TonyLight at blueyonder.co.uk
Tue Dec 31 14:33:01 CST 2002

[ Picked text/plain from multipart/alternative ]
On  Mon, 30 Dec 2002, Atul wrote:

I am trying to add controls (input tag) to an HTML form on the fly in PHP.
During all this control addition, I also have to keep the existing
information in the form flowing through it. There are several mundane
approaches to do that. I was wondering if someone else has encountered this
problem.I know this can be achieved easily using Java Servlets. What is the
most effective way to accomplish this in PHP?


I'm not 100% sure that I have understood your requirement - as the terms 'on the fly' and PHP seem slightly contradictory to me.

If you want to change the form in some way depending on entries or choices the user makes while completing the form, then Javascript looks a good choice - but then you need some way to identify and cater for the non-Javascript enabled users.

To get PHP to change the form involves a round trip to the server and back - effectively generating a new page, based on the present contents of the form.  The user needs to invoke this by pressing a Submit button (unless you put some Javascript code in there to do the submit - in which case why not get Javascript to change the form without going back to the server).  Either way, because the page needs submitting and reshowing, it isn't 'on-the-fly'.

If my reading of your question is correct, you want to do the round trip to the server, calling the same page back, but now with a slightly different looking form.  When the page comes back you have lost the values that the user had already entered in the input fields?  If so, the bit of code below is for a sample form containing each type of input control along with the PHP code that will ensure that the form contains any previously entered data each time it is reshown by hitting the Submit button.

Watch the wrap....

 extract($_POST); // (or $HTTP_POST_VARS in older versions) in case register_globals is off
<form name="form1" method="post" action="">
 <input type="text" name="textfield" value="<?php echo $textfield ?>">
 <input type="checkbox" name="checkbox" value="checkbox" <?php if(isset($checkbox)) echo " checked"?>>
 <input type="radio" name="radiobutton" value="a" <?php if($radiobutton=="a") echo " checked"?>>
 <input type="radio" name="radiobutton" value="b" <?php if($radiobutton=="b") echo " checked"?>>
 <input type="radio" name="radiobutton" value="c" <?php if($radiobutton=="c") echo " checked"?>>
 <input type="file" name="file" value="<?php echo $file ?>">
 <select name="select">
    <option value="1" <?php if($select=="1") echo " selected"?>>one</option>
    <option value="2" <?php if($select=="2") echo " selected"?>>two</option>
    <option value="3" <?php if($select=="3") echo " selected"?>>three</option>
  <input type="submit" name="Submit" value="Submit">

This code works - but it is probably what you had in mind when you talked about there being 'several mundane approaches' to solving this problem.

Btw - extracting $_POST is not the most security conscious thing to do, but whether or not it is a problem for you depends on the application and is your call.

When I said 'this code works' - one exception that I know of is: <input type="file"> tags in IE and Netscape will not accept a value attribute.  Opera will.  I don't know how to get IE or Netscape to populate this tag.

AFAIK, the HTML that results from the code above is what you need to produce.  You can produce it much as above, or you can produce it in neater, more reusable ways.  I use PHP classes to which I can pass a few parameters, and they generate the relevant HTML.  If you are interested, email me.

Happy New Year,

More information about the thelist mailing list