|
-
Aug 10th, 2002, 06:09 AM
#1
Thread Starter
Member
Unclassified question :)
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.
I want something like $ans$c where c is incremented.
Thx.
-
Aug 10th, 2002, 06:23 AM
#2
Frenzied Member
Can't you just use an array? $ans[1], $ans[2], etc. That way you could substitute the number for $c.
-
Aug 10th, 2002, 06:26 AM
#3
Fanatic Member
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]);
}
-
Aug 10th, 2002, 06:31 AM
#4
Thread Starter
Member
I didn't know we could use arrays in HTML. I'm using this vars to get info from an HTML page. For eg,
Code:
<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?
-
Aug 10th, 2002, 06:49 AM
#5
Fanatic Member
Ok try
PHP Code:
for ($x=1; $x<=17; $x++){
print ($_REQUEST["ans$x"]);
}
-
Aug 10th, 2002, 08:43 AM
#6
Lively Member
well you could do something like this. and then you could call it like what gimlin had
Code:
<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
PHP Code:
while (list ($key, $value) = each ($_POST['ans'])){
echo "Answer #".$key." =". $value ."<br>";
}
something like that.
Just Imagine what you can do with the power of php.
257 Tutorials and counting.
529 Snippets and growing.
Add yours today @
SnippetLibrary.com
-
Aug 17th, 2002, 03:23 PM
#7
Thread Starter
Member
Originally posted by Gimlin
Ok try
PHP Code:
for ($x=1; $x<=17; $x++){
print ($_REQUEST["ans$x"]);
}
Thx but it doesnt work.
-
Aug 17th, 2002, 04:07 PM
#8
Lively Member
try
PHP Code:
for ($x=1; $x<=17; $x++){
print ($_REQUEST["ans"][$x]);
}
Just Imagine what you can do with the power of php.
257 Tutorials and counting.
529 Snippets and growing.
Add yours today @
SnippetLibrary.com
-
Aug 17th, 2002, 11:58 PM
#9
Conquistador
or maybe?
PHP Code:
$var = "ans".$x;
print ($_REQUEST[$var]);
I'm not entirely sure what you are trying to achieve
-
Aug 19th, 2002, 02:20 PM
#10
Addicted Member
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.
-
Aug 19th, 2002, 04:56 PM
#11
Fanatic Member
Originally posted by da_silvy
or maybe?
PHP Code:
$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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|