Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Text Box Generation Help...

Could you explain how this might work?

         

madcat

12:32 pm on Apr 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi:

I need to ask for some help building a function that creates dynamic text boxes. This is an example link, I'd like this function to work EXACTLY the same way...just keep clicking 'Enter More Numbers':

[ups.com...]
*Just let me know if I need to put this in my profile.

The only difference is, I will have about 10 different sections of information that I'll need this function to work for. If the UPS site creates new boxes for 'Tracking', mine needs to create them for tracking, pin numbers, part numbers, mfg numbers, etc.

My aim is to somehow (my php skills are null) build one all purpose function for this. If 'Enter More Numbers' is clicked on my site I would like the boxes to be spit out in sets of 5 to 25 (if needed by user). That means that each name value (within generated text box) will need to be dynamically generated so that they could be input to a database.

Would it be easier to build a function for each section, or one all-purpose function. Also, what would be the best way to start building this all-purpose function? Here is what 2 out of the 10 sections might look like. The first five boxes are always visible for every section:

Tracking Number
-----------------
track1
track2
track3
track4
track5
----
Enter More Numbers Link >
Generate 5 More boxes until link is clicked again till 25

Pin Number
-----------------
pin1
pin2
pin3
pin4
pin5
----
Enter More Numbers Link >
Generate 5 More boxes until link is clicked again till 25

Any help is greatly appreciated as I feel way in over my head.

M

mykel79

12:52 pm on Apr 13, 2004 (gmt 0)

10+ Year Member



The basic idea would be to print out these text boxes in a loop.
if(isset($_GET['nr_boxes'])) $i_max=$_GET['nr_boxes'];
else $i_max=5;
For ($i=1;$i<=$i_max;$i++) {
echo '.....';
}

Then, you can just have the link "show more boxes" be
$i_max=$i_max+5;
echo '<a href="script.php?nr_boxes=',$i_max,'">show more boxes</a>';

which would set a higher $nr_boxes than before. There are numerous ways you could approach this. You can start by reading the manual on php.net, about loops.

As for reading the values entered by the user, you can name the text boxes in the form like this:
tracking_value[1]
tracking_value[2]
tracking_value[3]
tracking_value[4]

When someone submits the form, the values will be posted into php as an array.

seomike2003

1:18 pm on Apr 13, 2004 (gmt 0)



<table>
<?
///variables

if(isset($tnum)){
$num=$tnum;
$more=$num+5;
}else{
$more=10;
$num=5;
}

///loop
for($i=1;$i<=$num;$i++){?>
<tr>
<td>
<input name="tracking<? echo $i?>" type="text" id="tracking<? echo $i?>">
</tr>
</td>
<?
}
?>
</table>
<a href="tester.php?tnum=<? echo $more?>">Add more</a>

Birdman

2:03 pm on Apr 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe it's just me, but I would do this with javascript to avoid a new page load every time. I'm only going up to 15 entries to keep the code down but you will get the idea.

<style type="text/css">
#track_6_10,#track_11_15{display: none;}
</style>

<input type="text" name="track1" />
<input type="text" name="track2" />
<input type="text" name="track3" />
<input type="text" name="track4" />
<input type="text" name="track5" />
<div id="track_6_10">
<input type="text" name="track6" />
<input type="text" name="track7" />
<input type="text" name="track8" />
<input type="text" name="track9" />
<input type="text" name="track10" />
</div>
<div id="track_11_15">
<input type="text" name="track11" />
<input type="text" name="track12" />
<input type="text" name="track13" />
<input type="text" name="track14" />
<input type="text" name="track15" />
</div>
<a href="javascript: showHide(track)">Enter more numbers</a>

function showHide(section){
f = document.forms[0].elements;

if(f[section+"6_10"].style.display == "none"){
f[section+"6_10"].style.display == "block";
return;
} else {
if(f[section+"11_15"].style.display == "none"){
f[section+"11_15"].style.display == "block";
return;
} else {
return;
}
}

Probably a bit confusing but it really is a nice trick for adding optional form elements.

WhosAWhata

2:15 am on Apr 14, 2004 (gmt 0)

10+ Year Member



i like that idea
very interesting