-
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. :confused:
I want something like $ans$c where c is incremented.
Thx.
-
Can't you just use an array? $ans[1], $ans[2], etc. That way you could substitute the number for $c.
-
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]);
}
-
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?
-
Ok try
PHP Code:
for ($x=1; $x<=17; $x++){
print ($_REQUEST["ans$x"]);
}
-
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.
-
Quote:
Originally posted by Gimlin
Ok try
PHP Code:
for ($x=1; $x<=17; $x++){
print ($_REQUEST["ans$x"]);
}
Thx but it doesnt work. :(
-
try
PHP Code:
for ($x=1; $x<=17; $x++){
print ($_REQUEST["ans"][$x]);
}
-
or maybe?
PHP Code:
$var = "ans".$x;
print ($_REQUEST[$var]);
I'm not entirely sure what you are trying to achieve
-
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.
-
Quote:
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.