Click to See Complete Forum and Search --> : Unclassified question :)
SystemShock
Aug 10th, 2002, 06:09 AM
I don't know under what category to classify this question. :)
I have say 17 variables in a HTML page, now they all start with "ans" and have the format "ans1", "ans2", "ans3"....
I want to read these variables (and perform certain operations) one by one in a single while loop. How can it be done? I have tried various methods but nothing seems to be working. :confused:
I want something like $ans$c where c is incremented.
Thx.
Rick Bull
Aug 10th, 2002, 06:23 AM
Can't you just use an array? $ans[1], $ans[2], etc. That way you could substitute the number for $c.
Gimlin
Aug 10th, 2002, 06:26 AM
make the variable names
myvariable[1]= blah
myvariable[2]= blah blah
etc...
Then you can loop it like an array
for ($x=1; $x<=17; $x++){
print ($myvariable[$x]);
}
SystemShock
Aug 10th, 2002, 06:31 AM
I didn't know we could use arrays in HTML. I'm using this vars to get info from an HTML page. For eg,
<html>
<form>
<h2><b>Would you like to : </b></h2>
1. Do embroidery work.<br>
<input type="radio" name=ans1 value="Yes">
Yes
<input type="radio" name="ans1" value="No">
No
<input type="radio" name="ans1" value="Not Sure">
Not Sure
<br><br>
2. Play with the children.<br>
<input type="radio" name=ans2 value="Yes">
Yes
<input type="radio" name="ans2" value="No">
No
<input type="radio" name="ans2" value="Not Sure">
Not Sure
<br><br>
Now I have 17 such radio buttons. I want to read what value has been inputted by the user for each and every one of the buttons. Get me?
Gimlin
Aug 10th, 2002, 06:49 AM
Ok try
for ($x=1; $x<=17; $x++){
print ($_REQUEST["ans$x"]);
}
scoutt
Aug 10th, 2002, 08:43 AM
well you could do something like this. and then you could call it like what gimlin had
<html>
<form>
<h2><b>Would you like to : </b></h2>
1. Do embroidery work.<br>
<input type="radio" name=ans[] value="Yes">
Yes
<input type="radio" name="ans[]" value="No">
No
<input type="radio" name="ans[]" value="Not Sure">
Not Sure
<br><br>
2. Play with the children.<br>
<input type="radio" name=ans[] value="Yes">
Yes
<input type="radio" name="ans[]" value="No">
No
<input type="radio" name="ans[]" value="Not Sure">
Not Sure
<br><br>
or you can try this
while (list ($key, $value) = each ($_POST['ans'])){
echo "Answer #".$key." =". $value ."<br>";
}
something like that.
SystemShock
Aug 17th, 2002, 03:23 PM
Originally posted by Gimlin
Ok try
for ($x=1; $x<=17; $x++){
print ($_REQUEST["ans$x"]);
}
Thx but it doesnt work. :(
scoutt
Aug 17th, 2002, 04:07 PM
try
for ($x=1; $x<=17; $x++){
print ($_REQUEST["ans"][$x]);
}
da_silvy
Aug 17th, 2002, 11:58 PM
or maybe?
$var = "ans".$x;
print ($_REQUEST[$var]);
I'm not entirely sure what you are trying to achieve
mralston
Aug 19th, 2002, 02:20 PM
I believe that $_REQUEST[] won't work in earlier PHP versions.
As long as you put [] after the names of your <input > which share the same name (like scoutt suggested), then PHP will will create an array with that name. You can then use a for() loop to run through the array,whether you use the new $_REQUEST[] collection or not.
Gimlin
Aug 19th, 2002, 04:56 PM
Originally posted by da_silvy
or maybe?
$var = "ans".$x;
print ($_REQUEST[$var]);
I'm not entirely sure what you are trying to achieve
He wants a form with, for example 4 inputs ans1 ans2 ans3 ans4. He do whatever with them, using a loop.
I dont know why mine didnt work, I tested it myself.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.