By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,978 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,978 IT Pros & Developers. It's quick & easy.

Multiple Text Boxes help

P: 12
I need to write a form that asks the usser to define the number of problems (this is simply from a dropdown menu), depending on the number they select they will be asked for a definition of each problem in a seperate text box (one text box for each problem). So if they pick 2 as the number of problem they will be given two text boxes, 5 boxes for selecting 5 from the drop down list.
I think I need some javascript (for form validation), some php for the form-mailer, but it's the dynamic text boxes I'm at a total loss at.
Can anyone point me in anything like the right direction?
Aug 6 '07 #1
Share this Question
Share on Google+
10 Replies


100+
P: 228
jx2
but it's the dynamic text boxes I'm at a total loss at.
i recomend javascript and DHTML for that purpose
you should have something like that in your HTML - <div id='textboxes'></div>

drop down list with a numbers can call a function i wrote for you below
Expand|Select|Wrap|Line Numbers
  1. function boxes(NumberOfBoxes){
  2.      for(i=0;i<NumberOfBoxes;i++){
  3.               boxes += "<textarea name='textbox[NumberOfBoxes'></textarea>";
  4.      }
  5.               document.getElementById('textboxes').innerHTML = boxes;
  6. }
i haven test it so ...

i hope that help

jx2
Aug 6 '07 #2

P: 12
I tried this;

<html>
<head>


</head>


<body>

<select name="boxes" onChange="boxes()">
<option value="">choose</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<script type="text/javascript">
function boxes(NumberOfBoxes){
for(i=0;i<NumberOfBoxes;i++){
boxes += "<textarea name='textbox[NumberOfBoxes]'></textarea>";
}
document.getElementById('textboxes').innerHTML = boxes;
}
</script>
<div id='textboxes'>
boxes should go here
</div>
</body>

</html>
and it give only one text box with loads of script showing :-(

Think I'll have another go at work tomorrow.
Aug 6 '07 #3

100+
P: 228
jx2
<select name="boxes" onChange="boxes()">

// boxes need one parameter "number" of textboxes
// u need to pass option value to it, e.g. boxes(2);
// u might try this boxes(this.value); - i'm not sure if that will work
// but u need to pass a value somehow

regards
jx2
Aug 6 '07 #4

100+
P: 228
jx2
i think you will like me:-)

[html]<html>
<head>


</head>


<body>

<select name="boxes" onChange="boxes(this.value)">
<option value="">choose</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<script type="text/javascript"><!--
function boxes(NumberOfBoxes){
var i,boxes='';


for(i=0; i<NumberOfBoxes; i++ ){
boxes += "<textarea name='textbox["+i+"]'></textarea>";
}
document.getElementById('textboxes').innerHTML = boxes;
}
--></script>
<div id='textboxes'>
boxes should go here
</div>
</body>

</html>[/html]

i check it!! it work very well
you'll get array textbox[]
preety cool hah

regards
jx2
Aug 6 '07 #5

P: 12
No good, well this line;

<select name="boxes" onChange="boxes(this.value)">

gave internet explorer an error (Object doesn't support this property or method), firefox just didn't do anything.

Any other ideas?
Aug 7 '07 #6

100+
P: 228
jx2
[html]<html>
<head>

<script type="text/javascript"><!--
function fboxes(NumberOfBoxes){
var i,boxes='';


for(i=0; i<NumberOfBoxes; i++ ){
boxes += "<textarea name='textbox["+i+"]'></textarea>";
}
document.getElementById('textboxes').innerHTML = boxes;
}
--></script>
</head>


<body>

<select name="boxes" onChange="fboxes(this.value)">
<option value="">choose</option>
<option value="1">1</option>
<option value="2">2</option>
</select>


<div id='textboxes'>
boxes should go here
</div>
</body>

</html>[/html]

well - i test it in IE and firefox it works very well in both of them check the syntacs of your code
i have also change the name of the function that could cos problems...
it work in my browsers
IT HAS TO WORK in yours!!!

regards
jx2
Aug 7 '07 #7

100+
P: 228
jx2
i know what might be wrong if you copy this code from here - lines 10 and 11 should be in the same line my guess is that that might had cos your problems

regards
jx2
Aug 7 '07 #8

P: 12
Fantastic! got it working, thank you very much (that saved me much hair pulling and headaches).
A further question would be how to reference the text boxes in the form-mailer php script. If I define each box

$textbox = ($_POST["textbox"]);
$textbox1 = ($_POST["textbox1"]);
$textbox2 = ($_POST["textbox2"]);

I get a result "Array,,,"

If I use $textbox["+i+"] = ($_POST["textbox["+i+"]"]);

I get nothing.

Any ideas on this part?
Aug 7 '07 #9

100+
P: 228
jx2
A further question would be how to reference the text boxes in the form-mailer php script. If I define each box

$textbox = ($_POST["textbox"]);
$textbox1 = ($_POST["textbox1"]);
$textbox2 = ($_POST["textbox2"]);

I get a result "Array,,,"

Any ideas on this part?
well actualy , i did allready answer your question :-)
fboxes function creates textareas as an array - look at names !!! of each box $textbox[1] ,$textbox[2] and so on well its no good practise (u should use $_POST['textbox'][1]) but it simplier to explain and to use
by the way if you need to know what is in the memory print it!!!
e.g.
[php]<?php
function echovariables($variable){
echo "<ul>";
foreach($variable as $k=>$v){
echo "<li>$k =>$v </li>";
if(is_array($v)){
echovariables($v);
}
}
echo "</ul>";
}
echovariables($_POST);//can be any variable
?>
[/PHP]

if you try it u ll see what you got

regards jx2
Aug 7 '07 #10

P: 12
You have been a great help, thank you. I need to brush up on php and arrays.
Aug 7 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.